'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 _immutable = require('immutable');

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

var _reduxRecord2 = _interopRequireDefault(_reduxRecord);

var _Item = require('./Item');

var _Item2 = _interopRequireDefault(_Item);

var _Stimulus = require('./Stimulus');

var _Stimulus2 = _interopRequireDefault(_Stimulus);

var _SessionItemResult = require('./SessionItemResult');

var _SessionItemResult2 = _interopRequireDefault(_SessionItemResult);

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

var SessionItem = function (_ReduxRecord) {
  (0, _inherits3.default)(SessionItem, _ReduxRecord);

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

  (0, _createClass3.default)(SessionItem, [{
    key: 'getItem',
    value: function getItem() {
      if (this.item) {
        return _Item2.default.create(this.item);
      }
    }
  }, {
    key: 'getEntry',
    value: function getEntry() {
      return {
        Stimulus: this.stimulusRecord,
        Item: this.itemRecord
      }[this.type];
    }
  }, {
    key: 'getNestedEntries',
    value: function getNestedEntries() {
      var _this2 = 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') === _this2.quizSessionId && si.getIn(['stimulus', 'id']) === _this2.getIn(['stimulus', 'id']);
        };

        var stimulusKeyFilter = function stimulusKeyFilter(si) {
          return si.get('quizSessionId') === _this2.quizSessionId && si.get('stimulusKey') === _this2.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 _SessionItemResult2.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 _Stimulus2.default.create(this.stimulus);
    }
  }, {
    key: 'itemRecord',
    get: function get() {
      return _Item2.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, _reduxRecord2.default)({
  id: null,
  item: {},
  pointsPossible: null,
  position: null,
  quizEntryId: null,
  quizSessionId: null,
  scoringData: {},
  stimulus: null,
  stimulusKey: null
}));

exports.default = SessionItem;