/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @plan:PLAN-20250218-STATELESSPROVIDER.P03
 * @requirement:REQ-SP-002.1
 * Derived from pseudocode/provider-invocation.md:2 and pseudocode/cli-runtime.md:5.
 */
import { SettingsService } from '../settings/SettingsService.js';
import { MissingProviderRuntimeError } from '../providers/errors.js';
let activeContext = null;
let fallbackFactory = null;
export function createProviderRuntimeContext(init = {}) {
    return {
        settingsService: init.settingsService ?? new SettingsService(),
        config: init.config,
        runtimeId: init.runtimeId,
        metadata: init.metadata,
    };
}
export function setActiveProviderRuntimeContext(context) {
    activeContext = context;
}
export function clearActiveProviderRuntimeContext() {
    activeContext = null;
}
export function peekActiveProviderRuntimeContext() {
    return activeContext;
}
export function getActiveProviderRuntimeContext() {
    if (activeContext) {
        return activeContext;
    }
    if (fallbackFactory) {
        const fallbackContext = fallbackFactory();
        if (!fallbackContext?.settingsService) {
            throw new MissingProviderRuntimeError({
                providerKey: 'provider-runtime',
                missingFields: ['settings'],
                requirement: 'REQ-SP4-004',
                stage: 'fallbackFactory',
                metadata: {
                    hint: 'Fallback context must supply SettingsService.',
                },
                message: 'MissingProviderRuntimeError(provider-runtime): fallback context is missing SettingsService (REQ-SP4-004).',
            });
        }
        activeContext = fallbackContext;
        return fallbackContext;
    }
    throw new MissingProviderRuntimeError({
        providerKey: 'provider-runtime',
        missingFields: ['runtime registration'],
        requirement: 'REQ-SP4-004',
        stage: 'getActiveProviderRuntimeContext',
        metadata: {
            hint: 'Call activateIsolatedRuntimeContext() + registerCliProviderInfrastructure() before invoking providers or CLI helpers.',
        },
        message: 'MissingProviderRuntimeError(provider-runtime): runtime registration missing. Run activateIsolatedRuntimeContext() before invoking providers (REQ-SP4-004).',
    });
}
export function setProviderRuntimeContextFallback(factory) {
    fallbackFactory = factory;
    if (!activeContext && fallbackFactory) {
        activeContext = fallbackFactory();
    }
}
//# sourceMappingURL=providerRuntimeContext.js.map