/**
 * This will first check if the mime-type of the file starts with `text/` and
 * fallback to checking a few file names or extensions that should be considered
 * text.
 *
 * This function is not guaranteed to be 100% correct and is only useful if
 * trying to generate a preview of files uploaded to the browser.
 *
 * @param file - The file to check
 * @returns `true` if the file should be considered as a text-content file.
 * @since 2.9.0
 */ export function isTextFile(file) {
    return /\.((j|t)sx?|json|lock|hbs|ya?ml|log|txt|md)$/i.test(file.name);
}
/**
 * This will first check if the mime-type of the file starts with `text\/` and
 * fallback to checking a few file names or extensions that should be considered
 * text.
 *
 * This function is not guaranteed to be 100% correct and is only useful if
 * trying to generate a preview of files uploaded to the browser.
 *
 * @param file - The file to check
 * @returns `true` if the file should be considered as a text content file.
 * @since 2.9.0
 */ export function isImageFile(file) {
    return /\.(a?png|avif|svg|tiff|gifv?|jpe?g)/i.test(file.name);
}
/**
 * This will first check if the mime-type of the file starts with `audio/` and
 * fallback to checking a few file names or extensions that should be considered
 * audio.
 *
 * This function is not guaranteed to be 100% correct and is only useful if
 * trying to generate a preview of files uploaded to the browser.
 *
 * @param file - The file to check
 * @returns `true` if the file should be considered as a audio content file.
 * @since 2.9.0
 */ export function isAudioFile(file) {
    return /\.(mp3|wav|ogg|m4p|flac)$/i.test(file.name);
}
/**
 * This will first check if the mime-type of the file starts with `video/` and
 * fallback to checking a few file names or extensions that should be considered
 * video.
 *
 * This function is not guaranteed to be 100% correct and is only useful if
 * trying to generate a preview of files uploaded to the browser.
 *
 * @param file - The file to check
 * @returns `true` if the file should be considered as a video content file.
 * @since 2.9.0
 */ export function isVideoFile(file) {
    return /\.(mkv|mpe?g|mov|avi|flv|webm|mp4)$/i.test(file.name);
}
/**
 * This function is not guaranteed to be 100% correct and is only useful if
 * trying to generate a preview of files uploaded to the browser.
 *
 * @param file - The file to check
 * @returns `true` if the file matches an image, audio, or video file.
 * @since 2.9.0
 */ export function isMediaFile(file) {
    return isImageFile(file) || isAudioFile(file) || isVideoFile(file);
}
/**
 * This function will attempt to read:
 * - media (image, audio, and video) files as a data url so they can be
 *   previewed in `<img>`, `<audio>`, and `<video>` tags
 * - text files as plain text
 * - everything else as an `ArrayBuffer` which can be manually converted into a
 *   data url if needed with `URL.createObjectURL`
 *
 * @since 2.9.0
 */ export const getFileParser = (file)=>{
    if (isMediaFile(file)) {
        return "readAsDataURL";
    }
    if (isTextFile(file)) {
        return "readAsText";
    }
    return "readAsArrayBuffer";
};
/**
 * This util will split all the current upload stats by status.
 *
 * @example Main Usage
 * ```tsx
 * import { FileUpload } from "@react-md/core/files/FileUpload";
 * import { useFileUpload } from "@react-md/core/files/useFileUpload";
 * import { getSplitFileUploads } from "@react-md/core/files/utils";
 *
 * function Example() {
 *   const { stats, errors, accept, onChange } = useFileUpload();
 *   const { pending, uploading, completed } = getSplitFileUploads(stats);
 *
 *   return (
 *     <>
 *       <FileUpload accept={accept} onChange={onChange} />
 *       {pending.map(({ key, file, progress, status }) => {
 *         // pretend some UI for each pending item with the provided data
 *         return null;
 *       })}
 *       {uploading.map(({ file, key, progress, status }) => {
 *         // pretend some UI for each uploading item with the provided data
 *         return null;
 *       })}
 *       {complete.map(({ file, key, progress, result, status }) => {
 *         // pretend some UI for each complete item with the provided data
 *         return null;
 *       })}
 *     </>
 *   );
 * }
 * ```
 *
 * @param stats - The {@link FileUploadStats} list generally returned by the
 * {@link useFileUpload} hook.
 * @returns the {@link SplitFileUploads}.
 * @since 2.9.0
 */ export function getSplitFileUploads(stats) {
    const pending = [];
    const uploading = [];
    const complete = [];
    stats.forEach((stat)=>{
        if (stat.status === "pending") {
            pending.push(stat);
        } else if (stat.status === "uploading") {
            uploading.push(stat);
        } else if (stat.status === "complete") {
            complete.push(stat);
        } else {
            /* istanbul ignore next */ throw new Error("Invalid upload stat");
        }
    });
    return {
        pending,
        uploading,
        complete
    };
}

//# sourceMappingURL=utils.js.map