import { cloneDeep, merge } from "lodash-es";
import { defaultData } from "./config/data";
import { Cell } from "@antv/x6";
import { getCompMap } from "./components/flowchartNode";
export * from "./components";

/**
 * 根据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,
      ports,
    };
    return merge(
      nodeMeta,
      {
        data: defaultDataByTheme.nodeDefaultData,
        ports: defaultDataByTheme.ports,
      },
      newNode
    );
  } else {
    const newEdge = {
      ...item,
      router: {
        name: "manhattan",
        args: {
          excludeShapes: ["page-container-node"],
          padding: 10,
        },
      },
    };
    return merge(cloneDeep(defaultDataByTheme.edgeDefaultData), newEdge);
  }
};

/**
 * 根据AI生成的数据获取节点元数据
 * @param data AI生成的数据
 */
export const getFlowchartNodesByAi: (data: any) =>  Cell.Metadata[] = (data) => {
  // 数组
  if (Array.isArray(data)) {
    const list = (data || []).map((item) =>
      getFlowNodeMetaByAi(item, defaultData)
    );
    console.log("=====ai creator cells:=======\n", list);
    return list as Cell.Metadata[];
  }
  // 对象
  if (data instanceof Object) {
    const cell = getFlowNodeMetaByAi(data, defaultData);
    return [cell] as Cell.Metadata[];
  }

  return [];
};
