'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _class, _temp;

var _immutable = require('immutable');

var _reduxRecord = require('./reduxRecord');

var _reduxRecord2 = _interopRequireDefault(_reduxRecord);

var _formatMessage = require('../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _constants = require('../../constants');

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

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

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

var Bank = (_temp = _class = function (_ReduxRecord) {
  (0, _inherits3.default)(Bank, _ReduxRecord);

  function Bank() {
    (0, _classCallCheck3.default)(this, Bank);
    return (0, _possibleConstructorReturn3.default)(this, (Bank.__proto__ || Object.getPrototypeOf(Bank)).apply(this, arguments));
  }

  (0, _createClass3.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 _this2 = this;

      var allImports = this.getState().get('qtiImports');
      var importsForBank = allImports.valueSeq().filter(function (qtiImport) {
        return qtiImport.get('parentId') === _this2.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, _reduxRecord2.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)()
})), _class.validations = {
  title: [(0, _instructureValidations.rule)('presence', { message: (0, _validationHelpers.presenceMessage)('Bank name') })]
}, _temp);
exports.default = Bank;