"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 _d3Array = require("d3-array");

var _quizI18n = require("@instructure/quiz-i18n");

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

var _interactionTypeMetadata = require("../util/interactionTypeMetadata");

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

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

  (0, _createClass2.default)(ItemAnalysis, [{
    key: "isLoaded",
    value: function isLoaded() {
      return this.itemId !== '0';
    }
  }, {
    key: "interactionTypeName",
    value: function interactionTypeName() {
      var intData = (0, _interactionTypeMetadata.interactionTypeMetadata)({
        slug: this.interactionType
      });
      return intData.name;
    }
  }, {
    key: "correctAnswerInfo",
    value: function correctAnswerInfo() {
      return this.itemAnalysis.get('correctAnswerInfo', (0, _immutable.Map)());
    }
  }, {
    key: "incorrectAnswerInfo",
    value: function incorrectAnswerInfo() {
      return this.itemAnalysis.get('incorrectAnswerInfo', (0, _immutable.Map)());
    }
  }, {
    key: "isPresent",
    value: function isPresent() {
      return !this.notFound;
    }
  }, {
    key: "scoreInfo",
    value: function scoreInfo() {
      return this.itemAnalysis.get('scoreInfo', (0, _immutable.Map)());
    }
  }, {
    key: "choiceInfo",
    value: function choiceInfo() {
      return this.itemAnalysis.get('choiceInfo', (0, _immutable.List)());
    }
  }, {
    key: "topCorrectAnswerInfo",
    value: function topCorrectAnswerInfo() {
      return this.itemAnalysis.get('topCorrectAnswerInfo', (0, _immutable.Map)());
    }
  }, {
    key: "bottomCorrectAnswerInfo",
    value: function bottomCorrectAnswerInfo() {
      return this.itemAnalysis.get('bottomCorrectAnswerInfo', (0, _immutable.Map)());
    }
  }, {
    key: "unansweredInfo",
    value: function unansweredInfo() {
      return this.itemAnalysis.get('unansweredInfo', 0);
    }
  }, {
    key: "quintile",
    value: function quintile() {
      return this.itemAnalysis.get('quintile', (0, _immutable.List)([0, 0, 0, 0, 0])).toJS();
    }
  }, {
    key: "choiceCounts",
    value: function choiceCounts() {
      return this.choiceInfo().toJS().map(function (c) {
        return c.choiceCount;
      }).concat(this.unansweredInfo());
    }
  }, {
    key: "choiceCountMax",
    value: function choiceCountMax() {
      return (0, _d3Array.max)(this.choiceCounts());
    }
  }, {
    key: "choiceCountSum",
    value: function choiceCountSum() {
      return (0, _d3Array.sum)(this.choiceCounts());
    }
  }, {
    key: "calculateMeanScore",
    value: function calculateMeanScore(scoreInfo) {
      var sum = scoreInfo.get('resultSum') || 0;
      var count = scoreInfo.get('resultCount') || 0;
      return sum / count;
    }
  }, {
    key: "calculateDifficultyRatio",
    value: function calculateDifficultyRatio(correctAnswerInfo) {
      var correctAnswerCount = correctAnswerInfo.get('resultCount') || 0;
      var appearedCount = correctAnswerInfo.get('appearedCount') || 0;
      return correctAnswerCount / appearedCount;
    }
  }, {
    key: "formatDecimal",
    value: function formatDecimal(value, locale) {
      return isFinite(value) ? (0, _quizI18n.formatDecimal2FractionDigits)(value, locale) : null;
    }
  }, {
    key: "meanScore",
    value: function meanScore(locale) {
      return this.formatDecimal(this.calculateMeanScore(this.scoreInfo()), locale);
    }
  }, {
    key: "difficultyIndex",
    value: function difficultyIndex(locale) {
      return this.formatDecimal(this.calculateDifficultyRatio(this.correctAnswerInfo()), locale);
    }
  }, {
    key: "pointBiserial",
    value: function pointBiserial(locale) {
      var correctMean = this.calculateMeanScore(this.correctAnswerInfo());
      var incorrectMean = this.calculateMeanScore(this.incorrectAnswerInfo());
      var stdDeviation = this.scoreInfo().get('scoreStddevPop');
      var numCorrect = this.correctAnswerInfo().get('resultCount');
      var numInCorrect = this.incorrectAnswerInfo().get('resultCount');
      var totalTakers = this.correctAnswerInfo().get('appearedCount');
      var firstTerm = (correctMean - incorrectMean) / stdDeviation;
      var secondTerm = Math.sqrt(numCorrect * numInCorrect / (totalTakers * totalTakers));
      return this.formatDecimal(firstTerm * secondTerm, locale);
    }
  }, {
    key: "discriminationIndex",
    value: function discriminationIndex(locale) {
      var topDifficulty = this.calculateDifficultyRatio(this.topCorrectAnswerInfo());
      var bottomDifficulty = this.calculateDifficultyRatio(this.bottomCorrectAnswerInfo());
      return this.formatDecimal(topDifficulty - bottomDifficulty, locale);
    }
  }]);
  return ItemAnalysis;
}((0, _reduxRecord.default)({
  itemId: '0',
  quizId: '0',
  pointsPossible: '0',
  interactionType: null,
  itemBody: null,
  itemAnalysis: null,
  notFound: false,
  outcomeAlignmentSetGuid: null
}));

exports.default = ItemAnalysis;