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

var _Button2 = _interopRequireDefault(_Button);

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

var _CloseButton2 = _interopRequireDefault(_CloseButton);

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

var _TextInput2 = _interopRequireDefault(_TextInput);

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 _formatMessage = require('../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _Bank2 = _interopRequireDefault(_Bank);

var _constants = require('../../../constants');

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

var _Modal = require('../../../common/components/Modal');

var _Modal2 = _interopRequireDefault(_Modal);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.CreateBankModal__styles__bankNameInput {\n  flex: 1 1 auto;\n  font-size: ' + theme.bankNameInputFontSize + ';\n  white-space: nowrap;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  padding: ' + theme.bankNamePadding + ';\n}\n\n.CreateBankModal__styles__bankNameLabel {\n  padding: ' + theme.bankNameLabelPadding + ';\n}\n';
  },
  'bankNameInput': 'CreateBankModal__styles__bankNameInput',
  'bankNameLabel': 'CreateBankModal__styles__bankNameLabel'
};
var CreateBankModal = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(CreateBankModal, _Component);

  function CreateBankModal() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = CreateBankModal.__proto__ || Object.getPrototypeOf(CreateBankModal)).call.apply(_ref, [this].concat(args))), _this), _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('\u041D\u0435\u043E\u0431\u0445\u043E\u0434\u0438\u043C\u043E \u0443\u043A\u0430\u0437\u0430\u0442\u044C \u043D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u0431\u0430\u043D\u043A\u0430.');
      }
    }, _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());
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(CreateBankModal, [{
    key: 'cancelButton',
    value: function cancelButton() {
      return _react2.default.createElement(
        _Button2.default,
        {
          onClick: this.closeAction,
          className: styles.footerButtonWrapper,
          margin: _constants.XSMALL_SIDE_MARGIN
        },
        '\u041E\u0442\u043C\u0435\u043D\u0438\u0442\u044C'
      );
    }
  }, {
    key: 'createBankButton',
    value: function createBankButton() {
      return _react2.default.createElement(
        _Button2.default,
        {
          type: 'button',
          variant: 'primary',
          onClick: this.continueAction,
          margin: _constants.XSMALL_SIDE_MARGIN,
          'data-automation': 'sdk-create-bank-button'
        },
        '\u0421\u043E\u0437\u0434\u0430\u0442\u044C \u0431\u0430\u043D\u043A'
      );
    }
  }, {
    key: 'renderContent',
    value: function renderContent() {
      return _react2.default.createElement(
        'div',
        { className: styles.bankNameInput },
        _react2.default.createElement(_TextInput2.default, {
          messages: this.getErrors(),
          onChange: this.onNameChange,
          label: _react2.default.createElement(
            'div',
            { className: styles.bankNameLabel },
            '\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u0431\u0430\u043D\u043A\u0430'
          ),
          type: 'text',
          placeholder: '\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0438\u043C\u044F...',
          'data-automation': 'sdk-create-bank-name-textinput'
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        _Modal2.default,
        {
          open: this.props.modalOpen,
          onDismiss: this.closeAction,
          size: 'small',
          label: '\u0411\u0430\u043D\u043A \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432',
          'data-automation': 'sdk-create-bank-modal'
        },
        _react2.default.createElement(
          _Modal.ModalHeader,
          null,
          _react2.default.createElement(
            _Heading2.default,
            { level: 'reset', as: 'h2' },
            '\u041D\u043E\u0432\u044B\u0439 \u0431\u0430\u043D\u043A \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432'
          ),
          _react2.default.createElement(_CloseButton2.default, {
            onClick: this.closeAction,
            placement: 'end',
            offset: 'medium',
            variant: 'icon'
          })
        ),
        _react2.default.createElement(
          _Modal.ModalBody,
          null,
          this.renderContent()
        ),
        _react2.default.createElement(
          _Modal.ModalFooter,
          null,
          this.cancelButton(),
          this.createBankButton()
        )
      );
    }
  }]);
  CreateBankModal.displayName = 'CreateBankModal'
  ;
  return CreateBankModal;
}(_react.Component), _class2.propTypes = {
  modalOpen: _propTypes2.default.bool.isRequired,
  modalTrigger: _propTypes2.default.instanceOf(Element),
  temporaryBank: _propTypes2.default.instanceOf(_Bank2.default).isRequired,
  displayError: _propTypes2.default.bool,
  errorKey: _propTypes2.default.string.isRequired,
  createBank: _propTypes2.default.func.isRequired,
  changeBank: _propTypes2.default.func.isRequired,
  clearModifications: _propTypes2.default.func.isRequired,
  showError: _propTypes2.default.func.isRequired,
  hideError: _propTypes2.default.func.isRequired,
  closeModal: _propTypes2.default.func.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired
}, _class2.defaultProps = {
  displayError: false,
  modalTrigger: null
}, _temp2)) || _class);
exports.default = CreateBankModal;