'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 _InteractionType = require('quiz-interactions/lib/records/InteractionType');

var _InteractionType2 = _interopRequireDefault(_InteractionType);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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

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

var _reduxRecord2 = _interopRequireDefault(_reduxRecord);

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

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

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

  (0, _createClass3.default)(Item, [{
    key: 'isLoaded',
    value: function isLoaded() {
      return this.id !== '0';
    }
  }, {
    key: 'hasResponse',
    value: function hasResponse(userResponse) {
      if (!userResponse || userResponse.value === null || userResponse.value === void 0) return false;
      return this.getInteractionType().hasResponse(userResponse.value, this.interactionData.toJS());
    }
  }, {
    key: 'getChoice',
    value: function getChoice(choiceId) {
      return this.getIn(['interactionData', 'choices'], (0, _immutable.fromJS)([])).find(function (choice) {
        return choice.get('id') === choiceId;
      });
    }
  }, {
    key: 'getInteractionType',
    value: function getInteractionType() {
      return this.stateHelper().findInteractionType(this.interactionType) || new _InteractionType2.default();
    }
  }, {
    key: 'hasModifications',
    value: function hasModifications() {
      return !(0, _immutable.is)(this.getWorkingInstance(), this);
    }
  }, {
    key: 'hasFeedback',
    value: function hasFeedback() {
      return this.getIn(['feedback', 'correct']) || this.getIn(['feedback', 'incorrect']) || this.getIn(['feedback', 'neutral']);
    }
  }, {
    key: 'getScoringAlgorithm',
    value: function getScoringAlgorithm() {
      return this.scoringAlgorithm || this.getInteractionType().scoringAlgorithmDefault;
    }
  }, {
    key: 'getWorkingInstance',
    value: function getWorkingInstance(overrides) {
      var mods = this.getState().getIn(['modifications', 'items', this.id]);
      var data = this.merge(overrides, mods);
      // needed so we can maintain state... this is sad
      return Item.create(data, this.getState());
    }
  }, {
    key: 'isImmutable',
    value: function isImmutable() {
      return this.status === 'immutable';
    }
  }, {
    key: 'isTemporary',
    value: function isTemporary() {
      return this.id === _constants.TEMPORARY_ITEM_ID;
    }

    // PRIVATE METHODS

  }, {
    key: '__defaultItem',
    value: function __defaultItem() {
      return this.id === '0';
    }
  }, {
    key: 'isStimulus',
    get: function get() {
      return false;
    }
  }]);
  return Item;
}((0, _reduxRecord2.default)({
  id: '0',
  title: 'Title',
  label: 'label',
  itemBody: 'Question stem',
  interactionTypeId: '0',
  interactionType: '0',
  interactionData: (0, _immutable.Map)(),
  properties: (0, _immutable.Map)(),
  contextId: null,
  toolName: null,
  parentItemId: null,
  parentItemRelationship: null,
  createdAt: null,
  updatedAt: null,
  scoringAlgorithm: null,
  scoringData: (0, _immutable.Map)(),
  scoringDataSchema: (0, _immutable.Map)(),
  status: 'mutable',
  outcomeDeclaration: null,
  outcomeAlignmentSetGuid: null,
  feedback: (0, _immutable.Map)(),
  stimulusId: null,
  calculatorType: 'none'
})), _class.validations = {
  onSelf: [(0, _instructureValidations.rule)('maxContent', { limit: _constants.BUILD_CHAR_LIMIT })]
}, _temp);
exports.default = Item;