import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { Node } from "@antv/x6";
import CustomInput from "@/components/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 || { width: 0, height: 0 };

  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 ${w} ${h}`}
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>{defsContent}</defs>
          <path
            d={`
              M ${Math.min(w*0.2,18)},${strokeWidth}
              Q ${Math.min(w*0.1,9)},${strokeWidth} ${Math.min(w*0.1,9)},${Math.min(h*0.1,9)}
              L ${Math.min(w*0.1,9)},${h*0.5-Math.min(h*0.1,9)}
              Q ${Math.min(w*0.1,9)},${h/2} ${strokeWidth},${h/2}
              Q ${Math.min(w*0.1,9)},${h/2} ${Math.min(w*0.1,9)},${h*0.5+Math.min(h*0.1,9)}
              L ${Math.min(w*0.1,9)},${h-Math.min(h*0.1,9)}
              Q ${Math.min(w*0.1,9)},${h} ${Math.min(w*0.2,18)},${h - strokeWidth}
              M ${w-Math.min(w*0.2,18)},${strokeWidth}
              Q ${w-Math.min(w*0.1,9)},${strokeWidth} ${w-Math.min(w*0.1,9)},${Math.min(h*0.1,9)}
              L ${w-Math.min(w*0.1,9)},${h*0.5-Math.min(h*0.1,9)}
              Q ${w-Math.min(w*0.1,9)},${h/2} ${w-strokeWidth},${h/2}
              Q ${w-Math.min(w*0.1,9)},${h/2} ${w-Math.min(w*0.1,9)},${h*0.5+Math.min(h*0.1,9)}
              L ${w-Math.min(w*0.1,9)},${h-Math.min(h*0.1,9)}
              Q ${w-Math.min(w*0.1,9)},${h} ${w-Math.min(w*0.2,18)},${h - strokeWidth}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

register({
  shape: "custom-react-sequenceConstraint",
  width: 170,
  height: 50,
  effect: ["data"],
  component: component,
});

const sequenceConstraint: CompoundedComponent = {
  name: "约束",
  icon: require("./image/sequenceConstraint.png"),
  node: {
    shape: "custom-react-sequenceConstraint",
    data: {
      label: "",
    },
  },
};

export default sequenceConstraint;
