"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 _getPrototypeOf3 = _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 _Text = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Text"));

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

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

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

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

var _ModerateTableRow = _interopRequireDefault(require("../ModerateTableRow"));

var _ModerateTray = _interopRequireDefault(require("../../sidebar/ModerateTray"));

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

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

var _ResetQuiz = _interopRequireDefault(require("../../sidebar/ResetQuiz"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'esFtE',
  template: function template(theme) {
    return "\n\n.esFtE_bwKH{display:block;margin:".concat(theme.emptyTextMargin || 'inherit', " 0 0 0}\n\n.esFtE_xNcP{background-color:").concat(theme.pageBackgroundColor || 'inherit', ";flex:1;overflow:auto}");
  },
  'emptyText': 'esFtE_bwKH',
  'page': 'esFtE_xNcP'
};

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

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

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

  function ModerateTable() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, ModerateTable);

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

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(ModerateTable)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.state = {
      moderatePanelIterations: 0,
      selectedStudent: null
    };

    _this.onModerateButtonClick = function (data) {
      _this.setState({
        selectedStudent: data.id
      });

      _this.props.onOpenTray(data);

      _this.props.openTray(data);
    };

    _this.onCloseTray = function () {
      _this.props.onCloseTray();

      _this.setState({
        moderatePanelIterations: _this.state.moderatePanelIterations + 1
      });
    };

    return _this;
  }

  (0, _createClass2.default)(ModerateTable, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          hasSessionTimeLimit = _this$props.hasSessionTimeLimit,
          sessionTimeLimitInSeconds = _this$props.sessionTimeLimitInSeconds;
      this.props.sendSessionTimeLimit({
        hasSessionTimeLimit: hasSessionTimeLimit,
        sessionTimeLimitInSeconds: sessionTimeLimitInSeconds
      });
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this$props2 = this.props,
          hasSessionTimeLimit = _this$props2.hasSessionTimeLimit,
          sessionTimeLimitInSeconds = _this$props2.sessionTimeLimitInSeconds;

      if (prevProps.hasSessionTimeLimit !== hasSessionTimeLimit || prevProps.sessionTimeLimitInSeconds !== sessionTimeLimitInSeconds) {
        this.props.sendSessionTimeLimit({
          hasSessionTimeLimit: hasSessionTimeLimit,
          sessionTimeLimitInSeconds: sessionTimeLimitInSeconds
        });
      }
    }
  }, {
    key: "renderTableRows",
    // =============
    //   RENDERING
    // =============
    value: function renderTableRows() {
      var _this2 = this;

      var _this$props3 = this.props,
          onModerateLogClick = _this$props3.onModerateLogClick,
          onResultsLinkClick = _this$props3.onResultsLinkClick,
          shouldRenderModerateButton = _this$props3.shouldRenderModerateButton;
      return this.props.tableData.map(function (rowData) {
        var id = rowData.id;
        return _react.default.createElement(_ModerateTableRow.default, {
          onResultsLinkClick: onResultsLinkClick,
          onAccommodationsClick: _this2.props.onAccommodationsClick,
          key: "".concat(id, "-").concat(_this2.state.moderatePanelIterations) // eslint-disable-line react/jsx-no-literals
          ,
          onModerateButtonClick: _this2.onModerateButtonClick,
          onModerateLogClick: onModerateLogClick,
          rowData: rowData,
          shouldRenderModerateButton: shouldRenderModerateButton,
          focusTarget: _this2.state.selectedStudent
        });
      });
    }
  }, {
    key: "renderTable",
    value: function renderTable() {
      return _react.default.createElement(_Table.default, {
        caption: _react.default.createElement(_ScreenReaderContent.default, null, "Tabella di moderazione")
      }, _react.default.createElement("thead", null, _react.default.createElement("tr", null, _react.default.createElement("th", null, "Studente"), _react.default.createElement("th", null, "Tentativi"), _react.default.createElement("th", null, "Punteggio"), _react.default.createElement("th", null, "Ora"), _react.default.createElement("th", null, "Registro"), this.props.onAccommodationsClick && _react.default.createElement("th", null, "Sistemazioni"), this.props.shouldRenderModerateButton && _react.default.createElement("th", null, _react.default.createElement(_ScreenReaderContent.default, null, "Modera")))), _react.default.createElement("tbody", null, this.renderTableRows()));
    }
  }, {
    key: "renderContent",
    value: function renderContent() {
      var _this$props4 = this.props,
          isFetching = _this$props4.isFetching,
          tableData = _this$props4.tableData,
          emptyMessage = _this$props4.emptyMessage;
      if (isFetching) return _ref;
      if (tableData.length) return this.renderTable();
      return _react.default.createElement("span", {
        className: styles.emptyText
      }, _react.default.createElement(_Text.default, null, emptyMessage));
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        className: styles.page
      }, _react.default.createElement(_ModerateTray.default, {
        onClose: this.onCloseTray
      }, _ref2, this.props.children), this.renderContent());
    }
  }]);
  ModerateTable.displayName = "ModerateTable";
  return ModerateTable;
}(_react.Component), _class2.propTypes = {
  children: _propTypes.default.node,
  emptyMessage: _propTypes.default.string,
  isFetching: _propTypes.default.bool.isRequired,
  onCloseTray: _propTypes.default.func,
  onModerateLogClick: _propTypes.default.func.isRequired,
  onAccommodationsClick: _propTypes.default.func,
  onOpenTray: _propTypes.default.func,
  onResultsLinkClick: _propTypes.default.func,
  openTray: _propTypes.default.func.isRequired,
  sessionTimeLimitInSeconds: _propTypes.default.number,
  hasSessionTimeLimit: _propTypes.default.bool,
  sendSessionTimeLimit: _propTypes.default.func,
  shouldRenderModerateButton: _propTypes.default.bool,
  tableData: _propTypes.default.arrayOf(_propTypes.default.shape({
    avatar: _propTypes.default.string,
    id: _propTypes.default.string.isRequired,
    name: _propTypes.default.string.isRequired,
    quizSessionIds: _propTypes.default.arrayOf(_propTypes.default.string).isRequired,
    maxAttempts: _propTypes.default.number,
    accommodations: _propTypes.default.arrayOf(_propTypes.default.shape({
      name: _propTypes.default.string.isRequired,
      value: _propTypes.default.string.isRequired
    }))
  })).isRequired
}, _class2.defaultProps = {
  emptyMessage: "Non sono ancora presenti informazioni di moderazione da visualizzare.",
  onCloseTray: function onCloseTray() {},
  onOpenTray: function onOpenTray() {},
  sendSessionTimeLimit: function sendSessionTimeLimit() {},
  sessionTimeLimitInSeconds: null,
  hasSessionTimeLimit: false,
  shouldRenderModerateButton: true,
  onAccommodationsClick: null //To support voiceover when the table changes

}, _temp)) || _class);
exports.default = ModerateTable;