
import { register } from "@antv/x6-react-shape";
import { Node } from "@antv/x6";
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"
        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>
          <polygon
            points={`
              ${strokeWidth},${strokeWidth}
              ${w - strokeWidth},${strokeWidth}
              ${w-Math.min(w/6,30)},${Math.min(h/6,30)}
              ${Math.min(w/6,30)},${Math.min(h/6,30)}
              ${Math.min(w/6,30)},${h - Math.min(h/6,30)}
              ${strokeWidth},${h - strokeWidth}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const corner = {
  name: "拐角",
  icon: require("./image/corner.png"),
  node: {
    shape: "custom-react-corner",
    data: {
      label: "",
    },
  },
};

export default corner;
