import { Transformer } from "react-konva";
import Konva from "konva";
import type { EditorObject } from "../../store/editorStore";

interface TransformerControlsProps {
    transformerRef: React.RefObject<Konva.Transformer | null>;
    selectedObject: EditorObject | null;
    isGroupedWithMask: boolean;
}

const TransformerControls = ({ transformerRef, selectedObject, isGroupedWithMask }: TransformerControlsProps) => {
    if (!selectedObject) return null;

    const isText = selectedObject.type === "text";
    const isGroup = selectedObject.type === "group";

    // اگر با mask گروه شده، امکان ریسایز نباشد
    const enabledAnchors = isGroupedWithMask
        ? []
        : [
            "top-left",
            "top-right",
            "bottom-left",
            "bottom-right",
            "top-center",
            "bottom-center",
            "middle-left",
            "middle-right",
        ];

    return (
        <Transformer
            ref={transformerRef}
            boundBoxFunc={(oldBox, newBox) => {
                if (Math.abs(newBox.width) < 5 || Math.abs(newBox.height) < 5) {
                    return oldBox;
                }
                // اگر با mask گروه شده، هیچ تغییر سایزی مجاز نیست
                if (isGroupedWithMask) {
                    return oldBox;
                }
                return newBox;
            }}
            ignoreStroke={isText}
            perfectDrawEnabled={false}
            anchorFill="#3b82f6"
            anchorStroke="#ffffff"
            borderStroke="#3b82f6"
            borderStrokeWidth={1}
            padding={-2}
            anchorSize={8}
            rotateEnabled={!isGroup && !isGroupedWithMask}
            enabledAnchors={enabledAnchors}
        />
    );
};

export default TransformerControls;

