"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _tslib = require("tslib");

var _mobx = require("mobx");

var _isNil = _interopRequireDefault(require("lodash/isNil"));

var extReg = /(.*)\.([^.]*)$/;

var AttachmentFile = /*#__PURE__*/function () {
  function AttachmentFile(file) {
    var _this = this;

    (0, _classCallCheck2["default"])(this, AttachmentFile);
    (0, _mobx.runInAction)(function () {
      _this.name = '';
      _this.filename = '';
      _this.size = 0;
      _this.type = '';
      _this.uid = '';
      _this.creationDate = new Date();

      _this.load(file);
    });
  }

  (0, _createClass2["default"])(AttachmentFile, [{
    key: "percent",
    get: function get() {
      var chunks = this.chunks;

      if (chunks) {
        var length = chunks.length;
        return chunks.reduce(function (sum, chunk) {
          var status = chunk.status,
              _chunk$percent = chunk.percent,
              percent = _chunk$percent === void 0 ? status === 'success' ? 100 : 0 : _chunk$percent;
          return sum + percent / length;
        }, 0);
      }

      return this.$percent;
    },
    set: function set(percent) {
      var _this2 = this;

      (0, _mobx.runInAction)(function () {
        _this2.$percent = percent;
      });
    }
  }, {
    key: "load",
    value: function load(file) {
      var _this3 = this;

      var name = file.name;

      if (name) {
        var matches = name.match(extReg);

        if (matches && matches.length > 2) {
          this.ext = matches[2] ? matches[2].toLowerCase() : '';
          this.filename = matches[1];
        } else {
          this.ext = '';
          this.filename = name;
        }
      }

      Object.keys(file).forEach(function (key) {
        var value = file[key];

        if (!(0, _isNil["default"])(value)) {
          switch (key) {
            case 'uid':
              _this3.uid = String(value);
              break;

            default:
              _this3[key] = value;
          }
        }
      });
    }
  }]);
  return AttachmentFile;
}();

exports["default"] = AttachmentFile;
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "name", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "size", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "url", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "filename", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "ext", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "status", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "$percent", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "error", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "errorMessage", void 0);
(0, _tslib.__decorate)([_mobx.observable], AttachmentFile.prototype, "invalid", void 0);
(0, _tslib.__decorate)([_mobx.action], AttachmentFile.prototype, "load", null);
//# sourceMappingURL=AttachmentFile.js.map
