"use strict";
/**
 * Config Loader Facade
 *
 * Single import path for all config loading operations.
 * Re-exports read-only functions from unified-config-loader and config-manager,
 * and provides cache-coherent write wrappers that keep the memoization cache in sync.
 *
 * IMPORTANT:
 * - Raw write functions (saveUnifiedConfig, mutateUnifiedConfig,
 *   updateUnifiedConfig) are NOT re-exported here. Use the cache-coherent
 *   wrappers (saveConfig, mutateConfig, updateConfig) instead.
 * - `loadOrCreateUnifiedConfig` and `loadUnifiedConfig` are re-exported as
 *   uncached reads. If you need cached reads, use `getCachedConfig()`.
 *   If you use an uncached read followed by a write outside this facade,
 *   call `invalidateConfigCache()` to keep the cache coherent.
 *
 * Usage:
 *   import { getCachedConfig, saveConfig, mutateConfig } from '../config/config-loader-facade';
 *   import { getCcsDir, loadSettings } from '../config/config-loader-facade';
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasCachedConfig = exports.updateConfig = exports.mutateConfig = exports.saveConfig = exports.invalidateConfigCache = exports.getCachedConfig = exports.getCcsDir = exports.readConfig = exports.loadConfigSafe = exports.loadSettings = exports.getCursorConfig = exports.getLoggingConfig = exports.getImageAnalysisConfig = exports.getBrowserConfig = exports.getDashboardAuthConfig = exports.isDashboardAuthEnabled = exports.getOfficialChannelsConfig = exports.getThinkingConfig = exports.getCliproxySafetyConfig = exports.getContinuityInheritanceMap = exports.getGlobalEnvConfig = exports.getWebSearchConfig = exports.setDefaultProfile = exports.getDefaultProfile = exports.isUnifiedMode = exports.getConfigFormat = exports.hasLegacyConfig = exports.hasUnifiedConfig = exports.getConfigJsonPath = exports.getConfigYamlPath = exports.loadOrCreateUnifiedConfig = exports.loadUnifiedConfig = void 0;
// Re-export read-only functions from unified-config-loader
var unified_config_loader_1 = require("./unified-config-loader");
Object.defineProperty(exports, "loadUnifiedConfig", { enumerable: true, get: function () { return unified_config_loader_1.loadUnifiedConfig; } });
Object.defineProperty(exports, "loadOrCreateUnifiedConfig", { enumerable: true, get: function () { return unified_config_loader_1.loadOrCreateUnifiedConfig; } });
Object.defineProperty(exports, "getConfigYamlPath", { enumerable: true, get: function () { return unified_config_loader_1.getConfigYamlPath; } });
Object.defineProperty(exports, "getConfigJsonPath", { enumerable: true, get: function () { return unified_config_loader_1.getConfigJsonPath; } });
Object.defineProperty(exports, "hasUnifiedConfig", { enumerable: true, get: function () { return unified_config_loader_1.hasUnifiedConfig; } });
Object.defineProperty(exports, "hasLegacyConfig", { enumerable: true, get: function () { return unified_config_loader_1.hasLegacyConfig; } });
Object.defineProperty(exports, "getConfigFormat", { enumerable: true, get: function () { return unified_config_loader_1.getConfigFormat; } });
Object.defineProperty(exports, "isUnifiedMode", { enumerable: true, get: function () { return unified_config_loader_1.isUnifiedMode; } });
Object.defineProperty(exports, "getDefaultProfile", { enumerable: true, get: function () { return unified_config_loader_1.getDefaultProfile; } });
Object.defineProperty(exports, "setDefaultProfile", { enumerable: true, get: function () { return unified_config_loader_1.setDefaultProfile; } });
Object.defineProperty(exports, "getWebSearchConfig", { enumerable: true, get: function () { return unified_config_loader_1.getWebSearchConfig; } });
Object.defineProperty(exports, "getGlobalEnvConfig", { enumerable: true, get: function () { return unified_config_loader_1.getGlobalEnvConfig; } });
Object.defineProperty(exports, "getContinuityInheritanceMap", { enumerable: true, get: function () { return unified_config_loader_1.getContinuityInheritanceMap; } });
Object.defineProperty(exports, "getCliproxySafetyConfig", { enumerable: true, get: function () { return unified_config_loader_1.getCliproxySafetyConfig; } });
Object.defineProperty(exports, "getThinkingConfig", { enumerable: true, get: function () { return unified_config_loader_1.getThinkingConfig; } });
Object.defineProperty(exports, "getOfficialChannelsConfig", { enumerable: true, get: function () { return unified_config_loader_1.getOfficialChannelsConfig; } });
Object.defineProperty(exports, "isDashboardAuthEnabled", { enumerable: true, get: function () { return unified_config_loader_1.isDashboardAuthEnabled; } });
Object.defineProperty(exports, "getDashboardAuthConfig", { enumerable: true, get: function () { return unified_config_loader_1.getDashboardAuthConfig; } });
Object.defineProperty(exports, "getBrowserConfig", { enumerable: true, get: function () { return unified_config_loader_1.getBrowserConfig; } });
Object.defineProperty(exports, "getImageAnalysisConfig", { enumerable: true, get: function () { return unified_config_loader_1.getImageAnalysisConfig; } });
Object.defineProperty(exports, "getLoggingConfig", { enumerable: true, get: function () { return unified_config_loader_1.getLoggingConfig; } });
Object.defineProperty(exports, "getCursorConfig", { enumerable: true, get: function () { return unified_config_loader_1.getCursorConfig; } });
// Re-export selected functions from config-manager
var config_manager_1 = require("../utils/config-manager");
Object.defineProperty(exports, "loadSettings", { enumerable: true, get: function () { return config_manager_1.loadSettings; } });
Object.defineProperty(exports, "loadConfigSafe", { enumerable: true, get: function () { return config_manager_1.loadConfigSafe; } });
Object.defineProperty(exports, "readConfig", { enumerable: true, get: function () { return config_manager_1.readConfig; } });
Object.defineProperty(exports, "getCcsDir", { enumerable: true, get: function () { return config_manager_1.getCcsDir; } });
// Internal imports for memoization wrappers
const fs_1 = require("fs");
const unified_config_loader_2 = require("./unified-config-loader");
// ---------------------------------------------------------------------------
// Memoization cache with mtime-based staleness detection
// ---------------------------------------------------------------------------
let _configCache = null;
let _cacheMtimeMs = 0;
function getConfigFileMtime() {
    try {
        return (0, fs_1.statSync)((0, unified_config_loader_2.getConfigYamlPath)()).mtimeMs;
    }
    catch {
        return 0;
    }
}
/**
 * Get the unified config with in-memory caching.
 * Checks file mtime on each call — if the config file was modified
 * externally (e.g. by code importing unified-config-loader directly),
 * the cache is automatically invalidated and re-read from disk.
 */
