'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 _dec,
    _class,
    _class2,
    _temp2,
    _default_it_number = Intl.NumberFormat('it', { style: 'decimal' }).format;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react3 = require('format-message/react');

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

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _immutable = require('immutable');

var _Alert = require('@instructure/ui-alerts/lib/components/Alert');

var _Alert2 = _interopRequireDefault(_Alert);

var _Button = require('@instructure/ui-buttons/lib/components/Button');

var _Button2 = _interopRequireDefault(_Button);

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

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

var _Text = require('@instructure/ui-elements/lib/components/Text');

var _Text2 = _interopRequireDefault(_Text);

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

var _lib2 = _interopRequireDefault(_lib);

var _CustomPropTypes = require('../../../../../common/util/CustomPropTypes');

var _CustomPropTypes2 = _interopRequireDefault(_CustomPropTypes);

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

var _Spinner2 = _interopRequireDefault(_Spinner);

var _componentForItem = require('../../../../../common/util/componentForItem');

var _componentForItem2 = _interopRequireDefault(_componentForItem);

var _constants = require('../../../../../constants');

var _interactionTypePropsHelper = require('../../../../../common/util/interactionTypePropsHelper');

var _RegradingModal = require('../../../RegradingModal');

var _RegradingModal2 = _interopRequireDefault(_RegradingModal);

var _presenter = require('../../../../../building/components/resources/quizEntry/QuizEntryEdit/presenter');

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

var _theme2 = _interopRequireDefault(_theme);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.RegradeOverride__styles__container {\n  position: relative;\n  padding: ' + theme.containerPadding + ';\n}\n\n.RegradeOverride__styles__alert {\n  display: block;\n}\n\n';
  },
  'container': 'RegradeOverride__styles__container',
  'alert': 'RegradeOverride__styles__alert'
};


var noop = function noop() {};

