import {
  type DropEvent,
  type DropzoneProps,
  useDropzone,
} from 'react-dropzone';

import { megabytesToBytes } from '../../../../utils/bytes';

export type UseHandleDropzoneProps = {
  onDropAccepted: (file: File, event: DropEvent) => void;
  maxSize?: number;
  disabled?: boolean;
} & Pick<DropzoneProps, 'onDropRejected'>;

const useHandleDropzone = ({
  onDropAccepted = () => null,
  onDropRejected = () => null,
  maxSize = megabytesToBytes(50),
  disabled = false,
}: UseHandleDropzoneProps) => {
  const handleDropAccepted = (files: File[], event: DropEvent) => {
    onDropAccepted(files[0], event);
  };

  return useDropzone({
    onDropAccepted: handleDropAccepted,
    onDropRejected,
    accept: { 'image/png': [], 'image/jpeg': [], 'image/webp': [] },
    maxFiles: 1,
    maxSize,
    disabled,
  });
};

export default useHandleDropzone;
