"use strict";
/**
 * Shared health check utilities for external services
 * Replaces duplicated checkServiceHealth() across modules
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkServiceHealth = checkServiceHealth;
exports.checkServicesHealth = checkServicesHealth;
exports.isServiceOnline = isServiceOnline;
const fetch_with_timeout_1 = require("./fetch-with-timeout");
/**
 * Shared health check utility for external services
 * Uses fetchWithTimeout to prevent hangs
 *
 * @param url - The service URL to check
 * @param timeoutMs - Timeout in milliseconds (default: 3000)
 * @returns HealthCheckResult with online status and optional error
 */
async function checkServiceHealth(url, timeoutMs = 3000) {
    try {
        const response = await (0, fetch_with_timeout_1.fetchWithTimeout)(url, {}, timeoutMs);
        // Consider 2xx/3xx/4xx as "online", only 5xx as "offline"
        const online = response.ok || response.status < 500;
        return {
            online,
            error: online ? undefined : `HTTP ${response.status}`,
        };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Unknown error';
        return {
            online: false,
            error: message,
        };
    }
}
/**
 * Check multiple services in parallel
 * Useful for dashboard status checks
 *
 * @param services - Record of service name to URL mappings
 * @param timeoutMs - Timeout in milliseconds for each check (default: 3000)
 * @returns Record of service name to HealthCheckResult
 */
async function checkServicesHealth(services, timeoutMs = 3000) {
    const results = await Promise.all(Object.entries(services).map(async ([name, url]) => {
        const result = await checkServiceHealth(url, timeoutMs);
        return [name, result];
    }));
    return Object.fromEntries(results);
}
/**
 * Simplified boolean health check (backwards compatible)
 * Returns only online status without error details
 *
 * @param url - The service URL to check
 * @param timeoutMs - Timeout in milliseconds (default: 3000)
 * @returns boolean indicating if service is online
 */
async function isServiceOnline(url, timeoutMs = 3000) {
    const result = await checkServiceHealth(url, timeoutMs);
    return result.online;
}
//# sourceMappingURL=health-check.js.map