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

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

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

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

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

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

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

  (0, _createClass2.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 _InteractionType.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, _reduxRecord.default)({
  id: '0',
  title: "Titre",
  label: "\xE9tiquette",
  itemBody: "Amorce de question",
  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'
}));

exports.default = Item;
Item.validations = {
  onSelf: [(0, _instructureValidations.rule)('maxContent', {
    limit: _constants.BUILD_CHAR_LIMIT
  })]
};