/**
 * Settings Service
 *
 * Provides global access to application settings within the CLI.
 * Uses globalThis/process storage so the singleton survives Turbopack
 * module re-evaluations in Next.js API routes.
 *
 * Usage:
 * ```typescript
 * import { getSettings } from './infrastructure/services/settings.service.js';
 *
 * const settings = getSettings();
 * console.log(settings.models.default); // 'claude-sonnet-4-6'
 * ```
 */
/** The globalThis / process key for the settings singleton. */
const SHEP_SETTINGS_KEY = '__shepSettings';
/** Read the settings instance from globalThis, falling back to process. */
function readSettings() {
    const fromGlobal = globalThis[SHEP_SETTINGS_KEY];
    if (fromGlobal != null)
        return fromGlobal;
    const fromProcess = process[SHEP_SETTINGS_KEY];
    if (fromProcess != null)
        return fromProcess;
    return null;
}
/** Write the settings instance to both globalThis and process. */
function writeSettings(value) {
    globalThis[SHEP_SETTINGS_KEY] = value;
    process[SHEP_SETTINGS_KEY] = value;
}
/**
 * Initialize the settings service with loaded settings.
 * Must be called once during CLI bootstrap.
 *
 * @param settings - The initialized settings
 * @throws Error if settings are already initialized
 */
export function initializeSettings(settings) {
    if (readSettings() !== null) {
        throw new Error('Settings already initialized. Cannot re-initialize.');
    }
    writeSettings(settings);
}
/**
 * Get the current application settings.
 *
 * @returns Current settings instance
 * @throws Error if settings haven't been initialized yet
 *
 * @example
 * ```typescript
 * const settings = getSettings();
 * console.log(settings.system.logLevel); // 'info'
 * ```
 */
export function getSettings() {
    const instance = readSettings();
    if (instance === null) {
        throw new Error('Settings not initialized. Call initializeSettings() during CLI bootstrap.');
    }
    return instance;
}
/**
 * Check if settings have been initialized.
 *
 * @returns True if settings are initialized, false otherwise
 */
export function hasSettings() {
    return readSettings() !== null;
}
/**
 * Update the settings singleton with new values.
 * Used after a successful database write to refresh the in-memory
 * singleton so the rest of the application sees the updated values
 * without a full page reload.
 *
 * @param settings - The updated settings to store
 * @throws Error if settings haven't been initialized yet
 */
export function updateSettings(settings) {
    if (readSettings() === null) {
        throw new Error('Settings not initialized. Cannot update before initialization.');
    }
    writeSettings(settings);
}
/**
 * Reset settings instance (for testing purposes only).
 * DO NOT use in production code.
 *
 * @internal
 */
export function resetSettings() {
    writeSettings(null);
}
