import { defaultNodeTypes } from '@/components/FlowEditor/node/types/defaultType';
import BasicNode from '@/components/FlowEditor/node/basicNode/BasicNode';
import SwitchNode from '@/components/FlowEditor/node/switchNode/SwitchNode';
import ImageNode from '@/components/FlowEditor/node/imageNode/ImageNode';
import CodeNode from '@/components/FlowEditor/node/codeNode/CodeNode';
import RestNode from '@/components/FlowEditor/node/restNode/RestNode';
import LocalNode from '@/components/FlowEditor/node/localNode/LocalNode';

// 获取handle类型 (api或data)
const getHandleType = (handleId: string, nodeParams: any) => {
  // 检查是否为api类型的handle
  const apiOuts = nodeParams.apiOuts || [];
  const apiIns = nodeParams.apiIns || [];

  if (apiOuts.some((api: any) => (api?.eventId || api.name || api.id) === handleId) ||
    apiIns.some((api: any) => (api?.eventId || api.name || api.id) === handleId) || (handleId.includes('loop'))) {
    return 'api';
  }

  // 检查是否为data类型的handle
  const dataOuts = nodeParams.dataOuts || [];
  const dataIns = nodeParams.dataIns || [];

  if (dataOuts.some((data: any) => (data.name || data.id) === handleId) ||
    dataIns.some((data: any) => (data.name || data.id) === handleId)) {
    return 'data';
  }

  // 默认为data类型
  return 'data';
};

// 验证数据类型是否匹配
const validateDataType = (sourceNode: defaultNodeTypes, targetNode: defaultNodeTypes, sourceHandleId: string, targetHandleId: string) => {
  const sourceParams = sourceNode.data?.parameters || {};
  const targetParams = targetNode.data?.parameters || {};

  // 获取源节点的输出参数
  let sourceDataType = '';
  const sourceApiOuts = sourceParams.apiOuts || [];
  const sourceDataOuts = sourceParams.dataOuts || [];

  // 查找源handle的数据类型
  const sourceApi = sourceApiOuts.find((api: any) => api.name === sourceHandleId);
  const sourceData = sourceDataOuts.find((data: any) => data.name === sourceHandleId);

  if (sourceApi) {
    sourceDataType = sourceApi.dataType || '';
  }
  else if (sourceData) {
    sourceDataType = sourceData.dataType || '';
  }

  // 获取目标节点的输入参数
  let targetDataType = '';
  const targetApiIns = targetParams.apiIns || [];
  const targetDataIns = targetParams.dataIns || [];

  // 查找目标handle的数据类型
  const targetApi = targetApiIns.find((api: any) => api.name === targetHandleId);
  const targetData = targetDataIns.find((data: any) => data.name === targetHandleId);

  if (targetApi) {
    targetDataType = targetApi.dataType || '';
  }
  else if (targetData) {
    targetDataType = targetData.dataType || '';
  }

  // 如果任一数据类型为空，则允许连接
  if (!sourceDataType || !targetDataType) {
    return true;
  }

  // 比较数据类型是否匹配
  return sourceDataType === targetDataType;
};


// 根据节点类型获取对应的节点组件
const getNodeComponent = (nodeType: string) => {
  switch (nodeType) {
    case 'BASIC':
    case 'SUB':
      return BasicNode;
    case 'SWITCH':
      return SwitchNode;
    case 'IMAGE':
      return ImageNode;
    case 'CODE':
      return CodeNode;
    case 'REST':
      return RestNode;
    default:
      return LocalNode;
  }
};


export {
  getHandleType,
  validateDataType,
  getNodeComponent
};