"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-i18n/lib/format-message"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'cqGrr',
  template: function template(theme) {
    return "\n\n.cqGrr_qYhL{display:flex;justify-content:space-between}\n\n.cqGrr_blJt{color:".concat(theme.labelColor || 'inherit', ";font-size:").concat(theme.labelFontSize || 'inherit', ";padding:").concat(theme.labelPadding || 'inherit', ";padding-bottom:").concat(theme.labelBottomPadding || 'inherit', ";padding-left:").concat(theme.labelLeftPadding || 'inherit', "}\n\n.cqGrr_UraS{align-self:flex-end;margin-right:").concat(theme.editButtonWrapperRightMargin || 'inherit', "}");
  },
  'topRow': 'cqGrr_qYhL',
  'label': 'cqGrr_blJt',
  'editButtonWrapper': 'cqGrr_UraS'
};
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 = "Modd golygu cyfarwyddiadau wedi\u2019i ddiffodd.";

        _this.props.screenreaderNotification(msg);

        _this.saveAndPreview();
      } else {
        msg = "Cyfarwyddiadau wedi\u2019u newid i\u2019r modd golygu. Ewch i lawr i gael mynediad at y golygydd testun.";

        _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"
      }, "Cyfarwyddiadau")), !this.props.isEditing && _react.default.createElement("div", {
        className: styles.editButtonWrapper
      }, _react.default.createElement(_EditButton.default, {
        screenreaderText: "Golygu Cyfarwyddiadau",
        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;