'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('@instructure/quiz-common/lib/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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

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

var _Modal2 = _interopRequireDefault(_Modal);

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

var _Bank2 = _interopRequireDefault(_Bank);

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.EditBankModal__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: 0 ' + theme.bankNameInputFontSize + ';\n}\n';
  },
  'bankNameInput': 'EditBankModal__styles__bankNameInput'
};
var EditBankModal = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(EditBankModal, _Component);

  function EditBankModal() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = EditBankModal.__proto__ || Object.getPrototypeOf(EditBankModal)).call.apply(_ref, [this].concat(args))), _this), _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());
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(EditBankModal, [{
    key: 'renderCancelButton',
    value: function renderCancelButton() {
      return _react2.default.createElement(
        _Button2.default,
        {
          onClick: this.closeAction,
          className: styles.footerButtonWrapper,
          margin: _constants.XSMALL_SIDE_MARGIN
        },
        'Whakakore'
      );
    }
  }, {
    key: 'renderSaveButton',
    value: function renderSaveButton() {
      return _react2.default.createElement(
        _Button2.default,
        {
          type: 'button',
          variant: 'primary',
          onClick: this.continueAction,
          margin: _constants.XSMALL_SIDE_MARGIN,
          'data-automation': 'sdk-edit-bank-save-button'
        },
        'Whakaora ng\u0101 huringa'
      );
    }
  }, {
    key: 'renderContent',
    value: function renderContent() {
      return _react2.default.createElement(
        'div',
        { className: styles.bankNameInput },
        _react2.default.createElement(_TextInput2.default, {
          value: this.props.workingBank.title,
          messages: this.getErrors(),
          onChange: this.onNameChange,
          label: _react2.default.createElement(
            'div',
            { className: styles.bankNameLabel },
            'ingoa P\u0113ke'
          ),
          type: 'text',
          placeholder: 'Whakauru he ingoa',
          'data-automation': 'sdk-edit-bank-name-textinput'
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        _Modal2.default,
        {
          open: this.props.modalOpen,
          onDismiss: this.closeAction,
          size: 'small',
          label: 'T\u016Bemi Peke',
          'data-automation': 'sdk-edit-bank-modal'
        },
        _react2.default.createElement(
          _Modal.ModalHeader,
          null,
          _react2.default.createElement(
            _Heading2.default,
            { level: 'reset', as: 'h2' },
            'Whakatika T\u016Bemi P\u0113ke'
          ),
          _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.renderCancelButton(),
          this.renderSaveButton()
        )
      );
    }
  }]);
  EditBankModal.displayName = 'EditBankModal'
  ;
  return EditBankModal;
}(_react.Component), _class2.propTypes = {
  modalOpen: _propTypes2.default.bool.isRequired,
  modalTrigger: _propTypes2.default.instanceOf(Element),
  workingBank: _propTypes2.default.instanceOf(_Bank2.default).isRequired,
  displayError: _propTypes2.default.bool,
  errorKey: _propTypes2.default.string.isRequired,
  updateBank: _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
}, _class2.defaultProps = {
  displayError: false,
  modalTrigger: null
}, _temp2)) || _class);
exports.default = EditBankModal;