import { Cell } from "@antv/x6";
import { merge, cloneDeep } from "lodash-es";
import { getCompMap } from "@/components/flowchartNode";
import { nodeMenu, edgeMenu } from "./contentMenu";

/**
 * 根据元素基本信息获取完整元素信息
 */
export const getFlowNodeMetaByBasic = (
  item: Cell.Properties,
  defaultDataByTheme: any
) => {
  if (item.shape !== "edge") {
    const nodeMeta = getCompMap()[item.shape as string] || {};
    // ports 先取自身 再判断节点本身是否带有 最后取通用的ports
    const ports: { items: any[]; groups: any[] } =
      nodeMeta?.ports || defaultDataByTheme.ports;
    if (item.ports) {
      ports.items = JSON.parse(item.ports || "")?.items || [];
    }
    const newNode = {
      ...item,
      data: JSON.parse(item?.data || "{}"),
      size: JSON.parse(item?.size || "{}"),
      position: JSON.parse(item?.position || "{}"),
      tools: [
        {
          name: "contextmenu",
          args: {
            menu: nodeMenu,
          },
        },
      ],
      ports,
    };
    return merge(
      nodeMeta,
      {
        data: defaultDataByTheme.nodeDefaultData,
        ports: defaultDataByTheme.ports,
      },
      newNode
    );
  } else {
    const router = item?.router?.includes("{")
      ? JSON.parse(item?.router)
      : item?.router === "manhattan" // manhatan重置
        ? undefined
        : item?.router;

    const newEdge = {
      ...item,
      shape: "edge",
      data: JSON.parse(item?.data || "{}"),
      attrs: JSON.parse((item?.attrs as unknown as string) || "{}"),
      source: JSON.parse(item?.source),
      target: JSON.parse(item?.target),
      ...(item?.connector ? { connector: JSON.parse(item.connector) } : {}),
      ...(item?.labels ? { labels: JSON.parse(item.labels) } : {}),
      // 大节点可能导致连线问题，忽略这个图形
      router: router || {
        name: "manhattan",
        args: {
          excludeShapes: ["page-container-node"],
          padding: 10,
        },
      },
      tools: [
        {
          name: "contextmenu",
          args: {
            menu: edgeMenu,
          },
        },
      ],
    };
    return merge(cloneDeep(defaultDataByTheme.edgeDefaultData), newEdge);
  }
};

/**
 * 根据ai生成的数据获取完整元素信息
 */
export const getFlowNodeMetaByAi = (
  item: Cell.Properties,
  defaultDataByTheme: any
) => {
  if (item.shape !== "edge") {
    const nodeMeta = getCompMap()[item.shape as string] || {};
    // ports 先取自身 再判断节点本身是否带有 最后取通用的ports
    const ports: { items: any[]; groups: any[] } =
      nodeMeta?.ports || defaultDataByTheme.ports;
    if (item?.ports?.items) {
      ports.items = item.ports.items;
    }
    const newNode = {
      ...item,
      tools: [
        {
          name: "contextmenu",
          args: {
            menu: nodeMenu,
          },
        },
      ],
      ports,
    };
    return merge(
      nodeMeta,
      {
        data: defaultDataByTheme.nodeDefaultData,
        ports: defaultDataByTheme.ports,
      },
      newNode
    );
  } else {
    const newEdge = {
      ...item,
      tools: [
        {
          name: "contextmenu",
          args: {
            menu: edgeMenu,
          },
        },
      ],
      router: {
        name: "manhattan",
        args: {
          excludeShapes: ["page-container-node"],
          padding: 10,
        },
      },
    };
    return merge(cloneDeep(defaultDataByTheme.edgeDefaultData), newEdge);
  }
};
