/**
 * Discover ShortcutXL paths by calling GET /config on the XLL's HTTP server.
 *
 * Returns { xll_dir, modules_path } or null if Excel isn't running.
 * Retries a few times on startup since Excel may still be loading the XLL.
 */
import { getEffectiveExcelHttpUrl } from './endpoint.js';
import { requireLocalExcelHttpUrl } from './http-url.js';
import { xllFetch } from './session.js';
/**
 * Fetch XLL config from the running Excel instance.
 * Retries up to `maxRetries` times with `intervalMs` between attempts.
 * Returns null if Excel/XLL is unreachable after all retries.
 */
export async function fetchExcelConfig(options) {
    const maxRetries = options?.maxRetries ?? 5;
    const intervalMs = options?.intervalMs ?? 2000;
    const baseUrl = requireLocalExcelHttpUrl(options?.url ?? getEffectiveExcelHttpUrl());
    for (let attempt = 0; attempt <= maxRetries; attempt++) {
        try {
            const response = await xllFetch(`${baseUrl}/config`);
            if (!response.ok)
                continue;
            const data = (await response.json());
            if (data.xll_dir && data.modules_path) {
                return { xllDir: data.xll_dir, modulesPath: data.modules_path };
            }
        }
        catch {
            // Connection refused — Excel not running or XLL not loaded yet
        }
        if (attempt < maxRetries) {
            await new Promise((resolve) => setTimeout(resolve, intervalMs));
        }
    }
    return null;
}
//# sourceMappingURL=config.js.map