"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilteredProviders = void 0;
const live_env_1 = require("@ledgerhq/live-env");
const react_1 = require("react");
const swap_1 = require("../../../providers/swap");
const featureFlags_1 = require("../../../../featureFlags");
const filterProvidersByFeatureFlags = (providers, ptxSwapMoonpayProviderFlag, ptxSwapExodusProviderFlag) => {
    let filtered = providers;
    if (!ptxSwapMoonpayProviderFlag?.enabled) {
        filtered = filtered.filter(provider => provider !== "moonpay");
    }
    if (!ptxSwapExodusProviderFlag?.enabled) {
        filtered = filtered.filter(provider => provider !== "exodus");
    }
    return filtered;
};
const useFilteredProviders = () => {
    const ptxSwapMoonpayProviderFlag = (0, featureFlags_1.useFeature)("ptxSwapMoonpayProvider");
    const ptxSwapExodusProviderFlag = (0, featureFlags_1.useFeature)("ptxSwapExodusProvider");
    const defaultProviders = (0, react_1.useMemo)(() => filterProvidersByFeatureFlags((0, swap_1.getDefaultSwapProviderKeys)(), ptxSwapMoonpayProviderFlag ?? undefined, ptxSwapExodusProviderFlag ?? undefined), [ptxSwapMoonpayProviderFlag, ptxSwapExodusProviderFlag]);
    const [providers, setProviders] = (0, react_1.useState)(defaultProviders);
    const [loading, setLoading] = (0, react_1.useState)(true);
    const [error, setError] = (0, react_1.useState)(null);
    const fetchProviders = (0, react_1.useCallback)(async () => {
        try {
            const ledgerSignatureEnv = (0, live_env_1.getEnv)("MOCK_EXCHANGE_TEST_CONFIG") ? "test" : "prod";
            const partnerSignatureEnv = (0, live_env_1.getEnv)("MOCK_EXCHANGE_TEST_PARTNER") ? "test" : "prod";
            const data = await (0, swap_1.fetchAndMergeProviderData)({ ledgerSignatureEnv, partnerSignatureEnv });
            const filteredProviders = filterProvidersByFeatureFlags(Object.keys(data), ptxSwapMoonpayProviderFlag ?? undefined, ptxSwapExodusProviderFlag ?? undefined);
            setProviders(filteredProviders);
        }
        catch (error) {
            setError(error);
        }
        finally {
            setLoading(false);
        }
    }, [ptxSwapMoonpayProviderFlag, ptxSwapExodusProviderFlag]);
    (0, react_1.useEffect)(() => {
        fetchProviders();
    }, [fetchProviders]);
    return { providers, loading, error };
};
exports.useFilteredProviders = useFilteredProviders;
//# sourceMappingURL=useFilteredProviders.js.map