'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _Button2 = _interopRequireDefault(_Button);

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

var _lib2 = _interopRequireDefault(_lib);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _RichContentInput2 = _interopRequireDefault(_RichContentInput);

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

var _componentWithOutsideClick2 = _interopRequireDefault(_componentWithOutsideClick);

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

var _CustomPropTypes2 = _interopRequireDefault(_CustomPropTypes);

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

var _ElementsForSelectors2 = _interopRequireDefault(_ElementsForSelectors);

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

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

var _theme2 = _interopRequireDefault(_theme);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.Edit__styles__instructionsEditWrapper {\n  position: relative;\n  width: 100%;\n  min-height: 4rem;\n  box-sizing: border-box;\n  padding: ' + theme.instructionsEditWrapperPadding + ';\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.Edit__styles__footer {\n  margin-top: ' + theme.footerTopMargin + ';\n  display: flex;\n  justify-content: flex-end;\n}\n\n.Edit__styles__instructionsEdit {}\n\n.Edit__styles__instructionsEditInput {}\n';
  },
  'instructionsEditWrapper': 'Edit__styles__instructionsEditWrapper',
  'footer': 'Edit__styles__footer',
  'instructionsEdit': 'Edit__styles__instructionsEdit',
  'instructionsEditInput': 'Edit__styles__instructionsEditInput'
};


// ===================================

// once editing is toggled this div
// is rendered & has actions that
// allow for canceling

// ===================================

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

  function QuizInstructionsEdit() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = QuizInstructionsEdit.__proto__ || Object.getPrototypeOf(QuizInstructionsEdit)).call.apply(_ref, [this].concat(args))), _this), _this.handleRef = function (node) {
      _this.rce = node;
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.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, _ElementsForSelectors2.default)(this.props.appContainer);
      return appNodes.every(function (node) {
        return node && !node.contains(e.target);
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: styles.instructionsEditWrapper },
        _react2.default.createElement(_RichContentInput2.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
        }),
        _react2.default.createElement(
          'div',
          { className: styles.footer },
          _react2.default.createElement(
            _Button2.default,
            { margin: '0 x-small', onClick: this.props.endEditing },
            'Annuler'
          ),
          _react2.default.createElement(
            _Button2.default,
            { variant: 'primary', onClick: this.props.saveAndPreview, 'data-automation': 'instructions-done' },
            'Termin\xE9'
          )
        )
      );
    }
  }]);
  QuizInstructionsEdit.displayName = 'QuizInstructionsEdit'
  ;
  return QuizInstructionsEdit;
}(_react.Component), _class2.propTypes = {
  endEditing: _propTypes2.default.func.isRequired,
  inputValue: _propTypes2.default.string.isRequired,
  onInputChange: _propTypes2.default.func.isRequired,
  quizId: _propTypes2.default.string.isRequired,
  saveAndPreview: _propTypes2.default.func.isRequired,
  appContainer: _CustomPropTypes2.default.selectors.isRequired
}, _temp2)) || _class);
exports.default = (0, _componentWithOutsideClick2.default)(QuizInstructionsEdit);