"use client";

import ProfilePictureCropModal from "./ProfilePictureCropModal";

/**
 * The crop dialog, and the file-input reset that has to go with cancelling it.
 *
 * Clearing every `input[type=file]` on cancel is not incidental: without it,
 * re-picking the **same** file fires no `change` event, so a volunteer who
 * cancels and changes their mind appears to have a dead button.
 */
export default function ProfilePictureCropGate({
  showCropModal,
  selectedImage,
  onCropComplete,
  setShowCropModal,
  setSelectedImage,
}: {
  showCropModal: boolean;
  selectedImage: string | null;
  onCropComplete: (croppedFile: File) => Promise<void> | void;
  setShowCropModal: (open: boolean) => void;
  setSelectedImage: (image: string | null) => void;
}) {
  return (
    <>
      {/* Render Crop Modal when image is selected */}
      {showCropModal && selectedImage && (
        <ProfilePictureCropModal
          imageSrc={selectedImage}
          onCropComplete={onCropComplete}
          onCancel={() => {
            setShowCropModal(false);
            setSelectedImage(null);

            // Clear the file input so re-picking the same file still fires change
            document
              .querySelectorAll('input[type="file"]')
              .forEach((input) => {
                (input as HTMLInputElement).value = "";
              });
          }}
        />
      )}
    </>
  );
}
