import { Select } from '@transferwise/components';
import PropTypes from 'prop-types';
import React from 'react';
import { useIntl } from 'react-intl';
import { g as getCurrencyForCode, a as getAllCurrencies } from './currencies-1f1f52ab.js';
import { s as styleInject } from './style-inject.es-1f59c1d0.js';

var css_248z = ".cc_tw-select_3768290182 a {\n  text-decoration: none;\n}\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNyYy9jb21tb24vQ3VycmVuY3lTZWxlY3Rvci5tb2R1bGUubGVzcyIsIkN1cnJlbmN5U2VsZWN0b3IubW9kdWxlLmxlc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7RUFDRSxxQkFBQTtBQ0NGIiwiZmlsZSI6IkN1cnJlbmN5U2VsZWN0b3IubW9kdWxlLmxlc3MifQ== */";
var defaultStyles = {"tw-select":"cc_tw-select_3768290182","twSelect":"cc_tw-select_3768290182"};
styleInject(css_248z);

function sortByCurrencyCode(a, b) {
  return a.currency.localeCompare(b.currency);
}
function toCurrencyOption(currency) {
  return {
    currency: currency.code,
    value: currency.code,
    label: currency.code,
    note: currency.name
  };
}
function generateCurrencyList(availableCurrencies) {
  return availableCurrencies ? availableCurrencies.map(getCurrencyForCode).filter(currency => currency !== undefined) : getAllCurrencies();
}
function generateCurrencyOptions(searchTerm, translate, availableCurrencies) {
  return generateCurrencyList(availableCurrencies).map(c => ({
    code: c.code,
    name: translate({
      id: c.nameTranslationKey
    })
  })).filter(c => {
    const currency = `${c.code} ${c.name}`.toLowerCase();
    return currency.includes(searchTerm.toLowerCase());
  }).map(toCurrencyOption).sort(sortByCurrencyCode);
}
function generateSelectedCurrencyOption(currencyCode, translate) {
  const currency = getCurrencyForCode(currencyCode);
  if (currency && currency.code && currency.nameTranslationKey) {
    return toCurrencyOption({
      code: currency.code,
      name: translate({
        id: currency.nameTranslationKey
      })
    });
  }
}
const CurrencySelector = ({
  type,
  currencyCode,
  style,
  search,
  onSelectChange,
  onSearchChange,
  availableCurrencies,
  disabled
}) => {
  const {
    formatMessage
  } = useIntl();
  return /*#__PURE__*/React.createElement(Select, {
    classNames: {
      ...style,
      ...defaultStyles
    },
    selected: currencyCode ? generateSelectedCurrencyOption(currencyCode, formatMessage) : null,
    onChange: onSelectChange(type),
    onSearchChange: onSearchChange,
    searchValue: search,
    options: generateCurrencyOptions(search, formatMessage, availableCurrencies),
    searchPlaceholder: formatMessage({
      id: 'search-currency'
    }),
    placeholder: formatMessage({
      id: 'search-currency'
    }),
    disabled: disabled
  });
};
CurrencySelector.propTypes = {
  type: PropTypes.string.isRequired,
  currencyCode: PropTypes.string.isRequired,
  style: PropTypes.shape({}).isRequired,
  search: PropTypes.string,
  onSelectChange: PropTypes.func,
  onSearchChange: PropTypes.func,
  availableCurrencies: PropTypes.arrayOf(PropTypes.string),
  disabled: PropTypes.bool
};
CurrencySelector.defaultProps = {
  search: '',
  onSelectChange: () => {},
  onSearchChange: () => {},
  availableCurrencies: null,
  disabled: false
};

export { CurrencySelector as C };
//# sourceMappingURL=CurrencySelector-9b98ec6f.js.map
