"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAssetsData = void 0;
const react_1 = require("react");
const api_1 = require("../state-manager/api");
const errorUtils_1 = require("../utils/errorUtils");
const emptyData = () => ({
    cryptoAssets: {},
    networks: {},
    cryptoOrTokenCurrencies: {},
    interestRates: {},
    markets: {},
    currenciesOrder: {
        currenciesIds: [],
        metaCurrencyIds: [],
        key: "",
        order: "",
    },
    pagination: { nextCursor: "" },
});
function useAssetsData({ search, currencyIds, useCase, areCurrenciesFiltered, product, version, isStaging, includeTestNetworks, skip, }) {
    const { data, isLoading, error, fetchNextPage, isSuccess, refetch, isFetching, isError, isFetchingNextPage, } = (0, api_1.useGetAssetsDataInfiniteQuery)({
        search,
        useCase,
        currencyIds: areCurrenciesFiltered ? currencyIds : undefined,
        product,
        version,
        isStaging,
        includeTestNetworks,
    }, { skip });
    const joinedPages = (0, react_1.useMemo)(() => {
        return data?.pages.reduce((acc, page) => {
            Object.assign(acc.cryptoAssets, page.cryptoAssets);
            Object.assign(acc.networks, page.networks);
            Object.assign(acc.cryptoOrTokenCurrencies, page.cryptoOrTokenCurrencies);
            Object.assign(acc.interestRates, page.interestRates);
            Object.assign(acc.markets, page.markets);
            acc.currenciesOrder.currenciesIds.push(...page.currenciesOrder.currenciesIds);
            acc.currenciesOrder.metaCurrencyIds.push(...page.currenciesOrder.metaCurrencyIds);
            acc.currenciesOrder.key = page.currenciesOrder.key;
            acc.currenciesOrder.order = page.currenciesOrder.order;
            acc.pagination.nextCursor = page.pagination.nextCursor;
            return acc;
        }, emptyData());
    }, [data]);
    const hasMore = Boolean(joinedPages?.pagination.nextCursor);
    const isInitialLoading = isLoading || (isFetching && !isFetchingNextPage);
    const errorInfo = (0, react_1.useMemo)(() => (0, errorUtils_1.parseError)(error), [error]);
    return {
        data: joinedPages,
        isLoading: isInitialLoading,
        isFetchingNextPage,
        error,
        errorInfo,
        loadNext: hasMore ? fetchNextPage : undefined,
        isSuccess,
        isError,
        refetch,
    };
}
exports.useAssetsData = useAssetsData;
//# sourceMappingURL=useAssetsData.js.map