"use strict";var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.FileUploaderControlProvider = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));

var _useMemoObject = require("../../hooks/useMemoObject");
var _useEffectWithoutInitCall = require("../../hooks/useEffectWithoutInitCall");


var _fileUtils = require("./fileUtils");
var _FileUploaderControlContext = require("./FileUploaderControlContext");
var _useControlLocale = require("./hooks/useControlLocale");
var _FileUploaderFileValidationResult = require("./FileUploaderFileValidationResult");












var updateFile = function updateFile(
files,
fileId,
getFileUpdatedProps)
{
  var fileIndex = files.findIndex(function (file) {return file.id === fileId;});
  if (fileIndex === -1) {
    return files;
  }

  var newFiles = [].concat(files);
  var file = files[fileIndex];

  var updatedProps = getFileUpdatedProps(file);

  newFiles[fileIndex] = (0, _extends2.default)({},
  file,
  updatedProps);


  return newFiles;
};

var FileUploaderControlProvider = exports.FileUploaderControlProvider = function FileUploaderControlProvider(
props)




{
  var initialFiles = props.initialFiles,multiple = props.multiple,children = props.children,onValueChange = props.onValueChange,onRemove = props.onRemove,onAttach = props.onAttach;

  var _useState = (0, _react.useState)(function () {
      if (initialFiles && initialFiles.length > 0) {
        var attachedFiles = initialFiles.map(function (item) {return (0, _fileUtils.getAttachedFile)(item);});

        return multiple ? attachedFiles : [attachedFiles[0]];
      }
      return [];
    }),files = _useState[0],setFiles = _useState[1];
  var _useState2 = (0, _react.useState)(false),isMinLengthReached = _useState2[0],setIsMinLengthReached = _useState2[1];
  var locale = (0, _useControlLocale.useControlLocale)();

  (0, _useEffectWithoutInitCall.useEffectWithoutInitCall)(function () {
    onValueChange == null || onValueChange(files);
  }, [files]);

  var setFileStatus = (0, _react.useCallback)(
    function (fileId, status) {
      setFiles(function (files) {return (
          updateFile(files, fileId, function (file) {return {
              status: status,
              validationResult:
              status === _fileUtils.FileUploaderFileStatus.Error || status === _fileUtils.FileUploaderFileStatus.Warning ?
              _FileUploaderFileValidationResult.FileUploaderFileValidationResult.error(locale.requestErrorText) :
              file.validationResult
            };}));}
      );
    },
    [locale]
  );

  var handleExternalSetFiles = (0, _react.useCallback)(
    function (files) {
      onAttach == null || onAttach(files);
      setFiles(function (state) {return [].concat(state, files);});
    },
    [onAttach]
  );

  var removeFile = (0, _react.useCallback)(
    function (fileId) {
      onRemove == null || onRemove(fileId);
      setFiles(function (state) {return state.filter(function (file) {return file.id !== fileId;});});
    },
    [onRemove]
  );

  var setFileValidationResult = (0, _react.useCallback)(
    function (fileId, validationResult, status) {
      setFiles(function (files) {return updateFile(files, fileId, function () {return { validationResult: validationResult, status: status };});});
    },
    []
  );

  var reset = _react.default.useCallback(function () {
    setFiles(function () {return [];});
  }, []);

  return /*#__PURE__*/(
    _react.default.createElement(_FileUploaderControlContext.FileUploaderControlContext.Provider, {
      value: (0, _useMemoObject.useMemoObject)({
        setFileStatus: setFileStatus,
        files: files,
        setFiles: handleExternalSetFiles,
        removeFile: removeFile,
        setFileValidationResult: setFileValidationResult,
        reset: reset,
        isMinLengthReached: isMinLengthReached,
        setIsMinLengthReached: setIsMinLengthReached
      }) },

    children
    ));

};

FileUploaderControlProvider.displayName = 'FileUploaderControlProvider';