"use strict";

exports.__esModule = true;
exports.UploaderPreviewItem = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

var _icons = require("@react-vant/icons");

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

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

var _interceptor = require("../utils/interceptor");

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

var _loading = _interopRequireDefault(require("../loading"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/* eslint-disable no-console */
// Utils
// Components
var UploaderPreviewItem = function UploaderPreviewItem(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('uploader', prefixCls),
      bem = _createNamespace[0];

  var renderMask = function renderMask() {
    var _props$item = props.item,
        status = _props$item.status,
        message = _props$item.message;

    if (status === 'uploading' || status === 'failed') {
      var MaskIcon = status === 'failed' ? /*#__PURE__*/_react.default.createElement(_icons.Close, {
        className: (0, _clsx.default)(bem('mask-icon'))
      }) : /*#__PURE__*/_react.default.createElement(_loading.default, {
        className: (0, _clsx.default)(bem('loading'))
      });
      var showMessage = (0, _utils2.isDef)(message) && message !== '';
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('mask'))
      }, MaskIcon, showMessage && /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('mask-message'))
      }, message));
    }

    return null;
  };

  var onDelete = function onDelete(event) {
    var name = props.name,
        item = props.item,
        index = props.index,
        beforeDelete = props.beforeDelete;
    event.stopPropagation();
    (0, _interceptor.callInterceptor)({
      interceptor: beforeDelete,
      args: [item, {
        name: name,
        index: index
      }],
      done: function done() {
        return props.onDelete == null ? void 0 : props.onDelete();
      }
    });
  };

  var onPreview = function onPreview() {
    if (props.onPreview) props.onPreview();
  };

  var renderDeleteIcon = function renderDeleteIcon() {
    if (props.deletable && props.item.status !== 'uploading') {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('preview-delete')),
        onClick: onDelete
      }, /*#__PURE__*/_react.default.createElement(_icons.Cross, {
        className: (0, _clsx.default)(bem('preview-delete-icon'))
      }));
    }

    return null;
  };

  var renderCover = function renderCover() {
    if (props.previewCoverRender) {
      var index = props.index,
          item = props.item;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('preview-cover'))
      }, props.previewCoverRender((0, _utils2.extend)({
        index: index
      }, item)));
    }

    return null;
  };

  var renderPreview = function renderPreview() {
    var item = props.item;

    if ((0, _utils.isImageFile)(item)) {
      return /*#__PURE__*/_react.default.createElement(_image.default, {
        fit: props.imageFit,
        src: item.content || item.url,
        className: (0, _clsx.default)(bem('preview-image')),
        width: props.previewSize,
        height: props.previewSize,
        onClick: onPreview
      }, renderCover());
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('file')),
      style: (0, _utils2.getSizeStyle)(props.previewSize)
    }, /*#__PURE__*/_react.default.createElement(_icons.Description, {
      className: (0, _clsx.default)(bem('file-icon'))
    }), /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('file-name'), 'rv-ellipsis')
    }, item.file ? item.file.name : item.url), renderCover());
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('preview')),
    onClick: props.onClick
  }, renderPreview(), renderMask(), renderDeleteIcon());
};

exports.UploaderPreviewItem = UploaderPreviewItem;