"use strict";
/**
 * GrimSwap ZK SDK - Relayer Client
 *
 * Simple fetch-based client for submitting ZK proofs to the GrimSwap relayer.
 * Works in both Node.js and browser environments.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.submitToRelayer = submitToRelayer;
exports.getRelayerInfo = getRelayerInfo;
exports.checkRelayerHealth = checkRelayerHealth;
const constants_1 = require("./constants");
/**
 * Submit a private swap proof to the relayer for execution
 *
 * @param relayerUrl - Relayer endpoint URL
 * @param proof - Formatted proof (a, b, c points)
 * @param publicSignals - Public signals array
 * @param swapParams - Swap parameters including pool key
 * @returns Relayer response with tx hash on success
 */
async function submitToRelayer(relayerUrl, proof, publicSignals, swapParams) {
    const url = relayerUrl || constants_1.RELAYER_DEFAULT_URL;
    const body = {
        proof,
        publicSignals,
        swapParams,
    };
    const response = await fetch(`${url}/relay`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body),
    });
    if (!response.ok) {
        const errorText = await response.text().catch(() => "Unknown error");
        return {
            success: false,
            error: `Relayer returned ${response.status}: ${errorText}`,
        };
    }
    return (await response.json());
}
/**
 * Get relayer info (address, fee)
 *
 * @param relayerUrl - Relayer endpoint URL
 * @returns Relayer address and fee info
 */
async function getRelayerInfo(relayerUrl) {
    const url = relayerUrl || constants_1.RELAYER_DEFAULT_URL;
    const response = await fetch(`${url}/info`);
    if (!response.ok) {
        throw new Error(`Failed to get relayer info: ${response.status}`);
    }
    return (await response.json());
}
/**
 * Check if the relayer is healthy and accepting requests
 *
 * @param relayerUrl - Relayer endpoint URL
 * @returns true if relayer is healthy
 */
async function checkRelayerHealth(relayerUrl) {
    const url = relayerUrl || constants_1.RELAYER_DEFAULT_URL;
    try {
        const response = await fetch(`${url}/health`);
        return response.ok;
    }
    catch {
        return false;
    }
}
//# sourceMappingURL=relayer.js.map