import assert from "node:assert/strict";

import {
  createControlPageSessionLoadState,
  loadMachineSessionForControlPageWorkflow,
} from "../../../runtime/ui/ini-panel/control-page-session-workflow.js";

const stateBundle = {
  report: {
    readiness: {
      ini: "INI WASM: ready",
      interp: "Interpreter WASM: ready",
      opfs: "OPFS: ready",
    },
  },
  overview: {
    phase: "ready",
  },
};
const workflowStatus = {
  overview: {
    phase: "ready",
  },
  sessionReadiness: {
    phase: "ready",
    ready: true,
    missing: [],
  },
  lastAction: "load-session",
  error: null,
};
const controlView = {
  status: {
    lastAction: "load-session",
    runStatus: null,
    error: null,
  },
  sections: [],
};

assert.deepEqual(createControlPageSessionLoadState({ phase: "waiting-panel" }), {
  phase: "waiting-panel",
  panelReady: false,
  apiReady: false,
  controlViewReady: false,
  stateBundle: null,
  workflowStatus: null,
  sessionReadiness: null,
  controlView: null,
  error: null,
});

assert.deepEqual(
  loadMachineSessionForControlPageWorkflow({
    getPanelWindow: () => null,
    getPanelApi: () => null,
    getControlView: () => null,
  }),
  createControlPageSessionLoadState({ phase: "waiting-panel" }),
);

assert.deepEqual(
  loadMachineSessionForControlPageWorkflow({
    getPanelWindow: () => ({}),
    getPanelApi: () => null,
    getControlView: () => null,
  }),
  createControlPageSessionLoadState({
    phase: "waiting-api",
    panelReady: true,
  }),
);

const panelApiWithoutControlView = {
  getMachineSessionStateBundle: () => stateBundle,
  getMachineSessionWorkflowStatus: () => workflowStatus,
};
assert.deepEqual(
  loadMachineSessionForControlPageWorkflow({
    getPanelWindow: () => ({}),
    getPanelApi: () => panelApiWithoutControlView,
    getControlView: () => null,
  }),
  createControlPageSessionLoadState({
    phase: "waiting-session",
    panelReady: true,
    apiReady: true,
    stateBundle,
    workflowStatus,
    sessionReadiness: workflowStatus.sessionReadiness,
  }),
);

const panelApi = {
  ...panelApiWithoutControlView,
  getMachineSessionControlView: () => controlView,
};
assert.deepEqual(
  loadMachineSessionForControlPageWorkflow({
    getPanelWindow: () => ({}),
    getPanelApi: () => panelApi,
    getControlView: () => controlView,
  }),
  createControlPageSessionLoadState({
    phase: "ready",
    panelReady: true,
    apiReady: true,
    controlViewReady: true,
    stateBundle,
    workflowStatus,
    sessionReadiness: workflowStatus.sessionReadiness,
    controlView,
  }),
);

assert.deepEqual(
  loadMachineSessionForControlPageWorkflow({
    getPanelWindow: () => {
      throw new Error("cross-origin frame");
    },
    getPanelApi: () => null,
    getControlView: () => null,
  }),
  createControlPageSessionLoadState({
    phase: "error",
    error: "cross-origin frame",
  }),
);

assert.throws(
  () => loadMachineSessionForControlPageWorkflow(),
  /panel window getter/,
);

console.log("ini_panel_control_page_session_workflow_node_smoke=ok");
