'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 _partial = require('lodash/partial');

var _partial2 = _interopRequireDefault(_partial);

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 _EditButton = require('../../editButton/EditButton');

var _EditButton2 = _interopRequireDefault(_EditButton);

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

var _Edit2 = _interopRequireDefault(_Edit);

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

var _Show2 = _interopRequireDefault(_Show);

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._1w2nucGUN32c4rGc89JsO7 {\n  display: flex;\n  justify-content: space-between;\n}\n\n._1Y2toczqWqWY3A15qYRwgW {\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.MJk8lMtKfPkQf7rD1B39j {\n  align-self: flex-end;\n  margin-right: ' + theme.editButtonWrapperRightMargin + ';\n}\n';
  },
  'topRow': '_1w2nucGUN32c4rGc89JsO7',
  'label': '_1Y2toczqWqWY3A15qYRwgW',
  'editButtonWrapper': 'MJk8lMtKfPkQf7rD1B39j'
};


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

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

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

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

  function QuizTitlePresenter() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = QuizTitlePresenter.__proto__ || Object.getPrototypeOf(QuizTitlePresenter)).call.apply(_ref, [this].concat(args))), _this), _this.workingTitle = function () {
      return _this.props.workingQuiz.title;
    }, _this.saveAndPreview = function () {
      _this.saveWorkingTitle();
      _this.props.onQuizUpdated({ id: _this.quizID(), title: _this.props.workingQuiz.title });
      _this.props.switchOffEditing();
    }, _this.updateWorkingTitle = function (e) {
      _this.props.changeQuizTitle(_this.quizID(), e.target.value);
    }, _this.handleInput = function (e) {
      if (e.keyCode === 13) {
        return _this.saveAndPreview();
      }
    }, _this.toggleEditing = function () {
      var msg = void 0;
      if (_this.props.isEditing) {
        msg = (0, _formatMessage2.default)('Title edit mode off.');
        _this.props.screenreaderNotification(msg);
        _this.saveAndPreview();
      } else {
        msg = (0, _formatMessage2.default)('Title switched to edit mode.');
        _this.props.screenreaderNotification(msg);
        _this.props.switchOnEditing();
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

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


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

    value: function quizID() {
      return this.props.quiz.id;
    }
  }, {
    key: 'titleErrors',
    value: function titleErrors() {
      return this.props.workingQuiz.errorsFor('title');
    }
  }, {
    key: 'isValid',
    value: function isValid() {
      return this.props.workingQuiz.isValid();
    }

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

  }, {
    key: 'saveWorkingTitle',
    value: function saveWorkingTitle() {
      this.props.saveQuizChange(this.props.workingQuiz, this.props.quiz);
    }
  }, {
    key: 'renderEdit',


    // ===============
    //    rendering
    // ===============

    value: function renderEdit() {
      var quizTitleEditKey = 'quiz_title_edit';
      return _react2.default.createElement(_Edit2.default, {
        isValid: this.isValid(),
        showError: (0, _partial2.default)(this.props.showError, quizTitleEditKey),
        hideError: (0, _partial2.default)(this.props.hideError, quizTitleEditKey),
        errorsAreShowing: this.props.errorsShowing.get(quizTitleEditKey),
        errors: this.titleErrors(),
        value: this.workingTitle(),
        onInputKeyDown: this.handleInput,
        onInputChange: this.updateWorkingTitle,
        saveAndPreview: this.saveAndPreview
      });
    }
  }, {
    key: 'renderShow',
    value: function renderShow() {
      return _react2.default.createElement(_Show2.default, {
        switchOnEditing: this.props.switchOnEditing,
        defaultTitle: this.workingTitle()
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var content = this.props.isEditing ? this.renderEdit() : this.renderShow();
      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)('Title')
            )
          ),
          _react2.default.createElement(
            'div',
            { className: styles.editButtonWrapper },
            _react2.default.createElement(_EditButton2.default, {
              screenreaderText: (0, _formatMessage2.default)('Edit Title'),
              scrollKey: this.props.scrollKey,
              onClick: this.toggleEditing
            })
          )
        ),
        content
      );
    }
  }]);
  QuizTitlePresenter.displayName = 'QuizTitlePresenter'
  ;
  return QuizTitlePresenter;
}(_react.Component), _class2.propTypes = {
  changeQuizTitle: _propTypes2.default.func.isRequired,
  errorsShowing: _reactImmutableProptypes2.default.map.isRequired,
  hideError: _propTypes2.default.func.isRequired,
  isEditing: _propTypes2.default.bool.isRequired, // injected by makeEditable
  onQuizUpdated: _propTypes2.default.func.isRequired,
  quiz: _reactImmutableProptypes2.default.record.isRequired,
  saveQuizChange: _propTypes2.default.func.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired,
  scrollKey: _propTypes2.default.string,
  showError: _propTypes2.default.func.isRequired,
  switchOffEditing: _propTypes2.default.func.isRequired, // injected by makeEditable
  switchOnEditing: _propTypes2.default.func.isRequired, // injected by makeEditable
  workingQuiz: _reactImmutableProptypes2.default.record.isRequired
}, _class2.defaultProps = {
  scrollKey: void 0 }, _temp2)) || _class);
exports.default = QuizTitlePresenter;