import CropImageModal from "@/components/CropImageModal";

type ProductCropModalProps = {
    isOpen: boolean;
    cropFile: File | null;
    cropImageId: number | string | null;
    productId: string | number;
    selectedImage: any;
    selectedVariation: any;
    selectedCropSize: {
        width: number;
        height: number;
    } | null;
    isRegularPhotos: boolean;
    previewAspect: number;
    setIsCropOpen: (value: boolean) => void;
    setCropFile: (value: File | null) => void;
    setCropImageId: (value: number | string | null) => void;
    setCropSourceMeta: (value: { width: number; height: number } | null) => void;
    onSaved: (data: any) => void;
};

export default function ProductCropModal({
                                             isOpen,
                                             cropFile,
                                             cropImageId,
                                             productId,
                                             selectedImage,
                                             selectedVariation,
                                             selectedCropSize,
                                             isRegularPhotos,
                                             previewAspect,
                                             setIsCropOpen,
                                             setCropFile,
                                             setCropImageId,
                                             setCropSourceMeta,
                                             onSaved,
                                         }: ProductCropModalProps) {
    const closeModal = () => {
        setIsCropOpen(false);
        setCropFile(null);
        setCropImageId(null);
        setCropSourceMeta(null);
    };

    return (
        <CropImageModal
            isOpen={isOpen}
            file={cropFile}
            imageId={cropImageId}
            productId={productId}
            variationId={selectedVariation?.id}
            selectedImage={selectedImage}
            calendarSlot={selectedImage?.calendar_slot}
            aspect={
                selectedCropSize
                    ? selectedCropSize.width / selectedCropSize.height
                    : isRegularPhotos
                        ? previewAspect
                        : 1
            }
            variationSize={
                selectedCropSize
                    ? {
                        width: selectedCropSize.width,
                        height: selectedCropSize.height,
                    }
                    : isRegularPhotos
                        ? {
                            width: Number(selectedVariation?.width || 0),
                            height: Number(selectedVariation?.height || 0),
                        }
                        : null
            }
            isRegularPhotos={isRegularPhotos}
            cropShape="rect"
            title="قص الصورة"
            onCancel={closeModal}
            onClose={closeModal}
            onSaved={onSaved}
        />
    );
}