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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

// require (PrototypeString.js), 使用了getSuffix
import React, { forwardRef } from 'react';

var Attach = forwardRef(function (_ref, ref) {
  var list = _ref.list,
      uploading = _ref.uploading,
      onChoose = _ref.onChoose,
      onDelete = _ref.onDelete,
      onClick = _ref.onClick,
      others = _objectWithoutProperties(_ref, ['list', 'uploading', 'onChoose', 'onDelete', 'onClick']);

  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], Number(index));
    } else if (target.classList.contains('attach-item-delete')) {
      // 点击删除
      var _index = target.parentNode.getAttribute('data-index');
      if (_index && onDelete) onDelete(e, list[_index], Number(_index));
    }
  }
  // file框选择
  function fileChange(e) {
    if (onChoose) onChoose(e);
    e.target.value = ''; // 防止选择重复图片时不触发
    e.stopPropagation();
  }
  return React.createElement(
    'div',
    _extends({
      ref: ref
    }, others, {
      className: 'attach' + (others.className ? ' ' + others.className : ''),
      onClick: click
    }),
    onChoose && React.createElement(
      'div',
      { className: 'attach-upload' + (uploading ? ' disabled' : '') },
      React.createElement('input', { type: 'file', name: 'uploadAttach', onChange: fileChange }),
      React.createElement(
        'div',
        { className: 'attach-upload-wrapper' },
        React.createElement('i', { className: 'attach-upload-icon' + (uploading ? ' uploading' : '') }),
        React.createElement(
          'div',
          { className: 'attach-upload-label' },
          '\u9009\u62E9\u9644\u4EF6'
        )
      )
    ),
    list && list.length > 0 && list.map(function (item, index) {
      return React.createElement(
        'div',
        {
          key: index,
          'data-index': index,
          className: 'attach-item' + (item.className ? ' ' + item.className : ''),
          style: item.style || {}
        },
        React.createElement('i', { className: 'icon attach-item-type ' + getIcon(item.src) }),
        React.createElement(
          'div',
          { className: 'attach-item-label' },
          item.name || item.src
        ),
        onDelete && React.createElement('i', { className: 'attach-item-delete' }),
        item.children
      );
    })
  );
});

export default Attach;