"use strict";
/**
 * Runtime environment and launch-args builders.
 *
 * Extracted from src/ccs.ts (lines 146-163, 300-327, 329-369).
 * Covers update-cache helpers and launch-arg resolution for native Claude/Codex targets.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveNativeClaudeLaunchArgs = exports.showCachedUpdateNotification = exports.refreshUpdateCache = exports.resolveCodexRuntimeConfigOverrides = void 0;
const ui_1 = require("../utils/ui");
const browser_1 = require("../utils/browser");
const config_loader_facade_1 = require("../config/config-loader-facade");
const official_channels_runtime_1 = require("../channels/official-channels-runtime");
const official_channels_store_1 = require("../channels/official-channels-store");
const browser_codex_overrides_1 = require("../utils/browser-codex-overrides");
const version_1 = require("../utils/version");
const update_checker_1 = require("../utils/update-checker");
// ========== Codex Runtime Config ==========
function resolveCodexRuntimeConfigOverrides(target, browserLaunchOverride) {
    if (target !== 'codex') {
        return [];
    }
    const codexBrowserExposure = (0, browser_1.resolveBrowserExposure)((0, config_loader_facade_1.getBrowserConfig)().codex, browserLaunchOverride);
    if (!codexBrowserExposure.exposeForLaunch) {
        return [];
    }
    return (0, browser_codex_overrides_1.buildCodexBrowserMcpOverrides)();
}
exports.resolveCodexRuntimeConfigOverrides = resolveCodexRuntimeConfigOverrides;
// ========== Update Cache Helpers ==========
/**
 * Perform background update check (refreshes cache, no notification).
 */
async function refreshUpdateCache() {
    try {
        const currentVersion = (0, version_1.getVersion)();
        // npm is now the only supported installation method
        await (0, update_checker_1.checkForUpdates)(currentVersion, true, 'npm');
    }
    catch (_e) {
        // Silently fail - update check shouldn't crash main CLI
    }
}
exports.refreshUpdateCache = refreshUpdateCache;
/**
 * Show update notification if cached result indicates update available.
 * Returns true if notification was shown.
 */
async function showCachedUpdateNotification() {
    try {
        const currentVersion = (0, version_1.getVersion)();
        const updateInfo = (0, update_checker_1.checkCachedUpdate)(currentVersion);
        if (updateInfo) {
            await (0, update_checker_1.showUpdateNotification)(updateInfo);
            return true;
        }
    }
    catch (_e) {
        // Silently fail
    }
    return false;
}
exports.showCachedUpdateNotification = showCachedUpdateNotification;
// ========== Native Claude Launch Args ==========
function resolveNativeClaudeLaunchArgs(args, profileType, targetConfigDir) {
    const config = (0, config_loader_facade_1.getOfficialChannelsConfig)();
    const environment = (0, official_channels_runtime_1.getOfficialChannelsEnvironmentStatus)(targetConfigDir ? { CLAUDE_CONFIG_DIR: targetConfigDir } : undefined);
    const channelReadiness = {
        telegram: (0, official_channels_store_1.getOfficialChannelReadiness)('telegram'),
        discord: (0, official_channels_store_1.getOfficialChannelReadiness)('discord'),
        imessage: !(0, official_channels_runtime_1.officialChannelRequiresMacOS)('imessage') || process.platform === 'darwin',
    };
    const plan = (0, official_channels_runtime_1.resolveOfficialChannelsLaunchPlan)({
        args,
        config,
        target: 'claude',
        profileType,
        environment,
        channelReadiness,
    });
    for (const message of plan.skippedMessages) {
        console.error((0, ui_1.warn)(message));
    }
    if (config.selected.length > 0 &&
        environment.auth.state === 'eligible' &&
        environment.auth.orgRequirementMessage) {
        console.error((0, ui_1.warn)(environment.auth.orgRequirementMessage));
    }
    if (!plan.applied) {
        return args;
    }
    return (0, official_channels_runtime_1.buildOfficialChannelsArgs)(args, plan.appliedChannels, plan.wantsPermissionBypass);
}
exports.resolveNativeClaudeLaunchArgs = resolveNativeClaudeLaunchArgs;
//# sourceMappingURL=environment-builder.js.map