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);

  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>
          <rect
            x={strokeWidth}
            y={strokeWidth}
            width={size?.width - strokeWidth * 2}
            height={size?.height - strokeWidth * 2}
            rx={4}
            ry={4}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const simpleInterface: CompoundedComponent = {
  name: "简单接口",
  icon: require("./image/simpleInterface.png"),
  node: {
    shape: "custom-react-simpleInterface",
    data: {
      label: "接口名称",
      text: {
        bold: true,
      },
    },
  },
};

export default simpleInterface;
