"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryStandaloneOpenOrders = exports.queryStandaloneClearinghouseStates = exports.createStandaloneInfoClient = void 0;
const hyperliquid_1 = require("@nktkas/hyperliquid");
const perpsConfig_1 = require("../constants/perpsConfig.cjs");
/**
 * Creates a standalone InfoClient for lightweight read-only queries.
 * Does not require full perps initialization (no wallet, WebSocket, etc.)
 *
 * @param options - The configuration options for the standalone client.
 * @returns A new InfoClient instance configured for read-only queries.
 */
const createStandaloneInfoClient = (options) => {
    const { isTestnet, timeout = perpsConfig_1.PERPS_CONSTANTS.ConnectionTimeoutMs } = options;
    const httpTransport = new hyperliquid_1.HttpTransport({
        isTestnet,
        timeout,
    });
    return new hyperliquid_1.InfoClient({ transport: httpTransport });
};
exports.createStandaloneInfoClient = createStandaloneInfoClient;
/**
 * Query clearinghouseState across multiple DEXs in parallel.
 * Used by standalone mode to aggregate positions/account state across HIP-3 DEXs.
 *
 * @param infoClient - The HyperLiquid InfoClient instance to use for queries.
 * @param userAddress - The user's wallet address to query state for.
 * @param dexs - The array of DEX identifiers to query (null for main DEX).
 * @returns A promise that resolves to an array of clearinghouse state responses.
 */
const queryStandaloneClearinghouseStates = async (infoClient, userAddress, dexs) => {
    const results = await Promise.allSettled(dexs.map(async (dex) => {
        const queryParams = {
            user: userAddress,
        };
        if (dex) {
            queryParams.dex = dex;
        }
        return infoClient.clearinghouseState(queryParams);
    }));
    return results
        .filter((result) => result.status === 'fulfilled')
        .map((result) => result.value);
};
exports.queryStandaloneClearinghouseStates = queryStandaloneClearinghouseStates;
/**
 * Query frontendOpenOrders across multiple DEXs in parallel.
 * Used by standalone mode to fetch open orders across HIP-3 DEXs.
 *
 * @param infoClient - The HyperLiquid InfoClient instance to use for queries.
 * @param userAddress - The user's wallet address to query orders for.
 * @param dexs - The array of DEX identifiers to query (null for main DEX).
 * @returns A promise that resolves to an array of frontend open orders responses.
 */
const queryStandaloneOpenOrders = async (infoClient, userAddress, dexs) => {
    const results = await Promise.allSettled(dexs.map(async (dex) => {
        const queryParams = {
            user: userAddress,
        };
        if (dex) {
            queryParams.dex = dex;
        }
        return infoClient.frontendOpenOrders(queryParams);
    }));
    return results
        .filter((result) => result.status === 'fulfilled')
        .map((result) => result.value);
};
exports.queryStandaloneOpenOrders = queryStandaloneOpenOrders;
//# sourceMappingURL=standaloneInfoClient.cjs.map