/*
*  组件编排处理
* */
// 组件编排画布数据处理(组件编排)
import { convertFlowData } from '@/utils/convertFlowData';
import { Edge } from '@xyflow/react';
import { updateCanvasDataMap } from '@/store/ideContainer';

export const projectFlowHandle = (initialData, useDefault, setNodes, setEdges, dispatch, canvasDataMap) => {
  const {
    nodes: convertedNodes,
    edges: convertedEdges
  } = convertFlowData(initialData?.main?.components || initialData?.components, useDefault);

  // 为所有边添加类型
  const initialEdges: Edge[] = convertedEdges.map(edge => ({
    ...edge,
    type: 'custom'
  }));

  setNodes(convertedNodes);
  setEdges(initialEdges);

  // 确定要使用的key: 对于子流程使用flowId，对于主流程使用appId
  const cacheKey = initialData?.flowId || initialData?.appId;

  if (cacheKey) {
    dispatch(updateCanvasDataMap({
      ...canvasDataMap,
      [cacheKey]: { nodes: convertedNodes, edges: initialEdges }
    }));
  }
};