"use strict";
/**
 * config-getters.ts
 *
 * Typed sub-config accessor functions extracted from unified-config-loader.ts
 * (Phase 5 split — issue #1164).
 *
 * All functions read the loaded config via loadOrCreateUnifiedConfig and
 * return typed sub-configs with defaults applied.
 *
 * No I/O beyond what loadOrCreateUnifiedConfig performs internally.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCursorConfig = exports.getLoggingConfig = exports.getImageAnalysisConfig = exports.getBrowserConfig = exports.getDashboardAuthConfig = exports.isDashboardAuthEnabled = exports.getOfficialChannelsConfig = exports.getThinkingConfig = exports.getCliproxySafetyConfig = exports.getContinuityInheritanceMap = exports.getGlobalEnvConfig = exports.getWebSearchConfig = void 0;
const unified_config_types_1 = require("../unified-config-types");
const normalizers_1 = require("./normalizers");
const image_analysis_backend_resolver_1 = require("../../utils/hooks/image-analysis-backend-resolver");
const official_channels_ids_1 = require("../../channels/official-channels-ids");
const types_1 = require("../../utils/websearch/types");
// ---------------------------------------------------------------------------
// Circular-import safety: loadOrCreateUnifiedConfig lives in
// unified-config-loader.ts which imports this file. We break the cycle by
// using a lazy require() inside getConfig() so the module is resolved at
// call time (after both modules have finished loading) rather than at import
// time. This also preserves spy/mock compatibility: test spies replace the
// function on the module namespace object, and require() returns that live
// namespace, so the spy is always picked up.
// ---------------------------------------------------------------------------
function getConfig() {
    const loader = require('../unified-config-loader');
    return loader.loadOrCreateUnifiedConfig();
}
// ---------------------------------------------------------------------------
// Accessor functions
// ---------------------------------------------------------------------------
/**
 * Get websearch configuration.
 * Returns defaults if not configured.
 * Supports deterministic providers and optional Gemini/OpenCode/Grok CLI fallbacks.
 */
function getWebSearchConfig() {
    const config = getConfig();
    // Build provider configs
    const exaConfig = {
        enabled: config.websearch?.providers?.exa?.enabled ?? false,
        max_results: config.websearch?.providers?.exa?.max_results ?? 5,
    };
    const tavilyConfig = {
        enabled: config.websearch?.providers?.tavily?.enabled ?? false,
        max_results: config.websearch?.providers?.tavily?.max_results ?? 5,
    };
    const duckDuckGoConfig = {
        enabled: config.websearch?.providers?.duckduckgo?.enabled ?? true,
        max_results: config.websearch?.providers?.duckduckgo?.max_results ?? 5,
    };
    const braveConfig = {
        enabled: config.websearch?.providers?.brave?.enabled ?? false,
        max_results: config.websearch?.providers?.brave?.max_results ?? 5,
    };
    const searxngConfig = {
        enabled: config.websearch?.providers?.searxng?.enabled ?? false,
        url: (0, types_1.normalizeSearxngBaseUrl)(config.websearch?.providers?.searxng?.url) ?? '',
        max_results: config.websearch?.providers?.searxng?.max_results ?? 5,
    };
    const geminiConfig = {
        enabled: config.websearch?.providers?.gemini?.enabled ?? config.websearch?.gemini?.enabled ?? false,
        model: config.websearch?.providers?.gemini?.model ?? 'gemini-2.5-flash',
        timeout: config.websearch?.providers?.gemini?.timeout ?? config.websearch?.gemini?.timeout ?? 55,
    };
    const opencodeConfig = {
        enabled: config.websearch?.providers?.opencode?.enabled ?? false,
        model: config.websearch?.providers?.opencode?.model ?? 'opencode/grok-code',
        timeout: config.websearch?.providers?.opencode?.timeout ?? 90,
    };
    const grokConfig = {
        enabled: config.websearch?.providers?.grok?.enabled ?? false,
        timeout: config.websearch?.providers?.grok?.timeout ?? 55,
    };
    // Auto-enable master switch if ANY provider is enabled
    const anyProviderEnabled = exaConfig.enabled ||
        tavilyConfig.enabled ||
        braveConfig.enabled ||
        searxngConfig.enabled ||
        duckDuckGoConfig.enabled ||
        geminiConfig.enabled ||
        opencodeConfig.enabled ||
        grokConfig.enabled;
    const enabled = anyProviderEnabled && (config.websearch?.enabled ?? true);
    return {
        enabled,
        providers: {
            exa: exaConfig,
            tavily: tavilyConfig,
            brave: braveConfig,
            searxng: searxngConfig,
            duckduckgo: duckDuckGoConfig,
            gemini: geminiConfig,
            opencode: opencodeConfig,
            grok: grokConfig,
        },
        // Legacy field for backwards compatibility
        gemini: config.websearch?.gemini,
    };
}
exports.getWebSearchConfig = getWebSearchConfig;
/**
 * Get global_env configuration.
 * Returns defaults if not configured.
 */
