"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 _instUIMessages = require("../../../common/util/instUIMessages");

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

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

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

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

var _dec, _class, _class2, _temp;

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

  function EditBankModal() {
    var _getPrototypeOf2;

    var _this;

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

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

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

      if (_this.props.workingBank.isValid()) {
        _this.props.updateBank(_this.props.workingBank);

        _this.closeAction();
      }
    };

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

      _this.props.clearModifications('banks', _this.props.workingBank.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.workingBank.id, {
        title: e.target.value
      });
    };

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

    return _this;
  }

  (0, _createClass2.default)(EditBankModal, [{
    key: "renderCancelButton",
    value: function renderCancelButton() {
      return _react.default.createElement(_Button.default, {
        onClick: this.closeAction,
        className: styles.footerButtonWrapper,
        margin: _constants.XSMALL_SIDE_MARGIN
      }, "Peruuta");
    }
  }, {
    key: "renderSaveButton",
    value: function renderSaveButton() {
      return _react.default.createElement(_Button.default, {
        type: "button",
        variant: "primary",
        onClick: this.continueAction,
        margin: _constants.XSMALL_SIDE_MARGIN,
        "data-automation": "sdk-edit-bank-save-button"
      }, "Tallenna muutokset");
    }
  }, {
    key: "renderContent",
    value: function renderContent() {
      return _react.default.createElement("div", {
        className: styles.bankNameInput
      }, _react.default.createElement(_TextInput.default, {
        value: this.props.workingBank.title,
        messages: this.getErrors(),
        onChange: this.onNameChange,
        label: _react.default.createElement("div", {
          className: styles.bankNameLabel
        }, "Pankin nimi"),
        type: "text",
        placeholder: "Sy\xF6t\xE4 nimi...",
        "data-automation": "sdk-edit-bank-name-textinput"
      }));
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement(_Modal.default, {
        open: this.props.modalOpen,
        onDismiss: this.closeAction,
        size: "small",
        label: "Kohteen pankki",
        "data-automation": "sdk-edit-bank-modal"
      }, _react.default.createElement(_Modal.ModalHeader, null, _react.default.createElement(_Heading.default, {
        level: "reset",
        as: "h2"
      }, "Muokkaa kohteen pankkia"), _react.default.createElement(_CloseButton.default, {
        onClick: this.closeAction,
        placement: "end",
        offset: "medium",
        variant: "icon"
      }, "Sulje")), _react.default.createElement(_Modal.ModalBody, null, this.renderContent()), _react.default.createElement(_Modal.ModalFooter, null, this.renderCancelButton(), this.renderSaveButton()));
    }
  }]);
  EditBankModal.displayName = "EditBankModal";
  return EditBankModal;
}(_react.Component), _class2.propTypes = {
  modalOpen: _propTypes.default.bool.isRequired,
  modalTrigger: _propTypes.default.instanceOf(Element),
  workingBank: _propTypes.default.instanceOf(_Bank.default).isRequired,
  displayError: _propTypes.default.bool,
  errorKey: _propTypes.default.string.isRequired,
  updateBank: _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
}, _class2.defaultProps = {
  displayError: false,
  modalTrigger: null
}, _temp)) || _class);
exports.default = EditBankModal;