'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 _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _immutable = require('immutable');

var _Select = require('@instructure/ui-forms/lib/components/Select');

var _Select2 = _interopRequireDefault(_Select);

var _formatMessage = require('../../../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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

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

var BankSelection = (_temp = _class = function (_Component) {
  (0, _inherits3.default)(BankSelection, _Component);

  function BankSelection() {
    (0, _classCallCheck3.default)(this, BankSelection);
    return (0, _possibleConstructorReturn3.default)(this, (BankSelection.__proto__ || Object.getPrototypeOf(BankSelection)).apply(this, arguments));
  }

  (0, _createClass3.default)(BankSelection, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      return this.props.getAllBanks().then(function (bankIds) {
        return _this2.props.setUi(_constants.SELECT_BANKS_OPTIONS, (0, _immutable.List)(bankIds));
      });
    }
  }, {
    key: 'renderOptions',
    value: function renderOptions() {
      return this.props.banks.toArray().map(function (bank) {
        return _react2.default.createElement(
          'option',
          { key: bank.id, value: bank.id },
          bank.title
        );
      });
    }
  }, {
    key: 'getErrors',
    value: function getErrors() {
      var selectedBank = this.props.selectedBank;

      if (selectedBank && selectedBank.archived) {
        return (0, _instUIMessages.toErrors)(['El banco de elementos "' + selectedBank.title + '" no longer exists. Please select a valid bank.']);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var selectedBank = this.props.selectedBank;

      var selectedOption = { label: '' };
      if (selectedBank && !selectedBank.archived) {
        selectedOption.value = selectedBank.id;
        selectedOption.label = selectedBank.title;
      }
      return _react2.default.createElement(
        _Select2.default,
        {
          editable: true,
          selectedOption: selectedOption,
          label: 'Seleccione el banco de destino',
          onChange: this.props.onBankSelection,
          'data-automation': 'sdk-item-bank-modal',
          messages: this.getErrors()
        },
        this.renderOptions()
      );
    }
  }]);
  BankSelection.displayName = 'BankSelection'
  ;
  return BankSelection;
}(_react.Component), _class.propTypes = {
  banks: _reactImmutableProptypes2.default.list.isRequired,
  getAllBanks: _propTypes2.default.func.isRequired,
  onBankSelection: _propTypes2.default.func,
  selectedBank: _reactImmutableProptypes2.default.record,
  setUi: _propTypes2.default.func.isRequired
}, _class.defaultProps = {
  onBankSelection: function onBankSelection() {},
  selectedBank: null
}, _temp);
exports.default = BankSelection;