"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _button = _interopRequireDefault(require("../button"));

var _icon = _interopRequireDefault(require("../icon"));

var _components = require("../components");

var _context = require("../config-provider/context");

var _utils = require("./utils");

var _fileList = _interopRequireDefault(require("./fileList"));

var UploadLine = _icon.default.UploadLine,
    PlusLine = _icon.default.PlusLine,
    ContainerLine = _icon.default.ContainerLine;

var Upload = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var children = props.children,
      listType = props.listType,
      onChange = props.onChange,
      onRemove = props.onRemove,
      beforeUpload = props.beforeUpload,
      customPrefixCls = props.prefixCls,
      disabled = props.disabled,
      isOnGrayBg = props.isOnGrayBg,
      tips = props.tips,
      defaultFileList = props.defaultFileList,
      outerFileList = props.fileList,
      className = props.className,
      showUploadList = props.showUploadList,
      maxCount = props.maxCount,
      itemRender = props.itemRender,
      fileItemComponent = props.fileItemComponent,
      preview = props.preview,
      deletable = props.deletable,
      text = props.text,
      fit = props.fit,
      onPreview = props.onPreview,
      customOnClick = props.customOnClick,
      resetProps = (0, _objectWithoutProperties2.default)(props, ["children", "listType", "onChange", "onRemove", "beforeUpload", "prefixCls", "disabled", "isOnGrayBg", "tips", "defaultFileList", "fileList", "className", "showUploadList", "maxCount", "itemRender", "fileItemComponent", "preview", "deletable", "text", "fit", "onPreview", "customOnClick"]);

  var _React$useState = _react.default.useState(defaultFileList || outerFileList || []),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      fileList = _React$useState2[0],
      setFileList = _React$useState2[1];

  var RcUploadRef = ref || _react.default.useRef();

  var ReplaceFileRef = _react.default.useRef();

  var currentUploadTypeRef = _react.default.useRef('upload');

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var uploadPrefixCls = getPrefixClassName('upload', customPrefixCls);

  if ('fileList' in props && outerFileList !== fileList) {
    setFileList(outerFileList);
  }

  var handleChange = function handleChange(info) {
    if (!('fileList' in props)) {
      setFileList((0, _toConsumableArray2.default)(info.fileList));
    }

    onChange === null || onChange === void 0 ? void 0 : onChange((0, _objectSpread2.default)((0, _objectSpread2.default)({}, info), {}, {
      fileList: (0, _toConsumableArray2.default)(info.fileList)
    }));
  };

  var onFileReplace = function onFileReplace(file, index) {
    currentUploadTypeRef.current = 'replace';
    ReplaceFileRef.current = {
      file: file,
      index: index
    };
  };

  var onStart = function onStart(file) {
    var fileItem = (0, _utils.fileToObject)(file);
    fileItem.status = 'uploading';
    var nextFileList = (0, _toConsumableArray2.default)(fileList || []);

    if (ReplaceFileRef.current && ReplaceFileRef.current.file && currentUploadTypeRef.current === 'replace') {
      nextFileList[ReplaceFileRef.current.index] = fileItem;
    } else {
      nextFileList.push(fileItem);
    }

    if (maxCount === 1) {
      nextFileList = nextFileList.slice(-1);
    } else if (maxCount) {
      nextFileList = nextFileList.slice(0, maxCount);
    }

    handleChange({
      file: fileItem,
      fileList: nextFileList
    });
  };

  var onSuccess = function onSuccess(response, file, xhr) {
    var fileItem = (0, _utils.findItemFromList)(file, fileList);

    if (fileItem) {
      fileItem.status = 'done';
      fileItem.response = response;
      fileItem.xhr = xhr;
      handleChange({
        file: fileItem,
        fileList: fileList
      });
    }
  };

  var onProgress = function onProgress(e, file) {
    var fileItem = (0, _utils.findItemFromList)(file, fileList);

    if (fileItem) {
      fileItem.percent = e.percent;
      handleChange({
        event: e,
        file: fileItem,
        fileList: fileList
      });
    }
  };

  var onError = function onError(error, response, file) {
    var fileItem = (0, _utils.findItemFromList)(file, fileList);

    if (fileItem) {
      fileItem.error = error;
      fileItem.response = response;
      fileItem.status = 'error';
      handleChange({
        file: fileItem,
        fileList: fileList
      });
    }
  };

  var handleFileRemove = function handleFileRemove(file) {
    Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (res) {
      if (res === false) {
        return;
      }

      var key = file.uid ? 'uid' : 'name';
      var fileListAfterRemoved = fileList.filter(function (v) {
        return file[key] !== v[key];
      });

      if (fileListAfterRemoved) {
        var _RcUploadRef$current;

        file.status = 'removed';
        (_RcUploadRef$current = RcUploadRef.current) === null || _RcUploadRef$current === void 0 ? void 0 : _RcUploadRef$current.abort(file);
        handleChange({
          file: file,
          fileList: fileListAfterRemoved
        });
      }
    });
  };

  var getUploadChild = function getUploadChild() {
    var child = /*#__PURE__*/_react.default.createElement(_button.default, {
      disabled: disabled,
      icon: /*#__PURE__*/_react.default.createElement(UploadLine, null)
    }, text || '上传文件');

    if (listType === 'picture-card') {
      var uploadPrefixClsImageBtn = (0, _classnames.default)("".concat(uploadPrefixCls, "-imageBtn"), {
        bgWhite: isOnGrayBg
      });
      child = /*#__PURE__*/_react.default.createElement("div", {
        className: uploadPrefixClsImageBtn
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-imageBtn-icon")
      }, /*#__PURE__*/_react.default.createElement(PlusLine, null)), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-imageBtn-text")
      }, text || '上传图片'));
    }

    if (listType === 'drag') {
      child = /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn")
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn-content")
      }, /*#__PURE__*/_react.default.createElement(PlusLine, null)));
    }

    if (listType === 'drag' && tips) {
      child = /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn")
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn-content")
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn-icon")
      }, /*#__PURE__*/_react.default.createElement(ContainerLine, null)), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn-info")
      }, "\u70B9\u51FB\u6216\u5C06\u6587\u4EF6\u62D6\u62FD\u5230\u8FD9\u91CC\u4E0A\u4F20"), tips && /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(uploadPrefixCls, "-dragBtn-tip")
      }, tips)));
    }

    return child;
  };

  var onFileInputClick = function onFileInputClick() {
    RcUploadRef.current.uploader.fileInput.click();
  };

  var renderFileList = function renderFileList(renderRcOverlay, tipsOverlay) {
    return /*#__PURE__*/_react.default.createElement(_fileList.default, {
      fileList: fileList,
      prefixCls: uploadPrefixCls,
      listType: listType,
      deletable: deletable,
      removeClick: handleFileRemove,
      onFileInputClick: onFileInputClick,
      itemRender: itemRender,
      renderRcOverlay: renderRcOverlay,
      tipsOverlay: tipsOverlay,
      showUploadList: showUploadList,
      tips: tips,
      fit: fit,
      maxCount: maxCount,
      fileItemComponent: fileItemComponent,
      preview: preview,
      onImagePreview: onPreview,
      onFileReplace: onFileReplace,
      customOnClick: customOnClick
    });
  };

  var renderUpload = function renderUpload() {
    var listTypeCls = "".concat(uploadPrefixCls, "-").concat(listType);
    var RcUploadCls = (0, _classnames.default)(listTypeCls, className);
    var endRcUploadProps = (0, _objectSpread2.default)({
      beforeUpload: beforeUpload,
      onStart: onStart,
      onSuccess: onSuccess,
      onProgress: onProgress,
      onError: onError,
      prefixCls: uploadPrefixCls,
      className: RcUploadCls,
      disabled: disabled
    }, resetProps);

    var renderUploadOverlay = function renderUploadOverlay(noChildren) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_components.RcUpload, Object.assign({
        onClick: function onClick() {
          currentUploadTypeRef.current = 'upload';
        },
        customOnClick: customOnClick,
        ref: RcUploadRef
      }, endRcUploadProps), children || (noChildren ? null : getUploadChild())));
    };

    var tipsOverlay = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, listType !== 'drag' && tips && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(listTypeCls, "-tips")
    }, tips)); // return <>{renderFileList(uploadOverlay, tipsOverlay, endRcUploadProps)}</>;


    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderFileList(renderUploadOverlay, tipsOverlay));
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderUpload());
});

Upload.defaultProps = {
  preview: false,
  deletable: true,
  beforeUpload: function beforeUpload() {
    return true;
  },
  showUploadList: true,
  listType: 'text',
  fit: 'cover',
  accept: ''
};
var _default = Upload;
exports.default = _default;