"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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 _getPrototypeOf2 = _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 _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _immutable = require("immutable");

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

var _passthroughProps = require("@instructure/ui-utils/lib/react/passthroughProps");

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

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

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

var BankSelection =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(BankSelection, _Component);

  function BankSelection() {
    (0, _classCallCheck2.default)(this, BankSelection);
    return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(BankSelection).apply(this, arguments));
  }

  (0, _createClass2.default)(BankSelection, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this = this;

      return this.props.getAllBanks().then(function (bankIds) {
        return _this.props.setUi(_constants.SELECT_BANKS_OPTIONS, (0, _immutable.List)(bankIds));
      });
    }
  }, {
    key: "renderOptions",
    value: function renderOptions() {
      return this.props.banks.toArray().map(function (bank) {
        return _react.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)(["Der Objektpool " + selectedBank.title + " existiert nicht mehr. W\xE4hlen Sie einen g\xFCltigen Pool."]);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          selectedBank = _this$props.selectedBank,
          label = _this$props.label;
      var selectedOption = {
        label: ''
      };

      if (selectedBank && !selectedBank.archived) {
        selectedOption.value = selectedBank.id;
        selectedOption.label = selectedBank.title;
      }

      return _react.default.createElement(_Select.default, Object.assign({}, (0, _passthroughProps.omitProps)(this.props, BankSelection.propTypes), {
        editable: true,
        selectedOption: selectedOption,
        label: label || "W\xE4hlen Sie den Zielpool",
        onChange: this.props.onBankSelection,
        "data-automation": "sdk-item-bank-modal",
        messages: this.getErrors()
      }), this.renderOptions());
    }
  }]);
  BankSelection.displayName = "BankSelection";
  return BankSelection;
}(_react.Component);

exports.default = BankSelection;
BankSelection.propTypes = {
  banks: _reactImmutableProptypes.default.list.isRequired,
  getAllBanks: _propTypes.default.func.isRequired,
  label: _propTypes.default.string,
  onBankSelection: _propTypes.default.func,
  selectedBank: _reactImmutableProptypes.default.record,
  setUi: _propTypes.default.func.isRequired
};
BankSelection.defaultProps = {
  label: null,
  onBankSelection: function onBankSelection() {},
  selectedBank: null
};