import { __decorate, __metadata } from "tslib";
import { Injectable, Scope } from 'graphql-modules';
import { AdminContextProvider } from '../../admin-context/providers/admin-context.provider.js';
import { getRateForCurrency, isCryptoCurrency } from '../helpers/exchange.helper.js';
import { CryptoExchangeProvider } from './crypto-exchange.provider.js';
import { FiatExchangeProvider } from './fiat-exchange.provider.js';
let ExchangeProvider = class ExchangeProvider {
    cryptoExchangeProvider;
    fiatExchangeProvider;
    adminContextProvider;
    constructor(cryptoExchangeProvider, fiatExchangeProvider, adminContextProvider) {
        this.cryptoExchangeProvider = cryptoExchangeProvider;
        this.fiatExchangeProvider = fiatExchangeProvider;
        this.adminContextProvider = adminContextProvider;
    }
    async getExchangeRates(baseCurrency, quoteCurrency, date) {
        let rate = 1;
        // if base and quote are the same currency, return rate
        if (baseCurrency === quoteCurrency) {
            return rate;
        }
        const { defaultLocalCurrency, defaultCryptoConversionFiatCurrency } = await this.adminContextProvider.getVerifiedAdminContext();
        // adjust rate and convert to FIAT if base or quote are crypto
        const ifBaseIsCryptoAdjuster = async () => {
            if (isCryptoCurrency(baseCurrency)) {
                const { value } = await this.cryptoExchangeProvider.getCryptoExchangeRateLoader.load({
                    cryptoCurrency: baseCurrency,
                    date,
                });
                rate = rate * Number(value);
                baseCurrency = defaultCryptoConversionFiatCurrency;
            }
        };
        const ifQuoteIsCryptoAdjuster = async () => {
            if (isCryptoCurrency(quoteCurrency)) {
                const { value } = await this.cryptoExchangeProvider.getCryptoExchangeRateLoader.load({
                    cryptoCurrency: quoteCurrency,
                    date,
                });
                rate = rate / Number(value);
                quoteCurrency = defaultCryptoConversionFiatCurrency;
            }
        };
        const getFiatRatesPromise = this.fiatExchangeProvider.getExchangeRatesByDatesLoader.load(date);
        const [rates] = await Promise.all([
            getFiatRatesPromise,
            ifBaseIsCryptoAdjuster(),
            ifQuoteIsCryptoAdjuster(),
        ]);
        // adjust rate and convert to local default currency if base or quote are not local
        if (baseCurrency !== defaultLocalCurrency) {
            const baseRate = getRateForCurrency(baseCurrency, rates, defaultLocalCurrency);
            rate = rate * baseRate;
        }
        if (quoteCurrency !== defaultLocalCurrency) {
            const quoteRate = getRateForCurrency(quoteCurrency, rates, defaultLocalCurrency);
            rate = rate / quoteRate;
        }
        return rate;
    }
};
ExchangeProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [CryptoExchangeProvider,
        FiatExchangeProvider,
        AdminContextProvider])
], ExchangeProvider);
export { ExchangeProvider };
//# sourceMappingURL=exchange.provider.js.map