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

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

var _reduxRecord2 = _interopRequireDefault(_reduxRecord);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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

// TODO change the d3 dependency in package.json to match specific modules
var ItemAnalysis = function (_ReduxRecord) {
  (0, _inherits3.default)(ItemAnalysis, _ReduxRecord);

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

  (0, _createClass3.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) {
      return isFinite(value) ? _formatMessage2.default.number(value, 'decimal_2_fraction_digits') : null;
    }
  }, {
    key: 'meanScore',
    value: function meanScore() {
      return this.formatDecimal(this.calculateMeanScore(this.scoreInfo()));
    }
  }, {
    key: 'difficultyIndex',
    value: function difficultyIndex() {
      return this.formatDecimal(this.calculateDifficultyRatio(this.correctAnswerInfo()));
    }
  }, {
    key: 'pointBiserial',
    value: function pointBiserial() {
      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);
    }
  }, {
    key: 'discriminationIndex',
    value: function discriminationIndex() {
      var topDifficulty = this.calculateDifficultyRatio(this.topCorrectAnswerInfo());
      var bottomDifficulty = this.calculateDifficultyRatio(this.bottomCorrectAnswerInfo());
      return this.formatDecimal(topDifficulty - bottomDifficulty);
    }
  }]);
  return ItemAnalysis;
}((0, _reduxRecord2.default)({
  itemId: '0',
  quizId: '0',
  pointsPossible: '0',
  interactionType: null,
  itemBody: null,
  itemAnalysis: null,
  notFound: false,
  outcomeAlignmentSetGuid: null
})); /* eslint "import/no-extraneous-dependencies": "warn" */


exports.default = ItemAnalysis;