"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 _Item = _interopRequireDefault(require("./Item"));

var _Stimulus = _interopRequireDefault(require("./Stimulus"));

var _SessionItemResult = _interopRequireDefault(require("./SessionItemResult"));

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

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

  (0, _createClass2.default)(SessionItem, [{
    key: "getItem",
    value: function getItem() {
      if (this.item) {
        return _Item.default.create(this.item);
      }
    }
  }, {
    key: "getEntry",
    value: function getEntry() {
      return {
        Stimulus: this.stimulusRecord,
        Item: this.itemRecord
      }[this.type];
    }
  }, {
    key: "getNestedEntries",
    value: function getNestedEntries() {
      var _this = this;

      var fromState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

      if (this.stimulus) {
        if (this.items && !fromState) {
          return this.items;
        }

        var stimulusIdFilter = function stimulusIdFilter(si) {
          return si.get('quizSessionId') === _this.quizSessionId && si.getIn(['stimulus', 'id']) === _this.getIn(['stimulus', 'id']);
        };

        var stimulusKeyFilter = function stimulusKeyFilter(si) {
          return si.get('quizSessionId') === _this.quizSessionId && si.get('stimulusKey') === _this.stimulusKey;
        }; // fallback to use the stimulus id when stimulusKey is not present


        var filter = this.stimulusKey === null ? stimulusIdFilter : stimulusKeyFilter;
        this.items = this.getState().get('sessionItems').filter(filter).map(function (si) {
          return SessionItem.create(si);
        }).sortBy(function (si) {
          return si.position;
        }).toList();
        return this.items;
      }

      return (0, _immutable.List)();
    }
  }, {
    key: "sessionItemResult",
    get: function get() {
      return _SessionItemResult.default.create(this.getState().getIn(['sessionItemResults', this.id]));
    }
  }, {
    key: "type",
    get: function get() {
      return this.stimulus !== null ? 'Stimulus' : 'Item';
    }
  }, {
    key: "isStimulus",
    get: function get() {
      return this.type === 'Stimulus';
    }
  }, {
    key: "stimulusRecord",
    get: function get() {
      return _Stimulus.default.create(this.stimulus);
    }
  }, {
    key: "itemRecord",
    get: function get() {
      return _Item.default.create(this.item);
    }
  }, {
    key: "hasNestedRecords",
    get: function get() {
      return this.isStimulus;
    }
  }, {
    key: "positionSlots",
    get: function get() {
      // decrement to account for stimulus not being its own sessionItem
      // and the generateDisplayPositions assume each item with nested
      // values does not count itself, but it can't be negative
      var slots = this.displayPositionSlots - 1;
      return slots >= 0 ? slots : 0;
    }
  }, {
    key: "displayPositionSlots",
    get: function get() {
      if (this.isStimulus) {
        return this.getNestedEntries().size;
      }

      return 1;
    }
  }]);
  return SessionItem;
}((0, _reduxRecord.default)({
  id: null,
  item: {},
  pointsPossible: null,
  position: null,
  quizEntryId: null,
  quizSessionId: null,
  scoringData: {},
  calculatorType: 'none',
  stimulus: null,
  stimulusKey: null
}));

exports.default = SessionItem;