import { describe, expect, it } from "vitest";
import {
  applyMindmapCommandsLocally,
  applyMetadataOnlyMindmapCommands,
  isLocallyApplicableMindmapCommandSet,
  isMetadataOnlyMindmapCommandSet,
} from "./mindmap-command-apply-local";

describe("mindmap command apply local", () => {
  it("识别 metadata-only 命令集合", () => {
    expect(
      isMetadataOnlyMindmapCommandSet([
        { type: "setLayout", mindmapId: "mind_1", layout: "mindMap" },
        { type: "setTheme", mindmapId: "mind_1", theme: "dark" },
        { type: "patchView", mindmapId: "mind_1", patch: { state: { scale: 1.2 } } },
        { type: "compatPayloadPatch", mindmapId: "mind_1", path: "root.data.fillColor", value: "#dbeafe" },
      ]),
    ).toBe(true);
    expect(
      isMetadataOnlyMindmapCommandSet([
        { type: "insertChild", mindmapId: "mind_1", parentNodeId: "root", node: { text: "新节点" } },
      ]),
    ).toBe(false);
  });

  it("把 layout/theme/view/compat patch 合并回完整 mindmap blob", () => {
    const result = applyMetadataOnlyMindmapCommands(
      {
        data: { uid: "root", text: "KMIND" },
        children: [{ data: { uid: "node_1", text: "子节点" }, children: [] }],
        layout: "logicalStructure",
        theme: "classic",
        themeConfig: { lineColor: "#60a5fa" },
        view: { state: { scale: 1 } },
        compatPayload: { source: "compat-blob" },
      },
      [
        { type: "setLayout", mindmapId: "mind_1", layout: "mindMap" },
        { type: "setTheme", mindmapId: "mind_1", theme: "dark" },
        { type: "patchView", mindmapId: "mind_1", patch: { state: { scale: 1.25, x: 10 } } },
        { type: "compatPayloadPatch", mindmapId: "mind_1", path: "nodes.node_1.data.fillColor", value: "#dbeafe" },
        { type: "compatPayloadPatch", mindmapId: "mind_1", path: "style.map.backgroundColor", value: "#0f172a" },
      ],
    );

    expect(result.errors).toEqual([]);
    expect(result.applied).toBe(5);
    expect(result.data.layout).toBe("mindMap");
    expect(result.data.theme).toBe("dark");
    expect(result.data.view).toEqual({ state: { scale: 1.25, x: 10 } });
    expect(result.data.children[0].data.fillColor).toBe("#dbeafe");
    expect(result.data.compatPayload).toEqual({
      source: "compat-blob",
      style: {
        map: {
          backgroundColor: "#0f172a",
        },
      },
    });
  });

  it("识别可本地应用的结构命令集合", () => {
    expect(
      isLocallyApplicableMindmapCommandSet([
        { type: "updateText", mindmapId: "mind_1", nodeId: "node_1", text: "改名" },
        { type: "insertChild", mindmapId: "mind_1", parentNodeId: "node_1", node: { uid: "node_2", text: "子节点" } },
        { type: "insertSiblingAfter", mindmapId: "mind_1", targetNodeId: "node_1", node: { uid: "node_3", text: "同级" } },
        { type: "deleteNode", mindmapId: "mind_1", nodeId: "node_3" },
      ]),
    ).toBe(true);
    expect(
      isLocallyApplicableMindmapCommandSet([
        { type: "moveNode", mindmapId: "mind_1", nodeId: "node_1", newParentNodeId: "root" },
      ]),
    ).toBe(false);
  });

  it("可本地应用 update/insert/delete 到 mindmap tree，避免每次都整块重挂载", () => {
    const result = applyMindmapCommandsLocally(
      {
        data: { uid: "root", text: "KMIND" },
        children: [
          {
            data: { uid: "node_1", text: "节点 1" },
            children: [],
          },
          {
            data: { uid: "node_2", text: "节点 2" },
            children: [],
          },
        ],
        layout: "logicalStructure",
        theme: "default",
      },
      [
        { type: "updateText", mindmapId: "mind_1", nodeId: "node_1", text: "节点 1A" },
        {
          type: "insertChild",
          mindmapId: "mind_1",
          parentNodeId: "node_1",
          node: { uid: "node_1_child", text: "子节点 A" },
        },
        {
          type: "insertSiblingAfter",
          mindmapId: "mind_1",
          targetNodeId: "node_1",
          node: { uid: "node_1_sibling", text: "同级 A" },
        },
        { type: "deleteNode", mindmapId: "mind_1", nodeId: "node_2" },
      ],
    );

    expect(result.errors).toEqual([]);
    expect(result.applied).toBe(4);
    expect(result.data.children).toHaveLength(2);
    expect(result.data.children[0].data.text).toBe("节点 1A");
    expect(result.data.children[0].children[0].data.uid).toBe("node_1_child");
    expect(result.data.children[1].data.uid).toBe("node_1_sibling");
    expect(result.data.children[1].data.text).toBe("同级 A");
  });
});
