import { a as assertOkResponseCode, e as encodeQueryParams } from './encodeQueryParams-96e87b36.js';
import { f as fetchQuotes$1 } from './fetchQuotes-31ac98ed.js';
import transformQuotesResponse from './transformQuotesResponse.js';
import './extractQuote-aa1e3309.js';
import './sortQuotesByDeliverySpeedEstimate-f8c8a1b8.js';
import 'date-fns';
import 'duration-fns';

const config = {
  RATE_HOST: 'https://wise.com/gateway/v1/rates/live?source={SOURCE}&target={TARGET}'
};
function fetchRate(source, target) {
  return fetch(config.RATE_HOST.replace(/{SOURCE}/, source).replace(/{TARGET}/, target), {
    method: 'GET'
  }).then(assertOkResponseCode).then(response => response.json()).then(list => ({
    route: {
      source: list[0].source,
      target: list[0].target,
      rate: list[0].value,
      time: list[0].time
    }
  }));
}

const API_URL = 'https://api.wise.com/v1';
function reduceToMaxCollectedAmount(amounts) {
  return amounts.reduce((accumulator, value) => {
    const amount = value.amount || value;
    return amount > accumulator ? amount : accumulator;
  }, 0);
}
function findMaxAmountCollected(source, target, quotes) {
  return reduceToMaxCollectedAmount(
  // eslint-disable-next-line comma-dangle
  quotes.map(p => reduceToMaxCollectedAmount(p.routes[source][target].recordedAmounts)));
}
function fetchReferenceQuotes(source, target, {
  sourceCountry
} = {}) {
  const query = encodeQueryParams({
    source,
    target,
    sourceCountry
  });
  return fetch(`${API_URL}/comparisons/providers${query}`).then(assertOkResponseCode).then(response => response.json()).then(result => ({
    referenceQuotes: result.providers,
    maxAmountCollected: findMaxAmountCollected(source, target, result.providers)
  }));
}

function fetchQuotes(sourceCurrency, targetCurrency, amount, midMarketRate, wiseFee, {
  sourceCountry,
  providerCountry
} = {}, providerType = undefined) {
  return fetchQuotes$1(sourceCurrency, targetCurrency, amount, midMarketRate, wiseFee, {
    providerCountry,
    sourceCountry
  }, providerType).then(quotes => transformQuotesResponse(quotes, targetCurrency));
}

export { fetchQuotes, fetchRate, fetchReferenceQuotes, findMaxAmountCollected };
//# sourceMappingURL=clients.js.map
