"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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 _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _react2 = require("format-message/react");

var _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

var _lib = _interopRequireDefault(require("@instructure/ui-themeable/lib"));

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

var _Spinner = _interopRequireDefault(require("../../../../common/components/shared/spinner/Spinner"));

var _Card = _interopRequireDefault(require("../../../../common/components/shared/Card"));

var _FormattedDuration = _interopRequireDefault(require("../../../../common/components/shared/FormattedDuration"));

var _ItemAnalysisRow = _interopRequireDefault(require("../../../../reporting/components/itemAnalysis/ItemAnalysisRow"));

var _QuizAnalysisOverview = _interopRequireDefault(require("../../../../reporting/components/quizAnalysis/QuizAnalysisOverview"));

var _MetricsListItem = _interopRequireDefault(require("../../MetricsListItem"));

var _theme = _interopRequireDefault(require("./theme"));

var _dec,
    _class,
    _class2,
    _temp,
    _default_de_number = Intl.NumberFormat("de", { style: "decimal" }).format;

var styles = {
  componentId: 'cNnNG',
  template: function template(theme) {
    return "\n\n.QuizAndItemAnalysis__styles__title {\n  margin: ".concat(theme.titleMarginTop || 'inherit', " ").concat(theme.titleMarginLeftRight || 'inherit', " ").concat(theme.titleMarginBottom || 'inherit', " ").concat(theme.titleMarginLeftRight || 'inherit', ";\n}\n\n.QuizAndItemAnalysis__styles__emptyText {\n  padding: ").concat(theme.emptyTextPadding || 'inherit', ";\n  font-size: ").concat(theme.emptyTextFontSize || 'inherit', ";\n  text-align: center;\n  color: ").concat(theme.emptyTextColor || 'inherit', ";\n}\n\n[dir=\"ltr\"] .QuizAndItemAnalysis__styles__emptyText {\n  text-align: center;\n}\n\n[dir=\"rtl\"] .QuizAndItemAnalysis__styles__emptyText {\n  text-align: center;\n}\n\n.QuizAndItemAnalysis__styles__metric {\n  justify-content: center;\n}\n");
  },
  'title': 'QuizAndItemAnalysis__styles__title',
  'emptyText': 'QuizAndItemAnalysis__styles__emptyText',
  'metric': 'QuizAndItemAnalysis__styles__metric'
};
var QuizAndItemAnalysis = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(QuizAndItemAnalysis, _Component);

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

  (0, _createClass2.default)(QuizAndItemAnalysis, [{
    key: "getAnalyses",
    value: function getAnalyses(activeQuizId) {
      this.props.getExistingQuiz(activeQuizId);
      this.props.getItemAnalyses(activeQuizId);
      this.props.getQuizAnalysis(activeQuizId);
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getAnalyses(this.props.activeQuizId);
    }
  }, {
    key: "componentWillUpdate",
    value: function componentWillUpdate(nextProps) {
      if (this.props.activeQuizId !== nextProps.activeQuizId) {
        this.getAnalyses(nextProps.activeQuizId);
      }
    }
  }, {
    key: "generateItemAnalysisMetrics",
    value: function generateItemAnalysisMetrics(itemAnalysis) {
      var metrics = [];
      var mean = (0, _react2.formatChildren)((itemAnalysis.meanScore() || 0) + "<0>/" + _default_de_number(itemAnalysis.pointsPossible) + "\xA0Pkte.</0>", [_react.default.createElement("span", {
        key: "mean",
        className: styles.metricNote
      })]);
      this.props.itemMetrics.forEach(function (key) {
        switch (key) {
          case 'difficultyIndex':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "difficultyIndex",
              label: "Schwierigkeitsindex",
              value: itemAnalysis.difficultyIndex(),
              className: styles.metric
            }));
            break;

          case 'discriminationIndex':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "discriminationIndex",
              label: "Unterscheidungsindex",
              value: itemAnalysis.discriminationIndex(),
              className: styles.metric
            }));
            break;

          case 'rpb':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "rpb",
              label: "RPB",
              value: itemAnalysis.pointBiserial(),
              className: styles.metric
            }));
            break;

          case 'meanEarnedScore':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "meanEarnedScore",
              label: "Durchschnittlich erreichte Punktzahl",
              value: mean,
              className: styles.metric
            }));
            break;
        }
      });
      return metrics;
    }
  }, {
    key: "generateQuizAnalysisMetrics",
    value: function generateQuizAnalysisMetrics(quizAnalysis) {
      var metrics = [];

      var formattedElapsedTime = _react.default.createElement(_FormattedDuration.default, {
        timeLimitInSeconds: quizAnalysis.meanElapsedTime()
      });

      this.props.quizMetrics.forEach(function (key) {
        switch (key) {
          case 'highScore':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "highScore",
              label: "H\xF6chste Punktzahl",
              value: quizAnalysis.highScore(),
              className: styles.metric
            }));
            break;

          case 'lowScore':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "lowScore",
              label: "Niedrigste Punktzahl",
              value: quizAnalysis.lowScore(),
              className: styles.metric
            }));
            break;

          case 'meanScore':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "meanScore",
              label: "Durchschnittliches Ergebnis",
              value: quizAnalysis.meanScore(),
              className: styles.metric
            }));
            break;

          case 'standardDeviation':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "standardDeviation",
              label: "Standardabweichung",
              value: quizAnalysis.standardDeviation(),
              className: styles.metric
            }));
            break;

          case 'meanElapsedTime':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "meanElapsedTime",
              label: "Durchschnittlich verstrichene Zeit",
              value: formattedElapsedTime,
              className: styles.metric
            }));
            break;

          case 'cronbachsAlpha':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "cronbachsAlpha",
              label: "Cronbachs Alpha",
              value: quizAnalysis.cronbachsAlpha(),
              className: styles.metric
            }));
            break;
        }
      });
      return metrics;
    }
  }, {
    key: "renderItemAnalysisRows",
    value: function renderItemAnalysisRows() {
      var _this = this;

      var _this$props = this.props,
          animateDelay = _this$props.animateDelay,
          animateDuration = _this$props.animateDuration,
          quiz = _this$props.quiz,
          openRows = _this$props.openRows,
          activeQuizId = _this$props.activeQuizId,
          toggleItemAnalysisRowExpanded = _this$props.toggleItemAnalysisRowExpanded;
      var itemAnalyses = quiz.getItemAnalyses();

      if (itemAnalyses.size <= 0) {
        return;
      }

      return _react.default.createElement("div", null, _react.default.createElement("div", {
        className: styles.title
      }, _react.default.createElement(_Heading.default, {
        level: "h3",
        as: "h2"
      }, "Objektanalyse")), itemAnalyses.toArray().map(function (itemAnalysis, i) {
        var itemMetrics = _this.generateItemAnalysisMetrics(itemAnalysis);

        return _react.default.createElement(_ItemAnalysisRow.default, {
          animateDelay: animateDelay,
          animateDuration: animateDuration,
          expanded: openRows[itemAnalysis.itemId],
          itemAnalysis: itemAnalysis,
          itemId: itemAnalysis.itemId,
          key: itemAnalysis.itemId,
          position: i + 1,
          quizId: activeQuizId,
          toggleExpanded: toggleItemAnalysisRowExpanded,
          outcomesEndpoint: _this.props.outcomesEndpoint,
          outcomesToken: _this.props.outcomesToken
        }, itemMetrics);
      }));
    }
  }, {
    key: "renderNotFound",
    value: function renderNotFound() {
      return _react.default.createElement(_Card.default, {
        className: styles.emptyText
      }, "Bisher keine Berichte");
    }
  }, {
    key: "renderContent",
    value: function renderContent() {
      var _this$props2 = this.props,
          animateDelay = _this$props2.animateDelay,
          animateDuration = _this$props2.animateDuration,
          quiz = _this$props2.quiz,
          quizAnalysis = _this$props2.quizAnalysis;
      var quizMetrics = this.generateQuizAnalysisMetrics(quizAnalysis);
      return _react.default.createElement("div", null, _react.default.createElement(_QuizAnalysisOverview.default, {
        animateDelay: animateDelay,
        animateDuration: animateDuration,
        quizAnalysis: quizAnalysis,
        quizTitle: quiz.title
      }, quizMetrics), quizAnalysis.isPresent() && this.renderItemAnalysisRows());
    }
  }, {
    key: "render",
    value: function render() {
      if (this.props.quizAnalysisNotFound) {
        return this.renderNotFound();
      } else if (this.props.quiz.isLoaded() && this.props.quizAnalysis && this.props.quizAnalysis.isLoaded()) {
        return this.renderContent();
      } else {
        return _react.default.createElement(_Spinner.default, null);
      }
    }
  }]);
  QuizAndItemAnalysis.displayName = "QuizAndItemAnalysis";
  return QuizAndItemAnalysis;
}(_react.Component), _class2.propTypes = {
  animateDelay: _propTypes.default.number,
  animateDuration: _propTypes.default.number,
  activeQuizId: _propTypes.default.string.isRequired,
  openRows: _propTypes.default.objectOf(_propTypes.default.bool),
  outcomesEndpoint: _propTypes.default.string,
  outcomesToken: _propTypes.default.string,
  quiz: _reactImmutableProptypes.default.record.isRequired,
  quizAnalysis: _reactImmutableProptypes.default.record.isRequired,
  quizAnalysisNotFound: _propTypes.default.bool,
  quizMetrics: _propTypes.default.arrayOf(_propTypes.default.oneOf(['highScore', 'lowScore', 'meanScore', 'standardDeviation', 'meanElapsedTime', 'cronbachsAlpha'])),
  itemMetrics: _propTypes.default.arrayOf(_propTypes.default.oneOf(['difficultyIndex', 'discriminationIndex', 'rpb', 'meanEarnedScore'])),
  toggleItemAnalysisRowExpanded: _propTypes.default.func.isRequired,
  getExistingQuiz: _propTypes.default.func.isRequired,
  getItemAnalyses: _propTypes.default.func.isRequired,
  getQuizAnalysis: _propTypes.default.func.isRequired
}, _class2.defaultProps = {
  animateDelay: void 0,
  animateDuration: void 0,
  openRows: {},
  outcomesEndpoint: null,
  outcomesToken: null,
  quizAnalysisNotFound: false,
  quizMetrics: [],
  itemMetrics: []
}, _temp)) || _class);
exports.default = QuizAndItemAnalysis;