import { DAEMON_HOST } from "./constants.js";
export async function sleep(ms) {
    await new Promise((resolve) => setTimeout(resolve, ms));
}
export async function waitForHealth({ fetchImpl, port, timeoutMs, }) {
    const url = `http://${DAEMON_HOST}:${port}/health`;
    const startedAt = Date.now();
    // Simple polling; avoids bringing in extra deps.
    while (Date.now() - startedAt < timeoutMs) {
        try {
            const res = await fetchImpl(url, { method: "GET" });
            if (res.ok)
                return;
        }
        catch {
            // Retry until the deadline.
        }
        await sleep(200);
    }
    throw new Error(`Daemon not reachable at ${url}`);
}
export async function waitForHealthWithRetries({ fetchImpl, port, attempts, timeoutMs, delayMs, }) {
    let lastError = null;
    for (let attempt = 0; attempt < attempts; attempt += 1) {
        try {
            await waitForHealth({ fetchImpl, port, timeoutMs });
            return;
        }
        catch (error) {
            lastError = error;
            if (attempt < attempts - 1) {
                await sleep(Math.round(delayMs * 1.6 ** attempt));
            }
        }
    }
    throw lastError instanceof Error
        ? lastError
        : new Error(`Daemon not reachable at ${DAEMON_HOST}:${port}`);
}
export async function checkAuth({ fetchImpl, token, port, }) {
    try {
        const res = await fetchImpl(`http://${DAEMON_HOST}:${port}/v1/ping`, {
            headers: { Authorization: `Bearer ${token}` },
        });
        return res.ok;
    }
    catch {
        return false;
    }
}
export async function checkAuthWithRetries({ fetchImpl, token, port, attempts, delayMs, }) {
    for (let attempt = 0; attempt < attempts; attempt += 1) {
        if (await checkAuth({ fetchImpl, token, port }))
            return true;
        if (attempt < attempts - 1) {
            await sleep(Math.round(delayMs * 1.4 ** attempt));
        }
    }
    return false;
}
//# sourceMappingURL=cli-health.js.map