'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;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _Heading = require('@instructure/ui-elements/lib/components/Heading');

var _Heading2 = _interopRequireDefault(_Heading);

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

var _lib2 = _interopRequireDefault(_lib);

var _Edit = require('../Edit');

var _Edit2 = _interopRequireDefault(_Edit);

var _Show = require('../Show');

var _Show2 = _interopRequireDefault(_Show);

var _EditButton = require('../../editButton/EditButton');

var _EditButton2 = _interopRequireDefault(_EditButton);

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._3BtUiBjZ01wGALXSI8q6Az {\n  display: flex;\n  justify-content: space-between;\n}\n\n._8uYoAceCVI176R1FFBanE {\n  padding: ' + theme.labelPadding + ';\n  padding-left: ' + theme.labelLeftPadding + ';\n  padding-bottom: ' + theme.labelBottomPadding + ';\n\n  font-size: ' + theme.labelFontSize + ';\n  color: ' + theme.labelColor + ';\n}\n\n._2VFS08fYKsNGVBzi1GLrju {\n  align-self: flex-end;\n  margin-right: ' + theme.editButtonWrapperRightMargin + ';\n}\n';
  },
  'topRow': '_3BtUiBjZ01wGALXSI8q6Az',
  'label': '_8uYoAceCVI176R1FFBanE',
  'editButtonWrapper': '_2VFS08fYKsNGVBzi1GLrju'
};


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

// renders the editable quiz instructions
// input/display component

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

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

  function QuizInstructions() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = QuizInstructions.__proto__ || Object.getPrototypeOf(QuizInstructions)).call.apply(_ref, [this].concat(args))), _this), _this.workingInstructions = function () {
      return _this.props.quiz.getWorkingInstance().instructions || '';
    }, _this.updateWorkingInstructions = function (event, _ref2) {
      var editorContent = _ref2.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 = void 0;
      if (_this.props.isEditing) {
        msg = (0, _formatMessage2.default)('Instructions edit mode off.');
        _this.props.screenreaderNotification(msg);
        _this.saveAndPreview();
      } else {
        msg = (0, _formatMessage2.default)('Instructions switched to edit mode. Navigate down to access text editor.');
        _this.props.screenreaderNotification(msg);
        _this.props.switchOnEditing();
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(QuizInstructions, [{
    key: 'quizID',


    // ===========
    //   helpers
    // ===========

    value: function quizID() {
      return this.props.quiz.id;
    }

    // =========
    //  actions
    // =========

  }, {
    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 _react2.default.createElement(_Edit2.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 _react2.default.createElement(_Show2.default, {
        ref: this.setRef('instructionsPreview'),
        instructions: this.props.quiz.instructions,
        switchOnEditing: this.props.switchOnEditing
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { tabIndex: '-1' },
        _react2.default.createElement(
          'div',
          { className: styles.topRow },
          _react2.default.createElement(
            'div',
            { className: styles.label },
            _react2.default.createElement(
              _Heading2.default,
              { level: 'h4', as: 'h2' },
              (0, _formatMessage2.default)('Instructions')
            )
          ),
          _react2.default.createElement(
            'div',
            { className: styles.editButtonWrapper },
            _react2.default.createElement(_EditButton2.default, {
              screenreaderText: (0, _formatMessage2.default)('Edit Instructions'),
              scrollKey: this.props.scrollKey,
              onClick: this.toggleEditing
            })
          )
        ),
        this.editingRender(),
        this.showRender()
      );
    }
  }]);
  QuizInstructions.displayName = 'QuizInstructions'
  ;
  return QuizInstructions;
}(_react.Component), _class2.propTypes = {
  changeQuizInstructions: _propTypes2.default.func.isRequired,
  clearModifications: _propTypes2.default.func.isRequired,
  isEditing: _propTypes2.default.bool.isRequired, // Injected by makeEditable
  quiz: _reactImmutableProptypes2.default.record.isRequired,
  saveQuizChange: _propTypes2.default.func.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired,
  scrollKey: _propTypes2.default.string,
  switchOffEditing: _propTypes2.default.func.isRequired, // Injected by makeEditable
  switchOnEditing: _propTypes2.default.func.isRequired // Injected by makeEditable
}, _class2.defaultProps = {
  scrollKey: void 0 }, _temp2)) || _class);
exports.default = QuizInstructions;