import _extends from "@babel/runtime/helpers/esm/extends";
import React, { useCallback, useState } from 'react';
import { useMemoObject } from "../../../hooks/useMemoObject";
import { useEffectWithoutInitCall } from "../../../hooks/useEffectWithoutInitCall";
import { FileUploaderFileStatus, getAttachedFile } from "../fileUtils";
import { FileUploaderControlContext } from "../FileUploaderControlContext";
import { useControlLocale } from "../hooks/useControlLocale";
import { FileUploaderFileValidationResult } from "../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] = _extends({}, file, updatedProps);
  return newFiles;
};
export var 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 = useState(function () {
      if (initialFiles && initialFiles.length > 0) {
        var attachedFiles = initialFiles.map(function (item) {
          return getAttachedFile(item);
        });
        return multiple ? attachedFiles : [attachedFiles[0]];
      }
      return [];
    }),
    files = _useState[0],
    setFiles = _useState[1];
  var _useState2 = useState(false),
    isMinLengthReached = _useState2[0],
    setIsMinLengthReached = _useState2[1];
  var locale = useControlLocale();
  useEffectWithoutInitCall(function () {
    onValueChange == null || 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(files);
    setFiles(function (state) {
      return [].concat(state, files);
    });
  }, [onAttach]);
  var removeFile = useCallback(function (fileId) {
    onRemove == null || 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 /*#__PURE__*/(
    React.createElement(FileUploaderControlContext.Provider, {
      value: useMemoObject({
        setFileStatus: setFileStatus,
        files: files,
        setFiles: handleExternalSetFiles,
        removeFile: removeFile,
        setFileValidationResult: setFileValidationResult,
        reset: reset,
        isMinLengthReached: isMinLengthReached,
        setIsMinLengthReached: setIsMinLengthReached
      })
    }, children)
  );
};
FileUploaderControlProvider.displayName = 'FileUploaderControlProvider';