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

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

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

var _EditButton = _interopRequireDefault(require("../../editButton/EditButton"));

var _Edit = _interopRequireDefault(require("../Edit"));

var _Show = _interopRequireDefault(require("../Show"));

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 QuizTitlePresenter = ( // ================================
// renders the editable quiz title
// input/display component
// ================================
_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(QuizTitlePresenter, _Component);

  function QuizTitlePresenter() {
    var _getPrototypeOf2;

    var _this;

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

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

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

      if (_this.props.isEditing) {
        msg = "Tittel-redigeringsmodus av.";

        _this.props.screenreaderNotification(msg);

        _this.saveAndPreview();
      } else {
        msg = "Tittelen byttet til redigeringsmodus.";

        _this.props.screenreaderNotification(msg);

        _this.props.switchOnEditing();
      }
    };

    return _this;
  }

  (0, _createClass2.default)(QuizTitlePresenter, [{
    key: "quizID",
    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 _react.default.createElement(_Edit.default, {
        isValid: this.isValid(),
        showError: (0, _partial.default)(this.props.showError, quizTitleEditKey),
        hideError: (0, _partial.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 _react.default.createElement(_Show.default, {
        switchOnEditing: this.props.switchOnEditing,
        defaultTitle: this.workingTitle()
      });
    }
  }, {
    key: "render",
    value: function render() {
      var content = this.props.isEditing ? this.renderEdit() : this.renderShow();
      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"
      }, "Tittel")), !this.props.isEditing && _react.default.createElement("div", {
        className: styles.editButtonWrapper
      }, _react.default.createElement(_EditButton.default, {
        screenreaderText: "Redigere tittel",
        scrollKey: this.props.scrollKey,
        onClick: this.toggleEditing
      }))), content);
    }
  }]);
  QuizTitlePresenter.displayName = "QuizTitlePresenter";
  return QuizTitlePresenter;
}(_react.Component), _class2.propTypes = {
  changeQuizTitle: _propTypes.default.func.isRequired,
  errorsShowing: _reactImmutableProptypes.default.map.isRequired,
  hideError: _propTypes.default.func.isRequired,
  isEditing: _propTypes.default.bool.isRequired,
  // injected by makeEditable
  onQuizUpdated: _propTypes.default.func.isRequired,
  quiz: _reactImmutableProptypes.default.record.isRequired,
  saveQuizChange: _propTypes.default.func.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  scrollKey: _propTypes.default.string,
  showError: _propTypes.default.func.isRequired,
  switchOffEditing: _propTypes.default.func.isRequired,
  // injected by makeEditable
  switchOnEditing: _propTypes.default.func.isRequired,
  // injected by makeEditable
  workingQuiz: _reactImmutableProptypes.default.record.isRequired
}, _class2.defaultProps = {
  scrollKey: void 0 // ===========
  //   helpers
  // ===========

}, _temp)) || _class);
exports.default = QuizTitlePresenter;