/* eslint-disable no-console */
import React, { useContext } from 'react';
import cls from 'clsx';
import { Close, Cross, Description } from '@react-vant/icons'; // Utils

import { isImageFile } from './utils';
import { isDef, getSizeStyle, extend } from '../utils';
import { callInterceptor } from '../utils/interceptor'; // Components

import Image from '../image';
import Loading from '../loading';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
export var UploaderPreviewItem = function UploaderPreviewItem(props) {
  var _useContext = useContext(ConfigProviderContext),
      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.createElement(Close, {
        className: cls(bem('mask-icon'))
      }) : /*#__PURE__*/React.createElement(Loading, {
        className: cls(bem('loading'))
      });
      var showMessage = isDef(message) && message !== '';
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('mask'))
      }, MaskIcon, showMessage && /*#__PURE__*/React.createElement("div", {
        className: cls(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();
    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.createElement("div", {
        className: cls(bem('preview-delete')),
        onClick: onDelete
      }, /*#__PURE__*/React.createElement(Cross, {
        className: cls(bem('preview-delete-icon'))
      }));
    }

    return null;
  };

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

    return null;
  };

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

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

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

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