function getGlobalEnvConfig() {
    const config = getConfig();
    return {
        enabled: config.global_env?.enabled ?? true,
        env: config.global_env?.env ?? { ...unified_config_types_1.DEFAULT_GLOBAL_ENV },
    };
}
exports.getGlobalEnvConfig = getGlobalEnvConfig;
/**
 * Get continuity inheritance mapping.
 * Returns empty mapping when not configured.
 */
function getContinuityInheritanceMap() {
    const config = getConfig();
    return config.continuity?.inherit_from_account ?? {};
}
exports.getContinuityInheritanceMap = getContinuityInheritanceMap;
/**
 * Get cliproxy safety configuration.
 * Returns defaults if not configured.
 */
function getCliproxySafetyConfig() {
    const config = getConfig();
    return {
        antigravity_ack_bypass: config.cliproxy?.safety?.antigravity_ack_bypass ??
            unified_config_types_1.DEFAULT_CLIPROXY_SAFETY_CONFIG.antigravity_ack_bypass,
    };
}
exports.getCliproxySafetyConfig = getCliproxySafetyConfig;
/**
 * Get thinking configuration.
 * Returns defaults if not configured.
 */
function getThinkingConfig() {
    const config = getConfig();
    // W2: Check for invalid thinking config (e.g., thinking: true instead of object)
    if (config.thinking !== undefined && typeof config.thinking !== 'object') {
        console.warn(`[!] Invalid thinking config: expected object, got ${typeof config.thinking}. Using defaults.`);
        console.warn(`    Tip: Use 'thinking: { mode: auto }' instead of 'thinking: true'`);
        return unified_config_types_1.DEFAULT_THINKING_CONFIG;
    }
    return {
        mode: config.thinking?.mode ?? unified_config_types_1.DEFAULT_THINKING_CONFIG.mode,
        override: config.thinking?.override,
        tier_defaults: {
            opus: config.thinking?.tier_defaults?.opus ?? unified_config_types_1.DEFAULT_THINKING_CONFIG.tier_defaults.opus,
            sonnet: config.thinking?.tier_defaults?.sonnet ?? unified_config_types_1.DEFAULT_THINKING_CONFIG.tier_defaults.sonnet,
            haiku: config.thinking?.tier_defaults?.haiku ?? unified_config_types_1.DEFAULT_THINKING_CONFIG.tier_defaults.haiku,
        },
        provider_overrides: config.thinking?.provider_overrides,
        show_warnings: config.thinking?.show_warnings ?? unified_config_types_1.DEFAULT_THINKING_CONFIG.show_warnings,
    };
}
exports.getThinkingConfig = getThinkingConfig;
/**
 * Get Official Channels configuration.
 * Returns defaults if not configured.
 */
function getOfficialChannelsConfig() {
    const config = getConfig();
    return {
        selected: config.channels?.selected && config.channels.selected.length > 0
            ? (0, official_channels_ids_1.normalizeOfficialChannelIds)(config.channels.selected)
            : unified_config_types_1.DEFAULT_OFFICIAL_CHANNELS_CONFIG.selected,
        unattended: config.channels?.unattended ?? unified_config_types_1.DEFAULT_OFFICIAL_CHANNELS_CONFIG.unattended,
    };
}
exports.getOfficialChannelsConfig = getOfficialChannelsConfig;
/**
 * Check if dashboard auth is enabled.
 * Priority: ENV vars > config.yaml > defaults
 */
