/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { clearActiveProviderRuntimeContext, createProviderRuntimeContext, peekActiveProviderRuntimeContext, setActiveProviderRuntimeContext, } from '../runtime/providerRuntimeContext.js';
let settingsServiceInstance = null;
/**
 * Get or create the global SettingsService singleton instance.
 * Resolves through the currently active ProviderRuntimeContext when present.
 */
export function getSettingsService() {
    const activeContext = peekActiveProviderRuntimeContext();
    if (activeContext?.settingsService) {
        settingsServiceInstance = activeContext.settingsService;
        return activeContext.settingsService;
    }
    throw new Error('[settings] No SettingsService registered in the active provider runtime context. Call activateIsolatedRuntimeContext() and registerSettingsService() before accessing settings (@plan:PLAN-20251023-STATELESS-HARDENING.P08, @requirement:REQ-SP4-004).');
}
/**
 * Register an externally created SettingsService with the active runtime context.
 */
export function registerSettingsService(settingsService) {
    settingsServiceInstance = settingsService;
    const existingContext = peekActiveProviderRuntimeContext();
    if (existingContext) {
        setActiveProviderRuntimeContext({
            ...existingContext,
            settingsService,
        });
        return;
    }
    setActiveProviderRuntimeContext(createProviderRuntimeContext({
        settingsService,
        runtimeId: 'registered-singleton',
        metadata: { source: 'registerSettingsService' },
    }));
}
/**
 * Reset the settings service instance (for testing)
 */
export function resetSettingsService() {
    if (settingsServiceInstance) {
        settingsServiceInstance.clear();
    }
    settingsServiceInstance = null;
    clearActiveProviderRuntimeContext();
}
//# sourceMappingURL=settingsServiceInstance.js.map