"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _excluded = ["list", "uploading", "onChoose", "onDelete", "onClick"];

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Attach = (0, _react.forwardRef)(function (_ref, ref) {
  var list = _ref.list,
      uploading = _ref.uploading,
      onChoose = _ref.onChoose,
      onDelete = _ref.onDelete,
      onClick = _ref.onClick,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  function getIcon(src) {
    var suffix = src.getSuffix();
    if (!suffix) return 'unknown';

    if (suffix.indexOf('?') !== -1) {
      suffix = suffix.substring(0, suffix.indexOf('?'));
    }

    if ('RM,RMVB,MP4,3GP,AVI,MKV,WMV,MPG,VOB,FLV'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'video';
    }

    if ('WAVE,MPEG,MP3,MPEG-4,MIDI,WMA,VQF,AMR,APE,FLAC,AAC'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'audio';
    }

    if ('JPG,JPEG,WEBP,GIF,PNG,TIF,BMP'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'pic';
    }

    if ('RAR,ZIP'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'pack';
    }

    if ('DOC,DOCX'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'word';
    }

    if ('XSL,EXCEL'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'excel';
    }

    if ('PPT'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'ppt';
    }

    if ('PDF'.indexOf(suffix.toUpperCase()) !== -1) {
      return 'pdf';
    }

    return 'unknown';
  }

  function click(e) {
    var target = e.target;

    if (target.classList.contains('attach-item')) {
      // 点击一行
      var index = target.getAttribute('data-index');
      if (index && onClick) onClick(e, list[index].src, [list[index]], Number(index));
    } else if (target.classList.contains('attach-item-delete')) {
      // 点击删除
      var _index = target.parentNode.getAttribute('data-index');

      if (_index && onDelete) onDelete(e, list[_index].src, [list[_index]], Number(_index));
    }
  } // file框选择


  function fileChange(e) {
    if (onChoose) onChoose(e);
    e.target.value = ''; // 防止选择重复图片时不触发

    e.stopPropagation();
  }

  return /*#__PURE__*/_react["default"].createElement("div", _extends({
    ref: ref
  }, others, {
    className: "attach" + (others.className ? ' ' + others.className : ''),
    onClick: click
  }), onChoose && /*#__PURE__*/_react["default"].createElement("div", {
    className: "attach-upload" + (uploading ? ' disabled' : '')
  }, /*#__PURE__*/_react["default"].createElement("input", {
    type: "file",
    name: "uploadAttach",
    onChange: fileChange
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "attach-upload-wrapper"
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "attach-upload-icon" + (uploading ? ' uploading' : '')
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "attach-upload-label"
  }, "\u9009\u62E9\u9644\u4EF6"))), list && list.length > 0 && list.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: index,
      "data-index": index,
      className: "attach-item" + (item.className ? ' ' + item.className : ''),
      style: item.style || {}
    }, /*#__PURE__*/_react["default"].createElement("i", {
      className: "icon attach-item-type " + getIcon(item.src)
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "attach-item-label"
    }, item.name || item.src), onDelete && /*#__PURE__*/_react["default"].createElement("i", {
      className: "attach-item-delete"
    }), item.children);
  }));
});
var _default = Attach;
exports["default"] = _default;
module.exports = exports.default;