import { FileCollection, FileMimeType } from '@data-wrangling-components/utilities';
import { useCallback } from 'react';
import { useDropzone as UseDz } from 'react-dropzone';
export function useDropzone({ acceptedFileTypes , onDrop , onDropRejected , onDropAccepted , dropzoneOptions  }) {
    const handleOnDrop = useHandleOnDrop(onDrop);
    const handleOnDropAccepted = useHandleOnDropAccepted(onDropAccepted);
    const acceptedFileTypesExt = acceptedFileTypes.map((x)=>x.toLowerCase().includes('application') || x.toLowerCase().includes('text') ? FileMimeType[x] : x);
    const handleOnDropRejected = useHandleOnDropRejected(acceptedFileTypesExt, onDropRejected);
    const state = UseDz({
        onDrop (files) {
            void handleOnDrop(files);
        },
        onDropAccepted (files) {
            void handleOnDropAccepted(files);
        },
        onDropRejected: handleOnDropRejected,
        accept: acceptedFileTypes.toString(),
        ...dropzoneOptions
    });
    return {
        ...state,
        acceptedFileTypesExt
    };
}
function useHandleOnDropAccepted(onDropAccepted) {
    return useCallback(async (files)=>{
        const fileCollection = new FileCollection();
        try {
            await fileCollection.add(files);
            onDropAccepted?.(fileCollection);
        } catch (e) {
            console.error(e);
        }
    }, [
        onDropAccepted
    ]);
}
function useHandleOnDropRejected(acceptedFileTypes, onDropRejected) {
    return useCallback((files)=>{
        const message = `File type must be: ${acceptedFileTypes.join(', ')}`;
        onDropRejected?.(message, files);
    }, [
        onDropRejected,
        acceptedFileTypes
    ]);
}
function useHandleOnDrop(onDrop) {
    return useCallback(async (files)=>{
        const fileCollection = new FileCollection();
        try {
            await fileCollection.add(files);
            onDrop?.(fileCollection);
        } catch (e) {
            console.error(e);
        }
    }, [
        onDrop
    ]);
}
