import { useRef } from "react";
import { useMergeRefs } from "../../../utils/hooks/index.js";
import { validateFiles } from "../helpers/validate-files.js";
export const useFileUpload = ({ ref, accept, onSelect, validator, maxSizeInBytes, disabled, }) => {
    const inputRef = useRef(null);
    const mergedRef = useMergeRefs(inputRef, ref);
    const upload = (fileList) => {
        const { files, partitionedFiles } = validateFiles(Array.from(fileList), accept, validator, maxSizeInBytes);
        onSelect(files, partitionedFiles);
    };
    const onChange = (event) => {
        const fileList = event.target.files;
        if (!fileList) {
            return;
        }
        if (!disabled) {
            upload(fileList);
        }
        // Resets the value to make it is possible to upload the same file several consecutive times
        event.target.value = "";
    };
    return {
        upload,
        onChange,
        inputRef,
        mergedRef,
    };
};
//# sourceMappingURL=useFileUpload.js.map