"use strict";

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

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 _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _immutable = require("immutable");

var _reduxRecord = _interopRequireDefault(require("./reduxRecord"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-common/lib/format-message"));

var _constants = require("@instructure/quiz-common/lib/constants");

var _instructureValidations = require("instructure-validations");

var _validationHelpers = require("quiz-interactions/lib/util/validationHelpers");

var Bank =
/*#__PURE__*/
function (_ReduxRecord) {
  (0, _inherits2.default)(Bank, _ReduxRecord);

  function Bank() {
    (0, _classCallCheck2.default)(this, Bank);
    return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Bank).apply(this, arguments));
  }

  (0, _createClass2.default)(Bank, [{
    key: "createdAt",
    value: function createdAt() {
      return new Date(this.get('createdAt'));
    }
  }, {
    key: "updatedAt",
    value: function updatedAt() {
      return new Date(this.get('updatedAt'));
    }
  }, {
    key: "isLoaded",
    value: function isLoaded() {
      return this.id !== '0';
    }
  }, {
    key: "isTemporary",
    value: function isTemporary() {
      return this.id === _constants.TEMPORARY_BANK_ID;
    }
  }, {
    key: "getWorkingInstance",
    value: function getWorkingInstance() {
      var data = this.merge(this.getState().getIn(['modifications', 'banks', this.id]));
      return Bank.create(data);
    }
  }, {
    key: "hasModifications",
    value: function hasModifications() {
      return false;
    }
  }, {
    key: "canImport",
    value: function canImport() {
      return this.status === 'waiting' && this.entryCount === 0;
    }
  }, {
    key: "getLatestImport",
    value: function getLatestImport() {
      var _this = this;

      var allImports = this.getState().get('qtiImports', (0, _immutable.List)());
      var importsForBank = allImports.valueSeq().filter(function (qtiImport) {
        return qtiImport.get('parentId') === _this.id && qtiImport.get('parentType') === 'Bank';
      });
      var latestImport = importsForBank.maxBy(function (i) {
        return i.get('id');
      });
      var id = latestImport && latestImport.get('id');
      return this.stateHelper().findImport(id);
    }
  }]);
  return Bank;
}((0, _reduxRecord.default)({
  id: '0',
  owner: '',
  title: '',
  language: null,
  createdAt: null,
  updatedAt: null,
  filters: null,
  archived: false,
  sharedCount: 0,
  entryCount: 0,
  // item count + stimulus count
  itemEntryCount: 0,
  // item count only
  status: null,
  bankEntries: (0, _immutable.Set)()
}));

exports.default = Bank;
Bank.validations = {
  title: [(0, _instructureValidations.rule)('presence', {
    message: (0, _validationHelpers.presenceMessage)("Banknavn")
  })]
};