/* eslint-disable no-unused-expressions */
import accepts from 'attr-accept';
import { curry, filter, flip, gt, gte, isEmpty, lt, lte, map, not, pipe, prop, sum } from 'ramda';
const verifiedFileType = curry(flip(accepts));
const invalidFileType = curry(pipe(verifiedFileType, not));
const getSize = prop('size');
const totalSize = pipe(map(getSize), sum);
const biggerItems = curry((maxItemSize, file) => lt(maxItemSize, getSize(file)));
const smallerItems = curry((minItemSize, file) => gt(minItemSize, getSize(file)));
export const validFileTypes = curry((accept, files, onError) => {
    const badFiles = filter(invalidFileType(accept), [...files]);
    if (isEmpty(badFiles))
        return true;
    onError && onError({ message: 'Bad file type', files: badFiles });
    return false;
});
export const validMaxTotalSize = curry((maxTotalSize, files, onError) => {
    const totalFileSize = totalSize(files);
    if (gte(maxTotalSize, totalFileSize))
        return true;
    onError && onError({ message: 'Total size exceeded', totalFileSize });
    return false;
});
export const validMaxItemSize = curry((maxItemSize, files, onError) => {
    const badFiles = filter(biggerItems(maxItemSize), Array.from(files));
    if (isEmpty(badFiles))
        return true;
    onError && onError({ message: 'File size exceeded', files: badFiles });
    return false;
});
export const validMinTotalSize = curry((minTotalSize, files, onError) => {
    const totalFileSize = totalSize(files);
    if (lte(minTotalSize, totalFileSize))
        return true;
    onError && onError({ message: 'Total size too small', totalFileSize });
    return false;
});
export const validMinItemSize = curry((minItemSize, files, onError) => {
    const badFiles = filter(smallerItems(minItemSize), Array.from(files));
    if (isEmpty(badFiles))
        return true;
    onError && onError({ message: 'File size too small', files: badFiles });
    return false;
});
export default { validFileTypes, validMaxItemSize, validMaxTotalSize, validMinItemSize, validMinTotalSize };
//# sourceMappingURL=uploadButtonValidators.js.map