function getCachedConfig() {
    const currentMtime = getConfigFileMtime();
    if (!_configCache || currentMtime > _cacheMtimeMs) {
        _configCache = (0, unified_config_loader_2.loadOrCreateUnifiedConfig)();
        _cacheMtimeMs = getConfigFileMtime();
    }
    return structuredClone(_configCache);
}
exports.getCachedConfig = getCachedConfig;
/**
 * Clear the memoization cache.
 * The next call to getCachedConfig() will re-read from disk.
 */
function invalidateConfigCache() {
    _configCache = null;
    _cacheMtimeMs = 0;
}
exports.invalidateConfigCache = invalidateConfigCache;
/**
 * Save config to disk and update the cache.
 * Stores a deep copy to break the reference alias.
 */
function saveConfig(config) {
    (0, unified_config_loader_2.saveUnifiedConfig)(config);
    _configCache = structuredClone(config);
}
exports.saveConfig = saveConfig;
/**
 * Atomically mutate config (read-modify-write with lock) and invalidate cache.
 * Invalidated AFTER _mutateUnifiedConfig returns — if that throws, cache stays valid.
 */
function mutateConfig(mutator) {
    const result = (0, unified_config_loader_2.mutateUnifiedConfig)(mutator);
    _configCache = null;
    return result;
}
exports.mutateConfig = mutateConfig;
/**
 * Partial-update config and invalidate cache.
 * Shorthand for mutateConfig with Object.assign.
 */
function updateConfig(updates) {
    const result = (0, unified_config_loader_2.updateUnifiedConfig)(updates);
    _configCache = null;
    return result;
}
exports.updateConfig = updateConfig;
/**
 * Get the current cache state (for diagnostics/testing).
 * Returns true if a cached config exists, false otherwise.
 */
function hasCachedConfig() {
    return _configCache !== null;
}
exports.hasCachedConfig = hasCachedConfig;
//# sourceMappingURL=config-loader-facade.js.map