"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 _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 _Edit = _interopRequireDefault(require("../Edit"));

var _Show = _interopRequireDefault(require("../Show"));

var _EditButton = _interopRequireDefault(require("../../editButton/EditButton"));

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

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

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'cqGrrKyhIP',
  template: function template(theme) {
    return "/*  imported from styles.css  */\n\n.Instructions__styles__topRow {\n  display: flex;\n  justify-content: space-between;\n}\n\n.Instructions__styles__label {\n  padding: ".concat(theme.labelPadding || 'none', ";\n  padding-left: ").concat(theme.labelLeftPadding || 'none', ";\n  padding-bottom: ").concat(theme.labelBottomPadding || 'none', ";\n\n  font-size: ").concat(theme.labelFontSize || 'none', ";\n  color: ").concat(theme.labelColor || 'none', ";\n}\n\n.Instructions__styles__editButtonWrapper {\n  align-self: flex-end;\n  margin-right: ").concat(theme.editButtonWrapperRightMargin || 'none', ";\n}\n");
  },
  'topRow': 'Instructions__styles__topRow',
  'label': 'Instructions__styles__label',
  'editButtonWrapper': 'Instructions__styles__editButtonWrapper'
};
var QuizInstructions = ( // ======================================
// renders the editable quiz instructions
// input/display component
// ======================================
_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(QuizInstructions, _Component);

  function QuizInstructions() {
    var _getPrototypeOf2;

    var _this;

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

    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)(QuizInstructions)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.workingInstructions = function () {
      return _this.props.quiz.getWorkingInstance().instructions || '';
    };

    _this.updateWorkingInstructions = function (event, _ref) {
      var editorContent = _ref.editorContent;

      _this.props.changeQuizInstructions(_this.quizID(), editorContent);
    };

    _this.saveAndPreview = function () {
      _this.saveWorkingInstructions();

      _this.endEditing();
    };

    _this.endEditing = function () {
      _this.props.switchOffEditing();

      _this.props.clearModifications('quizzes', _this.quizID(), ['instructions']);
    };

    _this.toggleEditing = function () {
      var msg;

      if (_this.props.isEditing) {
        msg = "Instructions edit mode off.";

        _this.props.screenreaderNotification(msg);

        _this.saveAndPreview();
      } else {
        msg = "Instructions switched to edit mode. Navigate down to access text editor.";

        _this.props.screenreaderNotification(msg);

        _this.props.switchOnEditing();
      }
    };

    return _this;
  }

  (0, _createClass2.default)(QuizInstructions, [{
    key: "quizID",
    value: function quizID() {
      return this.props.quiz.id;
    }
  }, {
    key: "saveWorkingInstructions",
    value: function saveWorkingInstructions() {
      this.props.saveQuizChange(this.props.quiz.getWorkingInstance(), this.props.quiz);
    }
  }, {
    key: "setRef",
    value: function setRef(key) {
      var _this2 = this;

      return function (node) {
        _this2[key] = node;
      };
    } // ===============
    //    RENDERING
    // ===============

  }, {
    key: "editingRender",
    value: function editingRender() {
      if (!this.props.isEditing) {
        return;
      }

      return _react.default.createElement(_Edit.default, {
        endEditing: this.endEditing,
        inputValue: this.workingInstructions(),
        onInputChange: this.updateWorkingInstructions,
        ref: this.setRef('instructionsEdit'),
        saveAndPreview: this.saveAndPreview,
        quizId: this.props.quiz.id
      });
    }
  }, {
    key: "showRender",
    value: function showRender() {
      if (this.props.isEditing) {
        return;
      }

      return _react.default.createElement(_Show.default, {
        ref: this.setRef('instructionsPreview'),
        instructions: this.props.quiz.instructions,
        switchOnEditing: this.props.switchOnEditing
      });
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        tabIndex: "-1"
      }, _react.default.createElement("div", {
        className: styles.topRow
      }, _react.default.createElement("div", {
        className: styles.label
      }, _react.default.createElement(_Heading.default, {
        level: "h4",
        as: "h2"
      }, "Instructions")), !this.props.isEditing && _react.default.createElement("div", {
        className: styles.editButtonWrapper
      }, _react.default.createElement(_EditButton.default, {
        screenreaderText: "Edit Instructions",
        scrollKey: this.props.scrollKey,
        onClick: this.toggleEditing
      }))), this.editingRender(), this.showRender());
    }
  }]);
  QuizInstructions.displayName = "QuizInstructions";
  return QuizInstructions;
}(_react.Component), _class2.propTypes = {
  changeQuizInstructions: _propTypes.default.func.isRequired,
  clearModifications: _propTypes.default.func.isRequired,
  isEditing: _propTypes.default.bool.isRequired,
  // Injected by makeEditable
  quiz: _reactImmutableProptypes.default.record.isRequired,
  saveQuizChange: _propTypes.default.func.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  scrollKey: _propTypes.default.string,
  switchOffEditing: _propTypes.default.func.isRequired,
  // Injected by makeEditable
  switchOnEditing: _propTypes.default.func.isRequired // Injected by makeEditable

}, _class2.defaultProps = {
  scrollKey: void 0 // ===========
  //   helpers
  // ===========

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