import { describe, expect, it } from "vitest";
import {
  createDefaultMindmapShellInteractionState,
  deriveMindmapUiState,
  reduceMindmapChromeVisibility,
} from "./mindmap-ui-state";

describe("mindmap UI state", () => {
  it("无 active node 时禁用节点编辑动作，但保留视图动作", () => {
    const state = deriveMindmapUiState({ activeNodeId: null, readonly: false });

    expect(state.disabledActions.insertChild).toBe(true);
    expect(state.disabledActions.insertSiblingAfter).toBe(true);
    expect(state.disabledActions.deleteNode).toBe(true);
    expect(state.disabledActions.expandCollapse).toBe(true);
    expect(state.disabledActions.zoomIn).toBe(false);
    expect(state.disabledActions.centerRoot).toBe(false);
    expect(state.disabledActions.fullscreenCanvas).toBe(false);
    expect(state.disabledActions.exitFullscreen).toBe(false);
  });

  it("readonly 时禁用编辑动作，保留搜索和视图动作", () => {
    const state = deriveMindmapUiState({ activeNodeId: "node_1", readonly: true });

    expect(state.disabledActions.insertChild).toBe(true);
    expect(state.disabledActions.note).toBe(true);
    expect(state.disabledActions.setTheme).toBe(true);
    expect(state.disabledActions.expandCollapse).toBe(true);
    expect(state.disabledActions.copyNodeText).toBe(false);
    expect(state.disabledActions.search).toBe(false);
    expect(state.disabledActions.zoomOut).toBe(false);
    expect(state.disabledActions.fullscreenCanvas).toBe(false);
    expect(state.disabledActions.fullscreenPage).toBe(false);
  });

  it("缺少 runtime capability 时禁用对应 action", () => {
    const state = deriveMindmapUiState({
      activeNodeId: "node_1",
      readonly: false,
      runtimeCapabilities: ["runtimeCommand", "kernelCommand"],
    });

    expect(state.disabledActions.note).toBe(true);
    expect(state.disabledActions.insertChild).toBe(false);
  });

  it("导出能力暂缓时在 UI state 中保持禁用", () => {
    const state = deriveMindmapUiState({ activeNodeId: "node_1", readonly: false });

    expect(state.disabledActions.export).toBe(true);
  });

  it("派生默认 shell state，给 Leptos shell 提供稳定合同", () => {
    const state = deriveMindmapUiState({
      activeNodeId: "node_1",
      readonly: true,
      shell: {
        chromeVisibility: "hiddenByPointerLeave",
        toolbarOverflow: {
          availableWidth: 640,
          visibleActionIds: ["undo", "redo"],
          overflowActionIds: ["image"],
          moreOpen: true,
        },
        fullscreen: {
          mode: "canvas",
          isFullscreen: true,
          apiAvailable: false,
        },
        sidebar: {
          triggerVisible: false,
          panelOpen: false,
          activePanelId: "structure",
          collapsedByToggle: true,
        },
        navigator: {
          searchOpen: true,
          minimapOpen: true,
          zoomPercent: 138.4,
        },
      },
    });

    expect(state.shell.chromeVisibility).toBe("hiddenByPointerLeave");
    expect(state.shell.toolbarOverflow).toMatchObject({
      availableWidth: 640,
      visibleActionIds: ["undo", "redo"],
      overflowActionIds: ["image"],
      moreOpen: true,
    });
    expect(state.shell.fullscreen).toMatchObject({
      mode: "canvas",
      isFullscreen: true,
      target: "mindmap-root",
      apiAvailable: false,
    });
    expect(state.shell.sidebar).toMatchObject({
      triggerVisible: false,
      panelOpen: false,
      activePanelId: "structure",
      drawerWidth: 300,
      collapsedByToggle: true,
    });
    expect(state.shell.navigator).toMatchObject({
      searchOpen: true,
      minimapOpen: true,
      readonly: true,
      zoomPercent: 138,
      mouseBehavior: "leftSelectRightDrag",
    });
  });

  it("鼠标移出隐藏 chrome，鼠标移入不自动恢复，点击才恢复", () => {
    const hidden = reduceMindmapChromeVisibility("visible", "pointerLeave");
    expect(hidden).toBe("hiddenByPointerLeave");
    expect(reduceMindmapChromeVisibility(hidden, "pointerEnter")).toBe("hiddenByPointerLeave");
    expect(reduceMindmapChromeVisibility(hidden, "restoreClick")).toBe("visible");
  });

  it("sidebar 隐藏触发条后保留最近 active panel", () => {
    const state = createDefaultMindmapShellInteractionState({
      sidebar: {
        triggerVisible: false,
        panelOpen: false,
        activePanelId: "structure",
        collapsedByToggle: true,
      },
    });

    expect(state.sidebar.triggerVisible).toBe(false);
    expect(state.sidebar.panelOpen).toBe(false);
    expect(state.sidebar.activePanelId).toBe("structure");
    expect(state.sidebar.collapsedByToggle).toBe(true);

    const restored = createDefaultMindmapShellInteractionState({
      sidebar: {
        ...state.sidebar,
        triggerVisible: true,
        panelOpen: true,
        collapsedByToggle: false,
      },
    });
    expect(restored.sidebar.activePanelId).toBe("structure");
  });
});
