var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useCallback, useState } from 'react';
import { useMemoObject } from '../../hooks/useMemoObject.js';
import { useEffectWithoutInitCall } from '../../hooks/useEffectWithoutInitCall.js';
import { FileUploaderFileStatus, getAttachedFile } from './fileUtils.js';
import { FileUploaderControlContext } from './FileUploaderControlContext.js';
import { useControlLocale } from './hooks/useControlLocale.js';
import { FileUploaderFileValidationResult } from './FileUploaderFileValidationResult.js';
var updateFile = function (files, fileId, getFileUpdatedProps) {
    var fileIndex = files.findIndex(function (file) { return file.id === fileId; });
    if (fileIndex === -1) {
        return files;
    }
    var newFiles = __spreadArray([], files, true);
    var file = files[fileIndex];
    var updatedProps = getFileUpdatedProps(file);
    newFiles[fileIndex] = __assign(__assign({}, file), updatedProps);
    return newFiles;
};
export var FileUploaderControlProvider = React.memo(function (props) {
    var initialFiles = props.initialFiles, multiple = props.multiple, children = props.children, onValueChange = props.onValueChange, onRemove = props.onRemove, onAttach = props.onAttach;
    var _a = useState(function () {
        if (initialFiles && initialFiles.length > 0) {
            var attachedFiles = initialFiles.map(function (item) { return getAttachedFile(item); });
            return multiple ? attachedFiles : [attachedFiles[0]];
        }
        return [];
    }), files = _a[0], setFiles = _a[1];
    var locale = useControlLocale();
    useEffectWithoutInitCall(function () {
        onValueChange === null || onValueChange === void 0 ? void 0 : onValueChange(files);
    }, [files]);
    var setFileStatus = useCallback(function (fileId, status) {
        setFiles(function (files) {
            return updateFile(files, fileId, function (file) { return ({
                status: status,
                validationResult: status === FileUploaderFileStatus.Error || status === FileUploaderFileStatus.Warning
                    ? FileUploaderFileValidationResult.error(locale.requestErrorText)
                    : file.validationResult,
            }); });
        });
    }, [locale]);
    var handleExternalSetFiles = useCallback(function (files) {
        onAttach === null || onAttach === void 0 ? void 0 : onAttach(files);
        setFiles(function (state) { return __spreadArray(__spreadArray([], state, true), files, true); });
    }, [onAttach]);
    var removeFile = useCallback(function (fileId) {
        onRemove === null || onRemove === void 0 ? void 0 : onRemove(fileId);
        setFiles(function (state) { return state.filter(function (file) { return file.id !== fileId; }); });
    }, [onRemove]);
    var setFileValidationResult = useCallback(function (fileId, validationResult, status) {
        setFiles(function (files) { return updateFile(files, fileId, function () { return ({ validationResult: validationResult, status: status }); }); });
    }, []);
    var reset = React.useCallback(function () {
        setFiles(function () { return []; });
    }, []);
    return (React.createElement(FileUploaderControlContext.Provider, { value: useMemoObject({
            setFileStatus: setFileStatus,
            files: files,
            setFiles: handleExternalSetFiles,
            removeFile: removeFile,
            setFileValidationResult: setFileValidationResult,
            reset: reset,
        }) }, children));
});
FileUploaderControlProvider.displayName = 'FileUploaderControlProvider';
//# sourceMappingURL=FileUploaderControlProvider.js.map