'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

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 _dec, _class, _class2, _temp2; /* eslint "import/no-extraneous-dependencies": "warn" */


// TODO change the d3 dependency in package.json to match specific modules


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _immutable = require('immutable');

var _d3Scale = require('d3-scale');

var _IconXSolid = require('instructure-icons/lib/Solid/IconXSolid');

var _IconXSolid2 = _interopRequireDefault(_IconXSolid);

var _IconCheckSolid = require('instructure-icons/lib/Solid/IconCheckSolid');

var _IconCheckSolid2 = _interopRequireDefault(_IconCheckSolid);

var _Table = require('@instructure/ui-elements/lib/components/Table');

var _Table2 = _interopRequireDefault(_Table);

var _ScreenReaderContent = require('@instructure/ui-a11y/lib/components/ScreenReaderContent');

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

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

var _lib2 = _interopRequireDefault(_lib);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _extractTextFromHtml = require('quiz-interactions/lib/util/extractTextFromHtml');

var _extractTextFromHtml2 = _interopRequireDefault(_extractTextFromHtml);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n@keyframes AnswerFrequencySummary__styles__contentAnimation {\n  to {\n    opacity: 1;\n    transform: translate3d(0, 0, 0);\n  }\n}\n\n.AnswerFrequencySummary__styles__content {\n  opacity: 0.01;\n  transform: translate3d(0, 0, 0);\n  animation-name: AnswerFrequencySummary__styles__contentAnimation;\n  animation-fill-mode: forwards;\n  animation-duration: 0.3s;\n}\n\n.AnswerFrequencySummary__styles__content th,\n  .AnswerFrequencySummary__styles__content td {\n    border-bottom-color: ' + theme.contentCellBorderColor + ';\n    padding-left: 0.35rem;\n    padding-right: 0.35rem;\n  }\n\n.AnswerFrequencySummary__styles__answerRow th,\n  .AnswerFrequencySummary__styles__answerRow td {\n    padding-top: ' + theme.answerRowCellPadding + ';\n    padding-bottom: ' + theme.answerRowCellPadding + ';\n    white-space: nowrap;\n  }\n\n.AnswerFrequencySummary__styles__answerRowCorrect {\n  background-color: ' + theme.answerRowCorrectBackgroundColor + ';\n}\n\n.AnswerFrequencySummary__styles__answerRowCorrect .AnswerFrequencySummary__styles__answerIcon {\n    color: ' + theme.answerRowAnswerIconColor + ';\n  }\n\n.AnswerFrequencySummary__styles__answerRowCorrect .AnswerFrequencySummary__styles__answerTextfield,\n  .AnswerFrequencySummary__styles__answerRowCorrect .AnswerFrequencySummary__styles__answerRatio {\n    font-weight: ' + theme.answerRowCorrectTextFieldFontWeight + ';\n  }\n\n.AnswerFrequencySummary__styles__answerRowCorrect .AnswerFrequencySummary__styles__bar {\n    background: ' + theme.answerRowCorrectBarBackgroundColor + ';\n  }\n\ntd.AnswerFrequencySummary__styles__answerIcon {\n  font-size: ' + theme.answerIconFontSize + ';\n  color: ' + theme.answerIconColor + ';\n  width: 1%;\n}\n\nth.AnswerFrequencySummary__styles__answerTextfield {\n  font-weight: ' + theme.answerTextfieldFontWeight + ';\n  overflow: hidden;\n  max-width: 12rem;\n}\n\n.AnswerFrequencySummary__styles__respondentLink {\n  text-align: right;\n  width: 1%;\n}\n\n[dir="ltr"] .AnswerFrequencySummary__styles__respondentLink {\n  text-align: right;\n}\n\n[dir="rtl"] .AnswerFrequencySummary__styles__respondentLink {\n  text-align: right;\n}\n\ntd.AnswerFrequencySummary__styles__respondentLink {\n  color: ' + theme.respondentLinkColor + ';\n}\n\ntd.AnswerFrequencySummary__styles__respondentLinkPresent {\n  color: ' + theme.respondentLinkPresentColor + ';\n}\n\n.AnswerFrequencySummary__styles__answerRatio {\n  text-align: right;\n  width: 1%;\n}\n\n[dir="ltr"] .AnswerFrequencySummary__styles__answerRatio {\n  text-align: right;\n}\n\n[dir="rtl"] .AnswerFrequencySummary__styles__answerRatio {\n  text-align: right;\n}\n\ntd.AnswerFrequencySummary__styles__answerRatio {\n  padding-left: ' + theme.answerRatioPaddingLeft + ';\n}\n\ntd.AnswerFrequencySummary__styles__answerDistributionCell {\n  border-bottom-color: transparent;\n  padding: 0;\n  width: 7rem;\n  min-width: 7rem;\n  height: 2.3rem;\n}\n\n.AnswerFrequencySummary__styles__bar {\n  background: ' + theme.barBackgroundColor + ';\n  transition: width 0.5s;\n  position: relative;\n  min-width: ' + theme.barMinWidth + ';\n  height: 100%;\n}\n\n@media screen and (--phoneBreakPoint) {\n  th.AnswerFrequencySummary__styles__answerTextfield {\n    max-width: 0;\n  }\n\n  th.AnswerFrequencySummary__styles__respondentLink {\n    max-width: 3rem;\n    overflow: hidden;\n    text-overflow: ellipsis;\n  }\n}\n';
  },
  'content': 'AnswerFrequencySummary__styles__content',
  'contentAnimation': 'AnswerFrequencySummary__styles__contentAnimation',
  'answerRow': 'AnswerFrequencySummary__styles__answerRow',
  'answerRowCorrect': 'AnswerFrequencySummary__styles__answerRowCorrect',
  'answerIcon': 'AnswerFrequencySummary__styles__answerIcon',
  'answerTextfield': 'AnswerFrequencySummary__styles__answerTextfield',
  'answerRatio': 'AnswerFrequencySummary__styles__answerRatio',
  'bar': 'AnswerFrequencySummary__styles__bar',
  'respondentLink': 'AnswerFrequencySummary__styles__respondentLink',
  'respondentLinkPresent': 'AnswerFrequencySummary__styles__respondentLinkPresent',
  'answerDistributionCell': 'AnswerFrequencySummary__styles__answerDistributionCell'
};
var AnswerFrequencySummary = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(AnswerFrequencySummary, _Component);

  function AnswerFrequencySummary() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, AnswerFrequencySummary);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = AnswerFrequencySummary.__proto__ || Object.getPrototypeOf(AnswerFrequencySummary)).call.apply(_ref, [this].concat(args))), _this), _this.renderAnswerTableRow = function (choiceInfo, choiceIndex) {
      var _classNames, _classNames2;

      var choiceSum = _this.props.itemAnalysis.choiceCountSum();
      var choiceBody = _this.renderChoiceBody(choiceInfo);
      var rowClassNames = (0, _classnames2.default)((_classNames = {}, (0, _defineProperty3.default)(_classNames, styles.answerRow, true), (0, _defineProperty3.default)(_classNames, styles.answerRowCorrect, _this.isCorrect(choiceInfo)), _classNames));
      var respondentClassNames = (0, _classnames2.default)((_classNames2 = {}, (0, _defineProperty3.default)(_classNames2, styles.respondentLink, true), (0, _defineProperty3.default)(_classNames2, styles.respondentLinkPresent, choiceInfo.get('choiceCount') > 0), _classNames2));

      return _react2.default.createElement(
        'tr',
        { key: choiceIndex, className: rowClassNames },
        _react2.default.createElement(
          'td',
          { className: styles.answerIcon },
          _this.isCorrect(choiceInfo) ? _react2.default.createElement(_IconCheckSolid2.default, null) : _react2.default.createElement(_IconXSolid2.default, null),
          _react2.default.createElement(
            _ScreenReaderContent2.default,
            null,
            _this.getBarTitle(choiceInfo)
          )
        ),
        _react2.default.createElement(
          'th',
          { scope: 'row', className: styles.answerTextfield },
          _react2.default.createElement(
            'span',
            { title: choiceBody },
            choiceBody
          )
        ),
        _react2.default.createElement(
          'td',
          { className: respondentClassNames },
          _formatMessage2.default.number(Math.floor(choiceInfo.get('choiceCount')))
        ),
        _react2.default.createElement(
          'td',
          { className: styles.answerRatio },
          choiceSum > 0 ? _formatMessage2.default.number(choiceInfo.get('choiceCount') / choiceSum, 'percent') : 0
        ),
        _react2.default.createElement(
          'td',
          { className: styles.answerDistributionCell, 'aria-hidden': 'true' },
          _this.renderBarPlot(choiceInfo)
        )
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(AnswerFrequencySummary, [{
    key: 'getBarStyles',
    value: function getBarStyles(choiceInfo) {
      var highest = this.props.itemAnalysis.choiceCountMax();
      var xScale = (0, _d3Scale.scaleLinear)() // linear scale against domain, range
      .domain([highest, 0]) // bounds
      .range([100, 0]); // to percent of column width
      return {
        width: xScale(choiceInfo.get('choiceCount')) + '%'
      };
    }
  }, {
    key: 'isCorrect',
    value: function isCorrect(choiceInfo) {
      return choiceInfo !== this.unansweredChoiceInfo && choiceInfo.get('correct');
    }
  }, {
    key: 'getBarTitle',
    value: function getBarTitle(choiceInfo) {
      return this.isCorrect(choiceInfo) ? 'Resposta correta' : 'Resposta Incorreta';
    }
  }, {
    key: 'renderBarPlot',
    value: function renderBarPlot(choiceInfo) {
      return _react2.default.createElement('div', {
        className: styles.bar,
        style: this.getBarStyles(choiceInfo),
        alt: 'Gr\xE1fico de barras',
        title: this.getBarTitle(choiceInfo)
      });
    }
  }, {
    key: 'renderChoiceBody',
    value: function renderChoiceBody(choiceInfo) {
      var text = (0, _extractTextFromHtml2.default)(choiceInfo.get('itemBody'));
      if (text.length > 0) return text;
      return this.getBarTitle(choiceInfo);
    }
  }, {
    key: 'render',
    value: function render() {
      var choiceInfo = this.props.itemAnalysis.choiceInfo();
      this.unansweredChoiceInfo = (0, _immutable.fromJS)({
        itemBody: '(Sem resposta)',
        choiceCount: this.props.itemAnalysis.unansweredInfo()
      });

      return _react2.default.createElement(
        'div',
        { className: styles.content },
        _react2.default.createElement(
          _Table2.default,
          {
            caption: _react2.default.createElement(
              _ScreenReaderContent2.default,
              null,
              'Resumo da frequ\xEAncia da resposta'
            )
          },
          _react2.default.createElement(
            'thead',
            null,
            _react2.default.createElement(
              'tr',
              null,
              _react2.default.createElement(
                'th',
                null,
                _react2.default.createElement(
                  _ScreenReaderContent2.default,
                  null,
                  'Est\xE1 correto?'
                )
              ),
              _react2.default.createElement(
                'th',
                null,
                'Resposta'
              ),
              _react2.default.createElement(
                'th',
                { className: styles.respondentLink },
                'Respondentes'
              ),
              _react2.default.createElement(
                'th',
                { className: styles.answerRatio },
                '%'
              )
            )
          ),
          _react2.default.createElement(
            'tbody',
            null,
            choiceInfo.toArray().map(this.renderAnswerTableRow),
            this.renderAnswerTableRow(this.unansweredChoiceInfo, choiceInfo.size)
          )
        )
      );
    }
  }]);
  AnswerFrequencySummary.displayName = 'AnswerFrequencySummary'
  ;
  return AnswerFrequencySummary;
}(_react.Component), _class2.propTypes = {
  itemAnalysis: _reactImmutableProptypes2.default.record.isRequired
}, _temp2)) || _class);
exports.default = AnswerFrequencySummary;