"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.useUpload = void 0;var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var _react = require("react");
var _reactDom = _interopRequireDefault(require("react-dom"));



var _fileUtils = require("../fileUtils");
var _FileUploaderControlContext = require("../FileUploaderControlContext");

var useUpload = exports.useUpload = function useUpload(
request,
onRequestSuccess,
onRequestError)
{
  var _useContext = (0, _react.useContext)(_FileUploaderControlContext.FileUploaderControlContext),setFileStatus = _useContext.setFileStatus;

  var switchToLoading = (0, _react.useCallback)(
    function (fileId) {
      setFileStatus(fileId, _fileUtils.FileUploaderFileStatus.Loading);
    },
    [setFileStatus]
  );

  var switchToSuccess = (0, _react.useCallback)(
    function (fileId) {
      setFileStatus(fileId, _fileUtils.FileUploaderFileStatus.Uploaded);
      onRequestSuccess == null || onRequestSuccess(fileId);
    },
    [setFileStatus, onRequestSuccess]
  );

  var switchToError = (0, _react.useCallback)(
    function (fileId) {
      setFileStatus(fileId, _fileUtils.FileUploaderFileStatus.Error);
      onRequestError == null || onRequestError(fileId);
    },
    [setFileStatus, onRequestError]
  );

  return (0, _react.useCallback)( /*#__PURE__*/function () {var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(
      function _callee(file) {var id;return _regenerator.default.wrap(function _callee$(_context) {while (1) switch (_context.prev = _context.next) {case 0:
              id = file.id;
              _reactDom.default.flushSync(function () {
                switchToLoading(id);
              });_context.prev = 2;_context.next = 5;return (

                request == null ? void 0 : request(file));case 5:
              switchToSuccess(id);_context.next = 11;break;case 8:_context.prev = 8;_context.t0 = _context["catch"](2);

              switchToError(id);case 11:case "end":return _context.stop();}}, _callee, null, [[2, 8]]);}));return function (_x) {return _ref.apply(this, arguments);};}(),


  [request, switchToSuccess, switchToLoading, switchToError]
  );
};