"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 _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

var _CloseButton = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/CloseButton"));

var _TextInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/TextInput"));

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

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

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

var _Bank = _interopRequireDefault(require("../../../common/records/Bank"));

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

var _instUIMessages = require("../../../common/util/instUIMessages");

var _Modal = _interopRequireWildcard(require("@instructure/quiz-common/lib/components/Modal"));

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

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'fTWio',
  template: function template(theme) {
    return "\n\n.fTWio_detV{flex:1 1 auto;font-size:".concat(theme.bankNameInputFontSize || 'inherit', ";padding:").concat(theme.bankNamePadding || 'inherit', ";text-overflow:ellipsis;white-space:nowrap}\n\n.fTWio_rOsj{padding:").concat(theme.bankNameLabelPadding || 'inherit', "}");
  },
  'bankNameInput': 'fTWio_detV',
  'bankNameLabel': 'fTWio_rOsj'
};
var CreateBankModal = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(CreateBankModal, _Component);

  function CreateBankModal() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.continueAction = function () {
      _this.props.showError(_this.props.errorKey);

      if (_this.props.temporaryBank.isValid()) {
        _this.props.createBank(_this.props.temporaryBank);

        _this.closeAction();
      } else {
        _this.props.screenreaderNotification("Der Name des Pools darf nicht leer sein.");
      }
    };

    _this.closeAction = function () {
      _this.props.hideError(_this.props.errorKey);

      _this.props.clearModifications('banks', _this.props.temporaryBank.id);

      if (_this.props.modalTrigger) {
        _this.props.modalTrigger.focus();
      }

      _this.props.closeModal();
    };

    _this.onNameChange = function (e) {
      _this.props.showError(_this.props.errorKey);

      _this.props.changeBank(_this.props.temporaryBank.id, {
        title: e.target.value
      });
    };

    _this.getErrors = function () {
      if (_this.props.displayError) {
        return (0, _instUIMessages.toErrors)(_this.props.temporaryBank.errorsFor('title').toJS());
      }
    };

    return _this;
  }

  (0, _createClass2.default)(CreateBankModal, [{
    key: "cancelButton",
    value: function cancelButton() {
      return _react.default.createElement(_Button.default, {
        onClick: this.closeAction,
        className: styles.footerButtonWrapper,
        margin: _constants.XSMALL_SIDE_MARGIN
      }, "Abbrechen");
    }
  }, {
    key: "createBankButton",
    value: function createBankButton() {
      return _react.default.createElement(_Button.default, {
        type: "button",
        variant: "primary",
        onClick: this.continueAction,
        margin: _constants.XSMALL_SIDE_MARGIN,
        "data-automation": "sdk-create-bank-button"
      }, "Einen Pool erstellen");
    }
  }, {
    key: "renderContent",
    value: function renderContent() {
      return _react.default.createElement("div", {
        className: styles.bankNameInput
      }, _react.default.createElement(_TextInput.default, {
        messages: this.getErrors(),
        onChange: this.onNameChange,
        label: _react.default.createElement("div", {
          className: styles.bankNameLabel
        }, "Poolname"),
        type: "text",
        placeholder: "Geben Sie einen Namen ein\xA0...",
        "data-automation": "sdk-create-bank-name-textinput"
      }));
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement(_Modal.default, {
        open: this.props.modalOpen,
        onDismiss: this.closeAction,
        size: "small",
        label: "Objekt-Pool",
        "data-automation": "sdk-create-bank-modal"
      }, _react.default.createElement(_Modal.ModalHeader, null, _react.default.createElement(_Heading.default, {
        level: "reset",
        as: "h2"
      }, "Neuer Objektpool"), _react.default.createElement(_CloseButton.default, {
        onClick: this.closeAction,
        placement: "end",
        offset: "medium",
        variant: "icon"
      }, "Schlie\xDFen")), _react.default.createElement(_Modal.ModalBody, null, this.renderContent()), _react.default.createElement(_Modal.ModalFooter, null, this.cancelButton(), this.createBankButton()));
    }
  }]);
  CreateBankModal.displayName = "CreateBankModal";
  return CreateBankModal;
}(_react.Component), _class2.propTypes = {
  modalOpen: _propTypes.default.bool.isRequired,
  modalTrigger: _propTypes.default.instanceOf(_react.Component),
  temporaryBank: _propTypes.default.instanceOf(_Bank.default).isRequired,
  displayError: _propTypes.default.bool,
  errorKey: _propTypes.default.string.isRequired,
  createBank: _propTypes.default.func.isRequired,
  changeBank: _propTypes.default.func.isRequired,
  clearModifications: _propTypes.default.func.isRequired,
  showError: _propTypes.default.func.isRequired,
  hideError: _propTypes.default.func.isRequired,
  closeModal: _propTypes.default.func.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired
}, _class2.defaultProps = {
  displayError: false,
  modalTrigger: null
}, _temp)) || _class);
exports.default = CreateBankModal;