var RegradeOverride = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(RegradeOverride, _Component);

  function RegradeOverride() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = RegradeOverride.__proto__ || Object.getPrototypeOf(RegradeOverride)).call.apply(_ref, [this].concat(args))), _this), _this.navigateToTop = function () {
      setTimeout(function () {
        _this.props.screenreaderNotification('Stato attivo spostato sulla parte superiore dell\'elemento. Spostati in basso per continuare.');
        _this.alertNode.focus();
      });
    }, _this.turnOffRegradingEditMode = function () {
      _this.props.clearModifications('items', _this.props.itemId);
      _this.props.clearModifications('quizEntries', _this.props.sessionItem.quizEntryId);
      _this.props.toggleRegradingEditMode('');
      _this.props.onCancel(_this.props.sessionItem.id);
    }, _this.triggerRegradingModal = function () {
      var _this$props$workingEn = _this.props.workingEntry,
          scoringAlgorithm = _this$props$workingEn.scoringAlgorithm,
          scoringData = _this$props$workingEn.scoringData;
      var _this$props$workingQu = _this.props.workingQuizEntry,
          regradeAdjustedPointsPossible = _this$props$workingQu.regradeAdjustedPointsPossible,
          pointsPossible = _this$props$workingQu.pointsPossible;


      _this.props.setRegradingPayload({
        itemId: _this.props.itemId,
        quizEntryId: _this.props.quizEntry.id,
        pointsPossible: regradeAdjustedPointsPossible || pointsPossible,
        scoringAlgorithm: scoringAlgorithm,
        scoringData: scoringData
      });
      _this.props.openModal(_constants.REGRADE_MODAL);
    }, _this.setAlertRef = function (node) {
      _this.alertNode = node;
    }, _this.renderActionFooter = function () {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _Button2.default,
          {
            margin: 'small',
            onClick: _this.turnOffRegradingEditMode
          },
          'Annulla'
        ),
        _react2.default.createElement(
          _Button2.default,
          {
            margin: 'small',
            variant: 'primary',
            onClick: _this.triggerRegradingModal,
            'data-automation': 'sdk-trigger-regrade-button'
          },
          (0, _react3.formatChildren)('Rivaluta <0>elemento ' + _default_it_number(_this.props.quizEntry.position) + '</0>', [_react2.default.createElement(_ScreenReaderContent2.default, { key: '0' })])
        )
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(RegradeOverride, [{
    key: 'quizEntryProps',
    value: function quizEntryProps() {
      return Object.assign({}, this.props, {
        alternativeRightFooter: this.renderActionFooter,
        changeItemTitle: noop,
        clearModifications: noop,
        clearTemporaryItem: noop,
        cloneQuizEntry: noop,
        connectDragPreview: noop,
        connectDragSource: noop,
        createItem: noop,
        createQuizEntry: noop,
        disabledForRegrade: true,
        draggingAboveHalf: false,
        draggingOver: false,
        hideError: noop,
        isOccluded: false,
        itemFeedbackOn: false,
        name: '',
        openModal: noop,
        openRegradeModal: noop,
        quizEntries: (0, _immutable.List)(),
        removeQuizEntry: noop,
        screenreaderNotification: noop,
        setUi: noop,
        shouldClone: false,
        showError: noop,
        switchOffEditing: noop,
        updateItem: noop,
        updateQuizEntry: noop,
        withConfirm: noop
      });
    }
  }, {
    key: 'itemEditProps',
    value: function itemEditProps() {
      return Object.assign({}, this.props, {
        openImportModal: function openImportModal() {},
        parentId: this.props.quizId,
        parentType: _constants.PARENT_TYPE_QUIZ,
        renderImportModal: noop,
        title: this.props.entry.title,
        workingItem: this.props.workingEntry
      });
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      if (this.props.quizEntry.id === '0') {
        this.props.getQuizEntry(this.props.quizId, this.props.sessionItem.quizEntryId);
      }

      if (!this.props.entry.isLoaded()) {
        this.props.getItem(this.props.itemId);
      } else {
        this.navigateToTop();
      }
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      if (!prevProps.entry.isLoaded() && this.props.entry.isLoaded()) {
        this.navigateToTop();
      }
    }
  }, {
    key: 'renderRegradingAlert',
    value: function renderRegradingAlert() {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _Alert2.default,
          {
            margin: 'small 0',
            transition: 'none',
            variant: 'info'
          },
          _react2.default.createElement(
            'div',
            { className: styles.alert },
            (0, _react3.formatChildren)('<0>La rivalutazione si applica solo alle consegne completate.</0><1>Se sono interessati tutti gli studenti, attendi tutte le consegne prima di eseguire la rivalutazione.</1>', [_react2.default.createElement(_Text2.default, { as: 'div', weight: 'bold', key: '0' }), _react2.default.createElement(_Text2.default, { as: 'span', key: '1' })])
          )
        ),
        _react2.default.createElement(
          _Alert2.default,
          {
            margin: 'small 0',
            transition: 'none',
            variant: 'info'
          },
          _react2.default.createElement(
            'div',
            { className: styles.alert },
            (0, _react3.formatChildren)('<0>Questo elemento \xE8 in fase di rivalutazione.</0>\n              <1>Opzioni di rivalutazione per questo tipo di elemento:</1>\n              <2>\n                <3>modifica la risposta corretta</3>\n                <4>modifica il valore dei punti per l\'intero elemento</4>\n                <5>modifica i valori dei punti nelle singole risposte (se applicabile)</5>\n              </2>', [_react2.default.createElement(_Text2.default, { as: 'div', weight: 'bold', key: '0' }), _react2.default.createElement(_Text2.default, { as: 'span', key: '1' }), _react2.default.createElement('ul', { key: '2' }), _react2.default.createElement('li', { key: '3' }), _react2.default.createElement('li', { key: '4' }), _react2.default.createElement('li', { key: '5' })])
          )
        )
      );
    }
  }, {
    key: 'renderEditableView',
    value: function renderEditableView() {
      var propsForInteraction = (0, _interactionTypePropsHelper.propsForInteractionEdit)(this.itemEditProps());

      var interactionType = this.props.entry.getInteractionType().slug;
      var Interaction = (0, _componentForItem2.default)(interactionType, _constants.COMPONENT_EDIT);

      return _react2.default.createElement(
        _presenter.UndecoratedQuizEntryEdit,
        this.quizEntryProps(),
        this.renderRegradingAlert(),
        _react2.default.createElement(
          'div',
          { className: styles.container },
          _react2.default.createElement(Interaction, Object.assign({}, propsForInteraction, {
            itemId: this.props.sessionItem.id,
            overrideEditableForRegrading: true
          }))
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { tabIndex: '-1', ref: this.setAlertRef },
        _react2.default.createElement(_RegradingModal2.default, {
          fromSpeedgrader: true,
          quizId: this.props.quizId
        }),
        this.props.quizEntry.id !== '0' ? this.renderEditableView() : _react2.default.createElement(_Spinner2.default, null)
      );
    }
  }]);
  RegradeOverride.displayName = 'RegradeOverride'
  ;
  return RegradeOverride;
}(_react.Component), _class2.propTypes = {
  appContainer: _CustomPropTypes2.default.selectors.isRequired,
  entry: _reactImmutableProptypes2.default.record.isRequired,
  workingEntry: _reactImmutableProptypes2.default.record.isRequired,
  clearModifications: _propTypes2.default.func.isRequired,
  getItem: _propTypes2.default.func.isRequired,
  getQuizEntry: _propTypes2.default.func.isRequired,
  itemId: _propTypes2.default.string.isRequired,
  onCancel: _propTypes2.default.func.isRequired,
  openModal: _propTypes2.default.func.isRequired,
  quizEntry: _reactImmutableProptypes2.default.record.isRequired,
  workingQuizEntry: _reactImmutableProptypes2.default.record.isRequired,
  quizId: _propTypes2.default.string.isRequired,
  sessionItem: _reactImmutableProptypes2.default.record.isRequired,
  setRegradingPayload: _propTypes2.default.func.isRequired,
  toggleRegradingEditMode: _propTypes2.default.func.isRequired,

  // required by propsForInteractionEdit
  displayPosition: _propTypes2.default.number.isRequired,
  addError: _propTypes2.default.func.isRequired,
  changeItemState: _propTypes2.default.func.isRequired,
  changeQuizEntryPoints: _propTypes2.default.func.isRequired,
  changeQuizEntryProperties: _propTypes2.default.func.isRequired,
  errorsAreShowing: _propTypes2.default.bool,
  interactionFileUpload: _propTypes2.default.func.isRequired,
  parentId: _propTypes2.default.string.isRequired,
  parentType: _propTypes2.default.string.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired
}, _class2.defaultProps = {
  errorsAreShowing: false
}, _temp2)) || _class);
exports.default = RegradeOverride;