/**
 * Testfarm / Admin Factory local E2E — bypass public coder.vigthoria.io gateway
 * when VIGTHORIA_LOCAL_TEST_MODE=1 or VIGTHORIA_LOCAL_TEST_TOKEN is set.
 */
export function isLocalTestfarmMode() {
    const flag = String(process.env.VIGTHORIA_LOCAL_TEST_MODE || '').trim().toLowerCase();
    if (flag === '1' || flag === 'true' || flag === 'yes') {
        return true;
    }
    return Boolean(String(process.env.VIGTHORIA_LOCAL_TEST_TOKEN || '').trim());
}
/** Apply once at CLI startup — idempotent via ??= */
export function applyLocalTestfarmDefaults() {
    if (!isLocalTestfarmMode()) {
        return;
    }
    process.env.VIGTHORIA_ALLOW_LOCAL_SERVICES ??= '1';
    process.env.VIGTHORIA_ALLOW_LOCAL_V3_AGENT ??= '1';
    process.env.VIGTHORIA_SELF_HOSTED_MODELS_API_URL ??= 'http://127.0.0.1:4009';
    process.env.VIGTHORIA_HYPERLOOP_URL ??= 'http://127.0.0.1:8020/api/hyperloop/health';
    process.env.VIGTHORIA_HYPERLOOP_EXECUTE_URL ??= 'http://127.0.0.1:8020/api/hyperloop/execute';
    process.env.VIGTHORIA_HYPERLOOP_MODULES_URL ??= 'http://127.0.0.1:8020/api/hyperloop/modules';
    process.env.VIGTHORIA_V3_AGENT_URL ??= 'http://127.0.0.1:8030';
    process.env.VIGTHORIA_TEMPLATE_SERVICE_URL ??= 'http://127.0.0.1:4011';
    process.env.VIGTHORIA_HYPERLOOP_TIMEOUT_MS ??= '180000';
    const testToken = String(process.env.VIGTHORIA_LOCAL_TEST_TOKEN || '').trim();
    if (testToken && !process.env.VIGTHORIA_AUTH_TOKEN && !process.env.VIGTHORIA_TOKEN) {
        process.env.VIGTHORIA_AUTH_TOKEN = testToken;
    }
}
export function resolveHyperloopTimeoutMs() {
    const raw = process.env.VIGTHORIA_HYPERLOOP_TIMEOUT_MS
        || process.env.VIGTHORIA_HTTP_TIMEOUT_MS
        || '120000';
    const parsed = Number.parseInt(String(raw), 10);
    return Number.isFinite(parsed) && parsed > 0 ? parsed : 120000;
}
export async function fetchWithServiceTimeout(url, init = {}) {
    const ms = resolveHyperloopTimeoutMs();
    const signal = init.signal ?? AbortSignal.timeout(ms);
    return fetch(url, { ...init, signal });
}
