"use strict";

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

var _util = require("../../util");

var _util2 = require("../util");

var _request = _interopRequireDefault(require("./request"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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

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

var noop = _util.func.noop;

var Uploader =
/*#__PURE__*/
function () {
  function Uploader(options) {
    _classCallCheck(this, Uploader);

    this.options = _objectSpread({
      beforeUpload: noop,
      onProgress: noop,
      onSuccess: noop,
      onError: noop,
      data: {},
      name: 'file',
      method: 'post'
    }, options);
    this.reqs = {};
  }

  _createClass(Uploader, [{
    key: "setOptions",
    value: function setOptions(options) {
      _extends(this.options, options);
    }
  }, {
    key: "startUpload",
    value: function startUpload(files) {
      var _this = this;

      var filesArr = files.length ? Array.prototype.slice.call(files) : [files];
      filesArr.forEach(function (file) {
        file.uid = file.uid || (0, _util2.uid)();

        _this.upload(file);
      });
    }
  }, {
    key: "abort",
    value: function abort(file) {
      var reqs = this.reqs;

      if (file) {
        var _uid = file;

        if (file && file.uid) {
          _uid = file.uid;
        }

        if (reqs[_uid]) {
          reqs[_uid].abort();

          delete reqs[_uid];
        }
      } else {
        Object.keys(reqs).forEach(function (uid) {
          if (reqs[uid]) {
            reqs[uid].abort();
          }

          delete reqs[uid];
        });
      }
    }
  }, {
    key: "upload",
    value: function upload(file) {
      var _this2 = this;

      var _this$options = this.options,
          beforeUpload = _this$options.beforeUpload,
          action = _this$options.action,
          name = _this$options.name,
          headers = _this$options.headers,
          timeout = _this$options.timeout,
          withCredentials = _this$options.withCredentials,
          method = _this$options.method,
          data = _this$options.data;
      var before = beforeUpload(file, {
        action: action,
        name: name,
        headers: headers,
        timeout: timeout,
        withCredentials: withCredentials,
        method: method,
        data: data
      });

      _util.func.promiseCall(before, function (options) {
        if (options === false) {
          var err = new Error(_util2.errorCode.BEFOREUPLOAD_REJECT);
          err.code = _util2.errorCode.BEFOREUPLOAD_REJECT;
          return _this2.options.onError(err, null, file);
        }

        _this2.post(file, _util.obj.isPlainObject(options) ? options : undefined);
      }, function (error) {
        _this2.options.onError(error, null, file);
      });
    }
  }, {
    key: "post",
    value: function post(file) {
      var _this3 = this;

      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

      var requestOptions = _objectSpread({}, this.options, {}, options);

      var action = requestOptions.action,
          name = requestOptions.name,
          headers = requestOptions.headers,
          timeout = requestOptions.timeout,
          withCredentials = requestOptions.withCredentials,
          _onProgress = requestOptions.onProgress,
          _onSuccess = requestOptions.onSuccess,
          _onError = requestOptions.onError,
          method = requestOptions.method;
      var data = requestOptions.data;

      if (typeof data === 'function') {
        data = data(file);
      }

      var uid = file.uid;
      var request = typeof requestOptions.request === 'function' ? requestOptions.request : _request["default"];
      this.reqs[uid] = request({
        action: action,
        filename: name,
        file: file,
        data: data,
        timeout: timeout,
        headers: headers,
        withCredentials: withCredentials,
        method: method,
        onProgress: function onProgress(e) {
          _onProgress(e, file);
        },
        onSuccess: function onSuccess(ret) {
          delete _this3.reqs[uid];

          _onSuccess(ret, file);
        },
        onError: function onError(err, ret) {
          delete _this3.reqs[uid];

          _onError(err, ret, file);
        }
      });
    }
  }]);

  return Uploader;
}();

exports["default"] = Uploader;