"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetAssetDataQuery = exports.useGetAssetsDataInfiniteQuery = exports.assetsDataApi = void 0;
const react_1 = require("@reduxjs/toolkit/query/react");
const cryptoassets_1 = require("@ledgerhq/cryptoassets");
const live_env_1 = require("@ledgerhq/live-env");
const types_1 = require("./types");
function transformAssetsResponse(response, meta) {
    const enrichedCryptoOrTokenCurrencies = (0, cryptoassets_1.convertApiAssets)(response.cryptoOrTokenCurrencies);
    const nextCursor = meta?.response?.headers.get("x-ledger-next") || undefined;
    return {
        ...response,
        cryptoOrTokenCurrencies: enrichedCryptoOrTokenCurrencies,
        pagination: {
            nextCursor,
        },
    };
}
exports.assetsDataApi = (0, react_1.createApi)({
    reducerPath: "assetsDataApi",
    baseQuery: (0, react_1.fetchBaseQuery)({
        baseUrl: "", // Will be overridden in query
    }),
    tagTypes: [types_1.AssetsDataTags.Assets],
    endpoints: build => ({
        getAssetsData: build.infiniteQuery({
            query: ({ pageParam, queryArg }) => {
                const params = {
                    pageSize: 100,
                    ...(pageParam?.cursor && { cursor: pageParam.cursor }),
                    ...(queryArg?.useCase && { transaction: queryArg.useCase }),
                    ...(queryArg?.currencyIds &&
                        queryArg?.currencyIds.length > 0 && { currencyIds: queryArg.currencyIds }),
                    ...(queryArg?.search && { search: queryArg.search }),
                    product: queryArg.product,
                    minVersion: queryArg.version,
                    ...(queryArg?.includeTestNetworks && {
                        includeTestNetworks: queryArg.includeTestNetworks,
                    }),
                    additionalData: queryArg.additionalData || [
                        types_1.AssetsAdditionalData.Apy,
                        types_1.AssetsAdditionalData.MarketTrend,
                    ],
                };
                const baseUrl = queryArg.isStaging ? (0, live_env_1.getEnv)("DADA_API_STAGING") : (0, live_env_1.getEnv)("DADA_API_PROD");
                return {
                    url: `${baseUrl}/assets`,
                    params,
                };
            },
            providesTags: [types_1.AssetsDataTags.Assets],
            transformResponse: transformAssetsResponse,
            infiniteQueryOptions: {
                initialPageParam: {
                    cursor: "",
                },
                getNextPageParam: lastPage => {
                    if (lastPage.pagination.nextCursor) {
                        return {
                            cursor: lastPage.pagination.nextCursor,
                        };
                    }
                    else {
                        return undefined;
                    }
                },
            },
        }),
        getAssetData: build.query({
            query: queryArg => {
                const params = {
                    pageSize: 1,
                    ...(queryArg?.currencyIds &&
                        queryArg?.currencyIds.length > 0 && { currencyIds: queryArg.currencyIds }),
                    product: queryArg.product,
                    minVersion: queryArg.version,
                    additionalData: queryArg.additionalData || [
                        types_1.AssetsAdditionalData.Apy,
                        types_1.AssetsAdditionalData.MarketTrend,
                    ],
                };
                const baseUrl = queryArg.isStaging ? (0, live_env_1.getEnv)("DADA_API_STAGING") : (0, live_env_1.getEnv)("DADA_API_PROD");
                return {
                    url: `${baseUrl}/assets`,
                    params,
                };
            },
            providesTags: [types_1.AssetsDataTags.Assets],
            transformResponse: transformAssetsResponse,
        }),
    }),
});
exports.useGetAssetsDataInfiniteQuery = exports.assetsDataApi.useGetAssetsDataInfiniteQuery, exports.useGetAssetDataQuery = exports.assetsDataApi.useGetAssetDataQuery;
//# sourceMappingURL=api.js.map