"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMarketData = exports.useSupportedCurrencies = exports.useSupportedCounterCurrencies = exports.useCurrencyData = exports.useCurrencyChartData = exports.useMarketDataProvider = void 0;
const react_query_1 = require("@tanstack/react-query");
const api_1 = require("../api");
const react_1 = require("react");
const currencies_1 = require("../../currencies");
const currencyFormatter_1 = require("../utils/currencyFormatter");
const queryKeys_1 = require("../utils/queryKeys");
const timers_1 = require("../utils/timers");
const types_1 = require("../utils/types");
function useMarketDataProvider() {
    const supportedCurrenciesInLIve = (0, currencies_1.listSupportedCurrencies)();
    const liveCompatibleIds = supportedCurrenciesInLIve
        .map(({ id }) => id)
        .filter(Boolean);
    const { data: supportedCounterCurrencies } = (0, exports.useSupportedCounterCurrencies)();
    const { data: supportedCurrencies } = (0, exports.useSupportedCurrencies)();
    const liveCoinsList = (0, react_1.useMemo)(() => (supportedCurrencies || [])
        ?.filter(({ id }) => liveCompatibleIds.includes(id))
        .map(({ id }) => id), [liveCompatibleIds, supportedCurrencies]);
    return {
        supportedCounterCurrencies,
        supportedCurrencies,
        liveCoinsList,
    };
}
exports.useMarketDataProvider = useMarketDataProvider;
const useCurrencyChartData = ({ id, counterCurrency, range }) => (0, react_query_1.useQuery)({
    queryKey: [queryKeys_1.QUERY_KEY.CurrencyChartData, id, counterCurrency, range],
    queryFn: () => (0, api_1.fetchCurrencyChartData)({ counterCurrency, range, id }),
    refetchInterval: timers_1.REFETCH_TIME_ONE_MINUTE * timers_1.BASIC_REFETCH,
    staleTime: timers_1.REFETCH_TIME_ONE_MINUTE * timers_1.BASIC_REFETCH,
});
exports.useCurrencyChartData = useCurrencyChartData;
const useCurrencyData = ({ id, counterCurrency }) => (0, react_query_1.useQuery)({
    queryKey: [queryKeys_1.QUERY_KEY.CurrencyDataRaw, id, counterCurrency],
    queryFn: () => (0, api_1.fetchCurrency)({ id, counterCurrency }),
    refetchInterval: timers_1.REFETCH_TIME_ONE_MINUTE * timers_1.BASIC_REFETCH,
    staleTime: timers_1.REFETCH_TIME_ONE_MINUTE * timers_1.BASIC_REFETCH,
    select: data => (0, currencyFormatter_1.format)(data),
});
exports.useCurrencyData = useCurrencyData;
const useSupportedCounterCurrencies = () => (0, react_query_1.useQuery)({
    queryKey: [queryKeys_1.QUERY_KEY.SupportedCounterCurrencies],
    queryFn: () => (0, api_1.supportedCounterCurrencies)(),
    refetchOnWindowFocus: true,
    staleTime: timers_1.ONE_DAY,
});
exports.useSupportedCounterCurrencies = useSupportedCounterCurrencies;
const useSupportedCurrencies = () => (0, react_query_1.useQuery)({
    queryKey: [queryKeys_1.QUERY_KEY.SupportedCurrencies],
    queryFn: () => (0, api_1.getSupportedCoinsList)(),
    refetchOnWindowFocus: true,
    staleTime: timers_1.ONE_DAY,
});
exports.useSupportedCurrencies = useSupportedCurrencies;
function useMarketData(props) {
    const search = props.search?.toLowerCase() ?? "";
    return (0, react_query_1.useQueries)({
        queries: Array.from({ length: props.page ?? 1 }, (_, i) => i).map(page => ({
            queryKey: [
                queryKeys_1.QUERY_KEY.MarketData,
                page,
                props.order,
                {
                    counterCurrency: props.counterCurrency,
                    ...(props.search && props.search?.length >= 2 && { search: search }),
                    ...(props.starred && props.starred?.length >= 1 && { starred: props.starred }),
                    ...(props.liveCoinsList &&
                        props.liveCoinsList?.length >= 1 && { liveCoinsList: props.liveCoinsList }),
                    ...(props.order &&
                        [types_1.Order.topLosers, types_1.Order.topGainers].includes(props.order) && { range: props.range }),
                },
            ],
            queryFn: () => (0, api_1.fetchList)({ ...props, page, search }),
            select: (data) => ({
                formattedData: (0, currencyFormatter_1.currencyFormatter)(data),
                page,
            }),
            refetchOnMount: false,
            refetchOnReconnect: false,
            refetchOnWindowFocus: false,
        })),
        combine: combineMarketData,
    });
}
exports.useMarketData = useMarketData;
function combineMarketData(results) {
    const hashMap = new Map();
    results.forEach(result => {
        if (result.data) {
            hashMap.set(String(result.data.page), {
                updatedAt: result.dataUpdatedAt,
                refetch: result.refetch,
            });
        }
    });
    return {
        data: results.flatMap(result => result.data?.formattedData ?? []),
        isPending: results.some(result => result.isPending),
        isFetching: results.some(result => result.isFetching),
        isLoading: results.some(result => result.isLoading),
        isError: results.some(result => result.isError),
        cachedMetadataMap: hashMap,
    };
}
//# sourceMappingURL=useMarketDataProvider.js.map