"use strict";

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

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

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

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

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

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

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _divider = _interopRequireDefault(require("../divider"));

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

var _image = _interopRequireDefault(require("../image"));

var _imagePreview = _interopRequireDefault(require("../image/image-preview"));

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

var ZoomInLine = _icon.default.ZoomInLine,
    AttachmentLine = _icon.default.AttachmentLine,
    CloseLine = _icon.default.CloseLine,
    ReloadLine = _icon.default.ReloadLine,
    CloseFill = _icon.default.CloseFill,
    LoadingLine = _icon.default.LoadingLine,
    SuccessFill = _icon.default.SuccessFill,
    ErrorFill = _icon.default.ErrorFill;

var FileList = function FileList(props) {
  var _classNames;

  var fileList = props.fileList,
      prefixCls = props.prefixCls,
      listType = props.listType,
      deletable = props.deletable,
      removeClick = props.removeClick,
      itemRender = props.itemRender,
      renderRcOverlay = props.renderRcOverlay,
      tipsOverlay = props.tipsOverlay,
      showUploadList = props.showUploadList,
      maxCount = props.maxCount,
      fileItemComponent = props.fileItemComponent,
      onImagePreview = props.onImagePreview,
      onFileInputClick = props.onFileInputClick,
      onFileReplace = props.onFileReplace,
      customOnClick = props.customOnClick,
      preview = props.preview,
      fit = props.fit;

  var _React$useState = _react.default.useState(false),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      visible = _React$useState2[0],
      setVisible = _React$useState2[1];

  var _React$useState3 = _react.default.useState(0),
      _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
      previewId = _React$useState4[0],
      setPreviewId = _React$useState4[1];

  var itemPrefixCls = "".concat(prefixCls, "-fileList-").concat(listType);
  var fileListCls = (0, _classnames.default)("".concat(prefixCls, "-fileList"), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-picture-card"), listType === 'picture-card'), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-text"), listType === 'text'), _classNames));

  var onPreview = function onPreview(file) {
    if (onImagePreview) {
      onImagePreview(file);
      return;
    }

    onPreviewVisibleChange(true, file.url);
  };

  var onReplace = function onReplace(e, file, fileIndex) {
    if (customOnClick) {
      customOnClick(e, file);
      return;
    }

    onFileReplace(file, fileIndex);
    onFileInputClick();
  };

  var onPreviewVisibleChange = function onPreviewVisibleChange(isVisible, url) {
    setVisible(isVisible);
    var pid = fileList === null || fileList === void 0 ? void 0 : fileList.findIndex(function (v) {
      return v.url === url;
    });
    setPreviewId(pid);
  };

  var renderMask = function renderMask(file, index) {
    var _file$error;

    var childPrefixCls = "".concat(itemPrefixCls, "-mask-child");
    var itemCls = (0, _classnames.default)("".concat(itemPrefixCls, "-mask"), {
      'mask-white': preview !== true && file.status === 'done'
    });
    var errTip = '上传失败';

    var errorChild = /*#__PURE__*/_react.default.createElement(_tooltip.default, {
      title: ((_file$error = file.error) === null || _file$error === void 0 ? void 0 : _file$error.errmsg) || errTip
    }, /*#__PURE__*/_react.default.createElement("span", {
      className: childPrefixCls
    }, errTip));

    var successChild = preview === true ? /*#__PURE__*/_react.default.createElement("span", {
      className: childPrefixCls,
      onClick: function onClick() {
        return onPreview(file);
      }
    }, /*#__PURE__*/_react.default.createElement(ZoomInLine, null)) : /*#__PURE__*/_react.default.createElement("span", {
      className: childPrefixCls
    }, /*#__PURE__*/_react.default.createElement(_button.default, {
      size: "small",
      onClick: function onClick(e) {
        return onReplace(e, file, index);
      }
    }, "\u66FF\u6362"));

    var uploadingChild = /*#__PURE__*/_react.default.createElement("span", null, file.percent);

    var childMap = {
      error: errorChild,
      done: successChild,
      uploading: uploadingChild
    };
    return /*#__PURE__*/_react.default.createElement("div", {
      className: itemCls
    }, childMap[file.status] || null);
  };

  var getFileItemIcon = function getFileItemIcon(file) {
    var iconMap = {
      uploading: /*#__PURE__*/_react.default.createElement(LoadingLine, null),
      done: /*#__PURE__*/_react.default.createElement(SuccessFill, null),
      error: /*#__PURE__*/_react.default.createElement(ErrorFill, null)
    };
    var icon = iconMap[file.status];
    if (!icon) return iconMap.uploading;
    return iconMap[file.status];
  };

  var renderItem = function renderItem(file, key, index) {
    var _classNames2;

    if (!file) return;
    var itemCls = (0, _classnames.default)(itemPrefixCls, (_classNames2 = {}, (0, _defineProperty2.default)(_classNames2, "".concat(itemPrefixCls, "-error"), file.status === 'error'), (0, _defineProperty2.default)(_classNames2, "".concat(itemPrefixCls, "-done"), file.status === 'done'), (0, _defineProperty2.default)(_classNames2, "".concat(itemPrefixCls, "-uploading"), file.status === 'uploading'), (0, _defineProperty2.default)(_classNames2, "".concat(itemPrefixCls, "-first-child"), index === 0), _classNames2));
    var itemChildProps = {
      className: itemCls
    };

    if (!itemRender && !fileItemComponent) {
      itemChildProps.key = key;
    }

    var itemChild = /*#__PURE__*/_react.default.createElement("div", Object.assign({}, itemChildProps), /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(itemPrefixCls, "-icon")
    }, getFileItemIcon(file)), /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(itemPrefixCls, "-fileName")
    }, file.name, /*#__PURE__*/_react.default.createElement(AttachmentLine, null)), file.status === 'error' && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(itemPrefixCls, "-reload"),
      onClick: function onClick(e) {
        return onReplace(e, file, index);
      }
    }, /*#__PURE__*/_react.default.createElement(ReloadLine, null)), deletable ? /*#__PURE__*/_react.default.createElement(_divider.default, {
      style: {
        marginLeft: 6,
        marginRight: 6,
        top: 0
      },
      direction: "col"
    }) : null), deletable ? /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(itemPrefixCls, "-close"),
      onClick: function onClick() {
        return removeClick(file);
      }
    }, /*#__PURE__*/_react.default.createElement(CloseLine, null)) : null);

    if (listType === 'picture-card') {
      // const styleObj = {
      //   backgroundImage: `url(${file.url})`,
      // };
      itemChild = /*#__PURE__*/_react.default.createElement("div", Object.assign({}, itemChildProps), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(itemPrefixCls, "-wrapper")
      }, /*#__PURE__*/_react.default.createElement(_image.default, {
        src: file.url || 'error',
        fit: fit,
        width: "100%",
        height: "100%"
      })), file.showMain && /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(itemPrefixCls, "-mainImgTip")
      }, "\u4E3B\u56FE"), deletable ? /*#__PURE__*/_react.default.createElement("span", {
        className: "".concat(itemPrefixCls, "-close"),
        onClick: function onClick() {
          return removeClick(file);
        }
      }, /*#__PURE__*/_react.default.createElement(CloseFill, null)) : null, renderMask(file, index));
    }

    if (itemRender) {
      return itemRender(itemChild, file, index, fileList);
    }

    if (fileItemComponent) {
      var FileItemComponent = fileItemComponent;
      return /*#__PURE__*/_react.default.createElement(FileItemComponent, {
        key: file.uid,
        itemChild: itemChild,
        file: file,
        index: index,
        fileList: fileList
      });
    }

    return itemChild;
  };

  var renderFileListContent = function renderFileListContent() {
    var fileListDom = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, fileList && fileList.length && showUploadList ? fileList.map(function (file, index) {
      return renderItem(file, (file === null || file === void 0 ? void 0 : file.uid) || (file === null || file === void 0 ? void 0 : file.name), index);
    }) : null, /*#__PURE__*/_react.default.createElement(_imagePreview.default, {
      onClose: function onClose() {
        return onPreviewVisibleChange(false);
      },
      visible: visible,
      curIndex: previewId,
      images: fileList
    }));

    if (listType === 'picture-card') {
      if (maxCount && fileList && maxCount === fileList.length // &&
      // fileList[fileList.length - 1].status === ('done' || 'error')
      ) {
          return /*#__PURE__*/_react.default.createElement("div", {
            className: fileListCls
          }, fileListDom, renderRcOverlay(true), tipsOverlay);
        }

      return /*#__PURE__*/_react.default.createElement("div", {
        className: fileListCls
      }, fileListDom, renderRcOverlay(false), tipsOverlay);
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: fileListCls
    }, renderRcOverlay(false), tipsOverlay, fileListDom);
  };

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

var _default = FileList;
exports.default = _default;