"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 _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

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

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

var _withI18nSupport2 = _interopRequireDefault(require("@instructure/quiz-common/lib/with-i18n-support"));

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_zhHans_number = Intl.NumberFormat("zh-Hans", { style: "decimal" }).format;

var styles = {
  componentId: 'cNnNG',
  template: function template(theme) {
    return "\n\n.cNnNG_nvdO{margin:".concat(theme.titleMarginTop || 'inherit', " ").concat(theme.titleMarginLeftRight || 'inherit', " ").concat(theme.titleMarginBottom || 'inherit', " ").concat(theme.titleMarginLeftRight || 'inherit', "}\n\n.cNnNG_bwKH{color:").concat(theme.emptyTextColor || 'inherit', ";font-size:").concat(theme.emptyTextFontSize || 'inherit', ";padding:").concat(theme.emptyTextPadding || 'inherit', ";text-align:center}\n\n[dir=ltr] .cNnNG_bwKH,[dir=rtl] .cNnNG_bwKH{text-align:center}\n\n.cNnNG_eiMu{justify-content:center}");
  },
  'title': 'cNnNG_nvdO',
  'emptyText': 'cNnNG_bwKH',
  'metric': 'cNnNG_eiMu'
};

var _ref2 =
/*#__PURE__*/
_react.default.createElement(_Spinner.default, null);

var QuizAndItemAnalysis = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_withI18nSupport) {
  (0, _inherits2.default)(QuizAndItemAnalysis, _withI18nSupport);

  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 _this = this;

      var metrics = [];
      var mean = [itemAnalysis.meanScore(this.locale) || 0, function (_ref) {
        var children = _ref.children;
        return _react.default.createElement("span", {
          key: "mean",
          className: styles.metricNote
        }, children);
      }({
        children: ["/", _default_zhHans_number(itemAnalysis.pointsPossible), "\u5206"]
      })];
      this.props.itemMetrics.forEach(function (key) {
        switch (key) {
          case 'difficultyIndex':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "difficultyIndex",
              label: "\u96BE\u5EA6\u7D22\u5F15",
              value: itemAnalysis.difficultyIndex(_this.locale),
              className: styles.metric
            }));
            break;

          case 'discriminationIndex':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "discriminationIndex",
              label: "\u9274\u522B\u6307\u6570",
              value: itemAnalysis.discriminationIndex(_this.locale),
              className: styles.metric
            }));
            break;

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

          case 'meanEarnedScore':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "meanEarnedScore",
              label: "\u83B7\u5F97\u7684\u5E73\u5747\u5206\u6570",
              value: mean,
              className: styles.metric
            }));
            break;
        }
      });
      return metrics;
    }
  }, {
    key: "generateQuizAnalysisMetrics",
    value: function generateQuizAnalysisMetrics(quizAnalysis) {
      var metrics = [];

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

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

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

          case 'meanScore':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "meanScore",
              label: "\u5E73\u5747\u5206\u6570",
              value: quizAnalysis.meanScore(),
              className: styles.metric
            }));
            break;

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

          case 'meanElapsedTime':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "meanElapsedTime",
              label: "\u7ECF\u8FC7\u7684\u5E73\u5747\u65F6\u95F4",
              value: formattedElapsedTime,
              className: styles.metric
            }));
            break;

          case 'cronbachsAlpha':
            metrics.push(_react.default.createElement(_MetricsListItem.default, {
              key: "cronbachsAlpha",
              label: "\u4E00\u81F4\u6027\u7CFB\u6570",
              value: quizAnalysis.cronbachsAlpha(),
              className: styles.metric
            }));
            break;
        }
      });
      return metrics;
    }
  }, {
    key: "renderItemAnalysisRows",
    value: function renderItemAnalysisRows() {
      var _this2 = 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"
      }, "\u9879\u76EE\u5206\u6790")), itemAnalyses.toArray().map(function (itemAnalysis, i) {
        var itemMetrics = _this2.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: _this2.props.outcomesEndpoint,
          outcomesToken: _this2.props.outcomesToken
        }, itemMetrics);
      }));
    }
  }, {
    key: "renderNotFound",
    value: function renderNotFound() {
      return _react.default.createElement(_Card.default, {
        className: styles.emptyText
      }, "\u6682\u65E0\u62A5\u544A");
    }
  }, {
    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 _ref2;
      }
    }
  }]);
  QuizAndItemAnalysis.displayName = "QuizAndItemAnalysis";
  return QuizAndItemAnalysis;
}((0, _withI18nSupport2.default)(_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,
  locale: _propTypes.default.string
}, _class2.defaultProps = {
  animateDelay: void 0,
  animateDuration: void 0,
  openRows: {},
  outcomesEndpoint: null,
  outcomesToken: null,
  quizAnalysisNotFound: false,
  quizMetrics: [],
  itemMetrics: [],
  locale: null
}, _class2.contextTypes = {
  locale: _propTypes.default.string
}, _temp)) || _class);
exports.default = QuizAndItemAnalysis;