"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectServiceEnvironment = collectServiceEnvironment;
const definitions_1 = require("../../auth-profiles/definitions");
const control_plane_config_1 = require("../../config/control-plane-config");
const branding_1 = require("../../core/branding");
const repo_registry_1 = require("../../core/repo-registry");
const SERVICE_PASSTHROUGH_ENV_VARS = [
    definitions_1.CLAUDE_API_KEY_ENV_VAR_NAME,
    definitions_1.CODEX_API_KEY_ENV_VAR_NAME,
    branding_1.ALPACA_GITHUB_TOKEN_ENV_NAME,
    branding_1.ALPACA_GITLAB_TOKEN_ENV_NAME,
    branding_1.ALPACA_GITEA_USERNAME_ENV_NAME,
    branding_1.ALPACA_GITEA_PASSWORD_ENV_NAME,
];
async function collectServiceEnvironment(homeDirectory, env) {
    const collected = {};
    for (const key of await collectServiceEnvironmentKeys(homeDirectory)) {
        const value = env?.[key] ?? process.env[key];
        if (value?.trim()) {
            collected[key] = value;
        }
    }
    return collected;
}
async function collectServiceEnvironmentKeys(homeDirectory) {
    const keys = new Set(SERVICE_PASSTHROUGH_ENV_VARS);
    let registry;
    try {
        registry = await (0, repo_registry_1.loadRepoRegistry)(homeDirectory);
    }
    catch {
        return [...keys];
    }
    await Promise.all(registry.repos
        .filter((entry) => entry.enabled)
        .map(async (entry) => {
        try {
            const config = await (0, control_plane_config_1.loadConfig)(entry.cwd);
            for (const name of (0, control_plane_config_1.collectCustomWorkerEnvTemplateNames)(config)) {
                keys.add(name);
            }
        }
        catch {
            // Service startup should not be blocked by diagnostics already
            // surfaced by doctor and per-repo runtime validation.
        }
    }));
    return [...keys];
}
