// node.ts

import { Graph } from '@antv/x6';
import { Images } from '@/api/rule/images';

// 注册自定义节点
export function registerAlgoNode() {
  Graph.registerNode(
    'algo-node',
    {
      inherit: 'rect', // 继承自矩形节点
      width: 180,
      height: 36,
      attrs: {
        body: {
          stroke: 'rgba(30,46,58,0.2)',
          strokeWidth: 1,
          fill: '#ffffff',
          rx: 4,
          ry: 4
        },
        borderLeft: {
          type: 'rect',
          stroke: 'none',
          x: 0,
          y: 0,
          width: 6,
          height: 36,
          fill: '#0c51c0'
        },
        logo: {
          type: 'image',
          refX: 8,
          refY: 9,
          width: 20,
          height: 20
        },
        label: {
          type: 'text',
          refX: 44,
          refY: 18,
          textAnchor: 'start',
          textVerticalAnchor: 'middle',
          fontSize: 13,
          fill: '#666'
        },
        //删除工具容器
        deleteTools: {
          type: 'rect',
          stroke: 'none',
          fill: '#0c51c0',
          x: 150,
          y: -20,
          width: 20,
          height: 20
        }
      },
      markup: [
        { tagName: 'rect', selector: 'body' },
        { tagName: 'rect', selector: 'borderLeft' },
        { tagName: 'image', selector: 'logo' },
        { tagName: 'image', selector: 'deleteTools' },
        { tagName: 'text', selector: 'label' }
      ]
    },
    true
  );
}

// 创建节点的函数，现在接受一个额外的 type 参数
export function createNodeWithBorderPorts(label, type) {
  const Groups = {
    groupLeft: { position: 'left' },
    groupRight: { position: 'right' },
    groupTop: { position: 'top' },
    groupBottom: { position: 'bottom' }
  };
  const portsAttrs = {
    circle: {
      r: 3,
      magnet: true,
      stroke: 'transparent',
      strokeWidth: 2,
      fill: 'transparent'
    }
  };

  return {
    shape: 'algo-node',
    ports: {
      groups: Groups,
      items: [
        { group: 'groupLeft', attrs: portsAttrs },
        { group: 'groupRight', attrs: portsAttrs },
        { group: 'groupTop', attrs: portsAttrs },
        { group: 'groupBottom', attrs: portsAttrs }
      ]
    },
    data: {
      label: label,
      id: null,
      type: type // 添加类型属性
    },
    attrs: {
      label: {
        text: label
      },
      logo: {
        'xlink:href': Images[type]
      }
    }
  };
}

