"use strict";

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

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

function _icons() {
  const data = require("@react-vant/icons");

  _icons = function () {
    return data;
  };

  return data;
}

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 (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
const UploaderPreviewItem = props => {
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('uploader', prefixCls);

  const renderMask = () => {
    const {
      status,
      message
    } = props.item;

    if (status === 'uploading' || status === 'failed') {
      const MaskIcon = status === 'failed' ? (0, _jsxRuntime().jsx)(_icons().Close, {
        className: (0, _clsx().default)(bem('mask-icon'))
      }) : (0, _jsxRuntime().jsx)(_loading.default, {
        className: (0, _clsx().default)(bem('loading'))
      });
      const showMessage = (0, _utils2.isDef)(message) && message !== '';
      return (0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('mask'))
      }, {
        children: [MaskIcon, showMessage && (0, _jsxRuntime().jsx)("div", Object.assign({
          className: (0, _clsx().default)(bem('mask-message'))
        }, {
          children: message
        }))]
      }));
    }

    return null;
  };

  const onDelete = event => {
    const {
      name,
      item,
      index,
      beforeDelete
    } = props;
    event.stopPropagation();
    (0, _interceptor.callInterceptor)({
      interceptor: beforeDelete,
      args: [item, {
        name,
        index
      }],
      done: () => {
        var _a;

        return (_a = props.onDelete) === null || _a === void 0 ? void 0 : _a.call(props);
      }
    });
  };

  const onPreview = () => {
    if (props.onPreview) props.onPreview();
  };

  const renderDeleteIcon = () => {
    if (props.deletable && props.item.status !== 'uploading') {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('preview-delete')),
        onClick: onDelete
      }, {
        children: (0, _jsxRuntime().jsx)(_icons().Cross, {
          className: (0, _clsx().default)(bem('preview-delete-icon'))
        })
      }));
    }

    return null;
  };

  const renderCover = () => {
    if (props.previewCoverRender) {
      const {
        index,
        item
      } = props;
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('preview-cover'))
      }, {
        children: props.previewCoverRender((0, _utils2.extend)({
          index
        }, item))
      }));
    }

    return null;
  };

  const renderPreview = () => {
    const {
      item
    } = props;

    if ((0, _utils.isImageFile)(item)) {
      return (0, _jsxRuntime().jsx)(_image.default, Object.assign({
        fit: props.imageFit,
        src: item.content || item.url,
        className: (0, _clsx().default)(bem('preview-image')),
        width: props.previewSize,
        height: props.previewSize,
        onClick: onPreview
      }, {
        children: renderCover()
      }));
    }

    return (0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('file')),
      style: (0, _utils2.getSizeStyle)(props.previewSize)
    }, {
      children: [(0, _jsxRuntime().jsx)(_icons().Description, {
        className: (0, _clsx().default)(bem('file-icon'))
      }), (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('file-name'), 'rv-ellipsis')
      }, {
        children: item.file ? item.file.name : item.url
      })), renderCover()]
    }));
  };

  return (0, _jsxRuntime().jsxs)("div", Object.assign({
    className: (0, _clsx().default)(bem('preview')),
    onClick: props.onClick
  }, {
    children: [renderPreview(), renderMask(), renderDeleteIcon()]
  }));
};

exports.UploaderPreviewItem = UploaderPreviewItem;