function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { func } from '../../util';
import Uploader from './uploader';
import Selecter from './selecter';

var Html5Uploader =
/*#__PURE__*/
function (_Component) {
  _inherits(Html5Uploader, _Component);

  function Html5Uploader() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Html5Uploader);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Html5Uploader)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "getUploadOptions", function (props) {
      return {
        action: props.action,
        name: props.name,
        timeout: props.timeout,
        method: props.method,
        beforeUpload: props.beforeUpload,
        onProgress: props.onProgress,
        onSuccess: props.onSuccess,
        onError: props.onError,
        withCredentials: props.withCredentials,
        headers: props.headers,
        data: props.data,
        request: props.request
      };
    });

    return _this;
  }

  _createClass(Html5Uploader, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var props = this.props;
      var options = this.getUploadOptions(props);
      this.uploader = new Uploader(options);
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var options = this.getUploadOptions(nextProps);
      this.uploader.setOptions(options);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.abort();
    }
  }, {
    key: "abort",
    value: function abort(file) {
      this.uploader.abort(file);
    }
  }, {
    key: "startUpload",
    value: function startUpload(fileList) {
      this.uploader.startUpload(fileList);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          accept = _this$props.accept,
          multiple = _this$props.multiple,
          children = _this$props.children,
          id = _this$props.id,
          disabled = _this$props.disabled,
          dragable = _this$props.dragable,
          style = _this$props.style,
          className = _this$props.className,
          onSelect = _this$props.onSelect,
          onDragOver = _this$props.onDragOver,
          onDragLeave = _this$props.onDragLeave,
          onDrop = _this$props.onDrop,
          name = _this$props.name;
      return React.createElement(Selecter, {
        id: id,
        accept: accept,
        multiple: multiple,
        dragable: dragable,
        disabled: disabled,
        className: className,
        style: style,
        onSelect: onSelect,
        onDragOver: onDragOver,
        onDragLeave: onDragLeave,
        onDrop: onDrop,
        name: name
      }, children);
    }
  }]);

  return Html5Uploader;
}(Component);

_defineProperty(Html5Uploader, "propTypes", _objectSpread({}, Selecter.propTypes, {
  /**
   * 上传的地址
   */
  action: PropTypes.string,

  /**
   * 接受上传的文件类型 (image/png, image/jpg, .doc, .ppt) 详见 [input accept attribute](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-accept)
   */
  accept: PropTypes.string,

  /**
   * 上传额外传参
   */
  data: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),

  /**
   * 设置上传的请求头部
   */
  headers: PropTypes.object,

  /**
   * 是否允许请求携带 cookie
   */
  withCredentials: PropTypes.bool,

  /**
   * 上传文件之前
   * @param {Object} file 文件对象
   * @return {Boolean} `false` 停止上传
   */
  beforeUpload: PropTypes.func,

  /**
   * 正在上传文件的钩子，参数为上传的事件以及文件
   */
  onProgress: PropTypes.func,

  /**
   * 上传成功回调函数，参数为请求下响应信息以及文件
   */
  onSuccess: PropTypes.func,

  /**
   * 上传失败回调函数，参数为上传失败的信息、响应信息以及文件
   */
  onError: PropTypes.func,
  children: PropTypes.node,

  /**
   * 上传超时,单位ms
   */
  timeout: PropTypes.number,

  /**
   * 上传方法
   */
  method: PropTypes.oneOf(['post', 'put']),
  request: PropTypes.func
}));

_defineProperty(Html5Uploader, "defaultProps", _objectSpread({}, Selecter.defaultProps, {
  name: 'file',
  multiple: false,
  withCredentials: true,
  beforeUpload: func.noop,
  onSelect: func.noop,
  onDragOver: func.noop,
  onDragLeave: func.noop,
  onDrop: func.noop,
  onProgress: func.noop,
  onSuccess: func.noop,
  onError: func.noop,
  onAbort: func.noop,
  method: 'post'
}));

export { Html5Uploader as default };