import { Node, Graph } from "@antv/x6";
import { MenuProps } from "antd";
import { EditObject, SaveNode } from "@/api/node";
import { NodeType } from "@/enum";

const handleCopy = (node: Node, graph: Graph) => {
  const data = node.getData();
  const position = node.position();
  const width = node.size().width;
  const height = node.size().height;
  const newNode = graph.addNode({
    shape: node.shape,
    position: {
      x: position.x + 20,
      y: position.y + 20,
    },
    data: {
      ...data,
      x: position.x + 20,
      y: position.y + 20,
      id: `${data.id}_copy`,
    },
    width,
    height,
  });
  newNode.setData({
    id: newNode.id,
  });

  if (data.type === NodeType.TEXT) {
    EditObject(newNode.getData());
  } else {
    SaveNode(newNode.getData());
  }
};

export const getDropDownItems = (
  node: Node,
  graph: Graph
): MenuProps["items"] => {
  return [
    { key: "copy", label: "复制", onClick: () => handleCopy(node, graph) },
    { key: "delete", label: "删除", onClick: () => node.remove() },
  ];
};