function isDashboardAuthEnabled() {
    const envEnabled = process.env.CCS_DASHBOARD_AUTH_ENABLED;
    if (envEnabled !== undefined) {
        return envEnabled === 'true' || envEnabled === '1';
    }
    const config = getConfig();
    return config.dashboard_auth?.enabled ?? false;
}
exports.isDashboardAuthEnabled = isDashboardAuthEnabled;
/**
 * Get dashboard_auth configuration with ENV var override.
 * Priority: ENV vars > config.yaml > defaults
 */
function getDashboardAuthConfig() {
    const config = getConfig();
    // ENV vars take precedence
    const envEnabled = process.env.CCS_DASHBOARD_AUTH_ENABLED;
    const envUsername = process.env.CCS_DASHBOARD_USERNAME;
    const envPasswordHash = process.env.CCS_DASHBOARD_PASSWORD_HASH;
    return {
        enabled: envEnabled !== undefined
            ? envEnabled === 'true' || envEnabled === '1'
            : (config.dashboard_auth?.enabled ?? false),
        username: envUsername ?? config.dashboard_auth?.username ?? '',
        password_hash: envPasswordHash ?? config.dashboard_auth?.password_hash ?? '',
        session_timeout_hours: config.dashboard_auth?.session_timeout_hours ?? 24,
    };
}
exports.getDashboardAuthConfig = getDashboardAuthConfig;
/**
 * Get browser automation configuration.
 * Returns canonicalized defaults if not configured.
 */
function getBrowserConfig() {
    const config = getConfig();
    return (0, normalizers_1.canonicalizeBrowserConfig)(config.browser);
}
exports.getBrowserConfig = getBrowserConfig;
/**
 * Get image_analysis configuration.
 * Returns defaults if not configured.
 */
function getImageAnalysisConfig() {
    const config = getConfig();
    return (0, image_analysis_backend_resolver_1.canonicalizeImageAnalysisConfig)({
        enabled: config.image_analysis?.enabled ?? unified_config_types_1.DEFAULT_IMAGE_ANALYSIS_CONFIG.enabled,
        timeout: config.image_analysis?.timeout ?? unified_config_types_1.DEFAULT_IMAGE_ANALYSIS_CONFIG.timeout,
        provider_models: config.image_analysis?.provider_models ?? unified_config_types_1.DEFAULT_IMAGE_ANALYSIS_CONFIG.provider_models,
        fallback_backend: config.image_analysis?.fallback_backend ?? unified_config_types_1.DEFAULT_IMAGE_ANALYSIS_CONFIG.fallback_backend,
        profile_backends: config.image_analysis?.profile_backends ?? unified_config_types_1.DEFAULT_IMAGE_ANALYSIS_CONFIG.profile_backends,
    });
}
exports.getImageAnalysisConfig = getImageAnalysisConfig;
/**
 * Get logging configuration.
 * Returns defaults if not configured.
 */
function getLoggingConfig() {
    const config = getConfig();
    return {
        enabled: config.logging?.enabled ?? unified_config_types_1.DEFAULT_LOGGING_CONFIG.enabled,
        level: config.logging?.level ?? unified_config_types_1.DEFAULT_LOGGING_CONFIG.level,
        rotate_mb: config.logging?.rotate_mb ?? unified_config_types_1.DEFAULT_LOGGING_CONFIG.rotate_mb,
        retain_days: config.logging?.retain_days ?? unified_config_types_1.DEFAULT_LOGGING_CONFIG.retain_days,
        redact: config.logging?.redact ?? unified_config_types_1.DEFAULT_LOGGING_CONFIG.redact,
        live_buffer_size: config.logging?.live_buffer_size ?? unified_config_types_1.DEFAULT_LOGGING_CONFIG.live_buffer_size,
    };
}
exports.getLoggingConfig = getLoggingConfig;
/**
 * Get cursor configuration.
 * Returns defaults if not configured.
 */
function getCursorConfig() {
    const config = getConfig();
    return config.cursor ?? { ...unified_config_types_1.DEFAULT_CURSOR_CONFIG };
}
exports.getCursorConfig = getCursorConfig;
//# sourceMappingURL=config-getters.js.map