import assert from "node:assert/strict";

import {
  controlPageStatusText,
  createControlPageRefreshState,
  refreshMachineSessionControlPage,
} from "../../../runtime/ui/ini-panel/control-page-refresh-workflow.js";

const expectedView = {
  status: {
    lastAction: "run-gcode",
    runStatus: "ok",
    error: null,
  },
  sections: [
    {
      id: "run",
      rows: [
        { label: "Canonical events", value: 2 },
      ],
    },
  ],
};

assert.equal(controlPageStatusText(expectedView.status), "run-gcode | ok | -");
assert.deepEqual(createControlPageRefreshState({ phase: "waiting", statusText: "waiting for panel" }), {
  phase: "waiting",
  statusText: "waiting for panel",
  view: null,
  renderResult: null,
  error: null,
});

const waitingStatusNode = { textContent: "" };
const waitingViewNode = { textContent: "previous" };
assert.deepEqual(
  refreshMachineSessionControlPage({
    getControlView: () => null,
    statusNode: waitingStatusNode,
    viewNode: waitingViewNode,
    renderControlView: () => "unused",
  }),
  {
    phase: "waiting",
    statusText: "waiting for panel",
    view: null,
    renderResult: null,
    error: null,
  },
);
assert.equal(waitingStatusNode.textContent, "waiting for panel");
assert.equal(waitingViewNode.textContent, "");

const renderedStatusNode = { textContent: "" };
const renderedViewNode = { textContent: "" };
let renderedInput = null;
assert.deepEqual(
  refreshMachineSessionControlPage({
    getControlView: () => expectedView,
    statusNode: renderedStatusNode,
    viewNode: renderedViewNode,
    renderControlView: (container, view) => {
      renderedInput = { container, view };
      return { rendered: true };
    },
  }),
  {
    phase: "rendered",
    statusText: "run-gcode | ok | -",
    view: expectedView,
    renderResult: { rendered: true },
    error: null,
  },
);
assert.deepEqual(renderedInput, { container: renderedViewNode, view: expectedView });
assert.equal(renderedStatusNode.textContent, "run-gcode | ok | -");

const getterErrorStatusNode = { textContent: "" };
const getterErrorViewNode = { textContent: "previous" };
assert.deepEqual(
  refreshMachineSessionControlPage({
    getControlView: () => {
      throw new Error("panel unavailable");
    },
    statusNode: getterErrorStatusNode,
    viewNode: getterErrorViewNode,
    renderControlView: () => "unused",
  }),
  {
    phase: "error",
    statusText: "refresh failed | - | panel unavailable",
    view: null,
    renderResult: null,
    error: "panel unavailable",
  },
);
assert.equal(getterErrorViewNode.textContent, "");

const renderErrorStatusNode = { textContent: "" };
const renderErrorViewNode = { textContent: "previous" };
assert.deepEqual(
  refreshMachineSessionControlPage({
    getControlView: () => expectedView,
    statusNode: renderErrorStatusNode,
    viewNode: renderErrorViewNode,
    renderControlView: () => {
      throw new Error("renderer unavailable");
    },
  }),
  {
    phase: "error",
    statusText: "refresh failed | - | renderer unavailable",
    view: expectedView,
    renderResult: null,
    error: "renderer unavailable",
  },
);
assert.equal(renderErrorStatusNode.textContent, "refresh failed | - | renderer unavailable");
assert.equal(renderErrorViewNode.textContent, "");

assert.throws(
  () => refreshMachineSessionControlPage(),
  /control view getter/,
);

console.log("ini_panel_control_page_refresh_workflow_node_smoke=ok");
