"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.TitleEdit = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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 _reactDom = _interopRequireDefault(require("react-dom"));

var _defer = _interopRequireDefault(require("lodash/defer"));

var _classnames = _interopRequireDefault(require("classnames"));

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

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

var _Errors = _interopRequireDefault(require("../../../../../../common/components/shared/errors/Errors"));

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

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'fDDFj',
  template: function template(theme) {
    return "\n\n.quiz-core__Edit--titleEditWrapper {\n  position: relative;\n  width: 100%;\n  height: 5rem;\n\n  border-bottom: solid;\n  border-width: ".concat(theme.titleEditWrapperBorderWidth || 'inherit', ";\n  border-color: ").concat(theme.titleEditWrapperBorderColor || 'inherit', ";\n  box-sizing: border-box;\n\n  padding-left: ").concat(theme.titleEditWrapperLeftPadding || 'inherit', ";\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.quiz-core__Edit--titleEditInput {\n  font-size: ").concat(theme.titleEditInputFontSize || 'inherit', ";\n  width: 80%;\n}\n\n.quiz-core__Edit--inputWrapper,\n.quiz-core__Edit--errorsWrapper {\n  width: 100%;\n  flex: 1 1 auto;\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n}\n\n.quiz-core__Edit--errorBorder {\n  margin: ").concat(theme.errorBorderMargin || 'inherit', ";\n  padding: ").concat(theme.errorBorderPadding || 'inherit', ";\n  border: solid 1px ").concat(theme.errorBorderColor || 'inherit', ";\n  border-radius: ").concat(theme.errorBorderBorderRadius || 'inherit', ";\n}\n");
  },
  'titleEditWrapper': 'quiz-core__Edit--titleEditWrapper',
  'titleEditInput': 'quiz-core__Edit--titleEditInput',
  'inputWrapper': 'quiz-core__Edit--inputWrapper',
  'errorsWrapper': 'quiz-core__Edit--errorsWrapper',
  'errorBorder': 'quiz-core__Edit--errorBorder'
};
// =================================
// once editing is toggled this div
// is rendered & has actions that
// allow for canceling
// =================================
var TitleEdit = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(TitleEdit, _Component);

  function TitleEdit() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.defer = function (fn) {
      // to enable syncronous testing
      var deferFn = _this.props.deferMock || _defer.default;
      deferFn(fn);
    };

    _this.switchOffEditing = function () {
      _this.ifValidElseError(function () {
        _this.defer(function () {
          // make sure they have actually left the component
          if (document.activeElement !== _this.cancelButton && document.activeElement !== _this.input) {
            _this.props.saveAndPreview();
          }
        });
      });
    };

    return _this;
  }

  (0, _createClass2.default)(TitleEdit, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var input = _reactDom.default.findDOMNode(this.input); // some browsers add chars so * 2 to ensure at end


      var valLength = input.value.length * 2;
      input.focus();
      input.setSelectionRange(valLength, valLength);
    }
  }, {
    key: "ifValidElseError",
    value: function ifValidElseError(cb) {
      if (this.props.isValid) {
        this.props.hideError();
        cb();
      } else {
        this.props.showError();
      }
    }
  }, {
    key: "setRef",
    value: function setRef(key) {
      var _this2 = this;

      return function (node) {
        _this2[key] = node;
      };
    }
  }, {
    key: "hasErrors",
    value: function hasErrors() {
      return this.props.errorsAreShowing && this.props.errors.size > 0;
    }
  }, {
    key: "renderErrors",
    value: function renderErrors() {
      if (this.hasErrors()) {
        return _react.default.createElement("div", {
          className: styles.errorsWrapper
        }, _react.default.createElement(_Errors.default, {
          errorList: this.props.errors
        }));
      }
    }
  }, {
    key: "inputClasses",
    value: function inputClasses() {
      var _classNames;

      return (0, _classnames.default)((_classNames = {}, (0, _defineProperty2.default)(_classNames, styles.titleEditInput, true), (0, _defineProperty2.default)(_classNames, styles.errorBorder, this.hasErrors()), _classNames));
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        ref: this.setRef('wrapper'),
        className: styles.titleEditWrapper,
        onBlur: this.switchOffEditing,
        "data-automation": "sdk-quiz-title-edit-wrapper"
      }, _react.default.createElement("div", {
        className: styles.inputWrapper
      }, _react.default.createElement("input", {
        ref: this.setRef('input'),
        type: "text",
        "aria-label": "titel",
        placeholder: "L\xE4gg till en rubrik...",
        value: this.props.value,
        onKeyDown: this.props.onInputKeyDown,
        onChange: this.props.onInputChange,
        className: this.inputClasses()
      })), this.renderErrors());
    }
  }]);
  TitleEdit.displayName = "TitleEdit";
  return TitleEdit;
}(_react.Component), _class2.propTypes = {
  value: _propTypes.default.string.isRequired,
  isValid: _propTypes.default.bool.isRequired,
  showError: _propTypes.default.func.isRequired,
  hideError: _propTypes.default.func.isRequired,
  errors: _reactImmutableProptypes.default.list.isRequired,
  errorsAreShowing: _propTypes.default.bool,
  onInputChange: _propTypes.default.func.isRequired,
  onInputKeyDown: _propTypes.default.func.isRequired,
  saveAndPreview: _propTypes.default.func.isRequired,
  deferMock: _propTypes.default.func
}, _class2.defaultProps = {
  errorsAreShowing: false,
  deferMock: _defer.default // todo: extract this into a shared method
  // to be used on many components

}, _temp)) || _class);
exports.TitleEdit = TitleEdit;
var _default = TitleEdit;
exports.default = _default;