"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 _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

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

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

var _RichContentInput = _interopRequireDefault(require("../../../../../../common/components/RichContentInput"));

var _componentWithOutsideClick = _interopRequireDefault(require("../../../../../../common/components/shared/functionality/componentWithOutsideClick"));

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

var _ElementsForSelectors = _interopRequireDefault(require("../../../../../../common/util/ElementsForSelectors"));

var _constants = require("@instructure/quiz-common/lib/constants");

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

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'evnrd',
  template: function template(theme) {
    return "\n\n.evnrd_dCmx{box-sizing:border-box;display:flex;flex-direction:column;justify-content:center;min-height:4rem;padding:".concat(theme.instructionsEditWrapperPadding || 'inherit', ";position:relative;width:100%}\n\n.evnrd_cjWK{display:flex;justify-content:flex-end;margin-top:").concat(theme.footerTopMargin || 'inherit', "}");
  },
  'instructionsEditWrapper': 'evnrd_dCmx',
  'footer': 'evnrd_cjWK'
};
// ===================================
// once editing is toggled this div
// is rendered & has actions that
// allow for canceling
// ===================================
var QuizInstructionsEdit = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(QuizInstructionsEdit, _Component);

  function QuizInstructionsEdit() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.handleRef = function (node) {
      _this.rce = node;
    };

    return _this;
  }

  (0, _createClass2.default)(QuizInstructionsEdit, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.rce.getWrappedInstance().focus();
    } // enabled by wrapWithOutsideClick

  }, {
    key: "clickedOutside",
    value: function clickedOutside() {
      this.props.saveAndPreview();
    } // enabled by wrapWithOutsideClick

  }, {
    key: "ignoreClickOutsideIf",
    value: function ignoreClickOutsideIf(e) {
      var appNodes = (0, _ElementsForSelectors.default)(this.props.appContainer);
      return appNodes.every(function (node) {
        return node && !node.contains(e.target);
      });
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        className: styles.instructionsEditWrapper
      }, _react.default.createElement(_RichContentInput.default, {
        ref: this.handleRef,
        textareaId: _constants.INSTRUCTIONS_EDIT_ID,
        wrapperClassName: styles.instructionsEdit,
        textareaClassName: styles.instructionsEditInput,
        defaultContent: this.props.inputValue,
        onKeyUp: this.props.onInputChange,
        onChange: this.props.onInputChange,
        parentType: _constants.PARENT_TYPE_QUIZ,
        parentId: this.props.quizId
      }), _react.default.createElement("div", {
        className: styles.footer
      }, _react.default.createElement(_Button.default, {
        margin: "0 x-small",
        onClick: this.props.endEditing
      }, "Canslo"), _react.default.createElement(_Button.default, {
        variant: "primary",
        onClick: this.props.saveAndPreview,
        "data-automation": "instructions-done"
      }, "Wedi gorffen")));
    }
  }]);
  QuizInstructionsEdit.displayName = "QuizInstructionsEdit";
  return QuizInstructionsEdit;
}(_react.Component), _class2.propTypes = {
  endEditing: _propTypes.default.func.isRequired,
  inputValue: _propTypes.default.string.isRequired,
  onInputChange: _propTypes.default.func.isRequired,
  quizId: _propTypes.default.string.isRequired,
  saveAndPreview: _propTypes.default.func.isRequired,
  appContainer: _CustomPropTypes.default.selectors.isRequired
}, _temp)) || _class);

var _default = (0, _componentWithOutsideClick.default)(QuizInstructionsEdit);

exports.default = _default;