"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));

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

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

var QuestionaireViewModel =
/*#__PURE__*/
function () {
  function QuestionaireViewModel() {
    this._rate = null;
    this._nps = null;
    this.feedback = '';
    this.contact = '';
  }

  var _proto = QuestionaireViewModel.prototype;

  _proto.getInfo = function getInfo(position) {
    var _this = this;

    return _request.default.get('/api/deliveries', {
      params: {
        position
      },
      timeout: 2 * 1000
    }).then(function (res) {
      var data = res.data;

      if (data.length) {
        _this.metaData = data[0];
        return true;
      }

      return false;
    });
  };

  _proto.submit =
  /*#__PURE__*/
  function () {
    var _submit = (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (this.metaData) {
                _context.next = 2;
                break;
              }

              throw new Error('缺少问卷数据信息');

            case 2:
              return _context.abrupt("return", _request.default.post('/api/feedbacks', {
                satisfy_grade: this.rate,
                grade: this.nps,
                contact: this.contact,
                content: this.feedback,
                delivery_id: this.metaData.id,
                survey_id: this.metaData.survey.id
              }));

            case 3:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    function submit() {
      return _submit.apply(this, arguments);
    }

    return submit;
  }();

  (0, _createClass2.default)(QuestionaireViewModel, [{
    key: "rate",
    set: function set(value) {
      this._rate = Math.min(Math.max(value, 0), 5);
    },
    get: function get() {
      return this._rate;
    }
  }, {
    key: "nps",
    set: function set(value) {
      this._nps = Math.min(Math.max(value, 0), 10);
    },
    get: function get() {
      return this._nps;
    }
  }, {
    key: "canSubmit",
    get: function get() {
      return this.rate !== null && this.rate >= 0 && this.nps !== null && this.nps >= 0;
    }
  }]);
  return QuestionaireViewModel;
}();

exports.default = QuestionaireViewModel;