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

export function registerNodeDeleteTool(proxy) {
  Graph.unregisterNodeTool('delete-node-tool'); // 注销原工具

  // 注册一个自定义的节点工具
  Graph.registerNodeTool('delete-node-tool', {
    inherit: 'button', //自定义工具，一定要继承基类，否则位置调整极为复杂
    markup: [
      {
        tagName: 'rect',
        selector: 'toolRect', // 确保有一个矩形作为背景
        attrs: {
          width: 18,
          height: 18,
          rx: 2,
          ry: 2,
          fill: '#ffffff',
          stroke: '#265fa6',
          'stroke-width': 2,
          cursor: 'pointer',
          x: 150,
          y: -20
        }
      },
      {
        tagName: 'image',
        selector: 'delete_node_icon',
        attrs: {
          'xlink:href': Images.deleteIcon,
          width: 16,
          height: 16,
          x: 151,
          y: -20,
          cursor: 'pointer'
        }
      }
    ],
    onClick: async ({ view }) => {
      try {
        const shouldDelete = await proxy?.$modal.confirm('确定要删除节点 ' + (view.cell.attrs.label?.text || '') + '?');
        if (shouldDelete) {
          view.cell.remove();
        }
      } catch (error) {
        if (error === 'cancel') {
          // 用户取消了删除操作，不做任何处理
        } else {
          // 处理其他可能的错误
        }
      }
    }
  });
}

