import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { Graph, Node } from "@antv/x6";
import { ports, defaultData } from "../../config/data";
import CustomInput from "../CustomInput";
import { useSizeHook, useShapeProps } from "@/hooks";
const component = ({ node }: { node: Node }) => {
  const { label, text, fill, stroke, opacity } = node.getData();
  const { size, ref } = useSizeHook();
  const {
    fillContent,
    defsContent,
    strokeColor,
    strokeWidth,
    strokeDasharray,
  } = useShapeProps(fill, size, stroke);

  return (
    <>
      <div
        className="relative text-0 w-full h-full"
        ref={ref}
        style={{ opacity: opacity / 100 }}
      >
        <CustomInput value={label} styles={text} node={node} />
        <svg
          className="w-full h-full"
          viewBox={`0 0 ${size?.width} ${size?.height}`}
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>{defsContent}</defs>
          <path
            d={`
              M ${strokeWidth},${size.height * 0.25}
              L ${size.width - strokeWidth},${size.height*0.25}
              L ${size.width / 2},${size.height*0.75} Z
              M ${size.width / 2},${size.height*0.25}
              L ${size.width / 2},${0}
              M ${size.width / 2},${size.height*0.75}
              L ${size.width / 2},${size.height}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

register({
  shape: "custom-react-generalization",
  width: 70,
  height: 76,
  effect: ["data"],
  component: component,
});

const generalization: CompoundedComponent = {
  name: "概化",
  icon: require("./image/generalization.png"),
  node: {
    shape: "custom-react-generalization",
    data: {
      label: "",
      ...defaultData,
    },
    ports: {
      ...ports,
      groups: {
        ...ports.groups,
      },
      items: [
        {
          group: "top",
          args: {
            dy: 2,
          },
        },
        {
          group: "bottom",
          args: {
            dy: -2,
          },
        },
      ],
    },
  },
};

export default generalization;
