import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { 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);
  const { width: w, height: h } = size;
  return (
    <>
      <div
        className="relative text-0 w-full h-full"
        style={{ opacity: opacity / 100 }}
        ref={ref}
      >
        <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 ${w-strokeWidth},${strokeWidth}
              Q ${Math.min(w-w*0.1,w-9)},${strokeWidth} ${Math.min(w-w*0.1,w-9)},${Math.min(h*0.1,9)}
              L ${Math.min(w-w*0.1,w-9)},${h*0.5-Math.min(h*0.1,9)}
              Q ${Math.min(w-w*0.1,w-9)},${h/2} ${w-22},${h/2}
              Q ${Math.min(w-w*0.1,w-9)},${h/2} ${Math.min(w-w*0.1,w-9)},${h*0.5+Math.min(h*0.1,9)}
              L ${Math.min(w-w*0.1,w-9)},${h-Math.min(h*0.1,9)}
              Q ${Math.min(w-w*0.1,w-9)},${h} ${w-strokeWidth},${h - strokeWidth}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

register({
  shape: "custom-react-leftBraceFlow",
  width: 120,
  height: 140,
  effect: ["data"],
  component: component,
});

const leftBraceFlow: CompoundedComponent = {
  name: "备注",
  icon: require("./image/leftBraceFlow.png"),
  node: {
    shape: "custom-react-leftBraceFlow",
    data: {
      label: "",
      ...defaultData,
    },
    ports: {
      ...ports,
      items: [
        {
          group: 'left',
          args: {
            dx: 2,
          },
        },
      ]
    }
  },
};

export default leftBraceFlow;
