/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { getProviderKeyStorage, KeyringTokenStore, ProxyProviderKeyStorage, ProxySocketClient, ProxyTokenStore, } from '@vybestack/llxprt-code-core';
let proxyTokenStore;
let directTokenStore;
let proxyKeyStorage;
let directKeyStorage;
/**
 * Creates or returns a singleton TokenStore appropriate for the current environment.
 *
 * **This is the ONLY sanctioned way to obtain a TokenStore instance.**
 * Do not instantiate `KeyringTokenStore` or `ProxyTokenStore` directly.
 *
 * - When LLXPRT_CREDENTIAL_SOCKET env var is set: returns ProxyTokenStore
 * - Otherwise: returns KeyringTokenStore (direct host access)
 *
 * The singleton is cached per-mode, so switching between proxy and direct modes
 * will return the appropriate cached instance for each mode.
 *
 * @plan PLAN-20250214-CREDPROXY.P36
 */
export function createTokenStore() {
    const socketPath = process.env.LLXPRT_CREDENTIAL_SOCKET;
    if (socketPath) {
        if (!proxyTokenStore) {
            proxyTokenStore = new ProxyTokenStore(socketPath);
        }
        return proxyTokenStore;
    }
    else {
        if (!directTokenStore) {
            directTokenStore = new KeyringTokenStore();
        }
        return directTokenStore;
    }
}
/**
 * Creates or returns a singleton ProviderKeyStorage appropriate for the current environment.
 *
 * **This is the ONLY sanctioned way to obtain a ProviderKeyStorage instance.**
 * Do not call `getProviderKeyStorage()` directly or instantiate `ProviderKeyStorage`.
 *
 * - When LLXPRT_CREDENTIAL_SOCKET env var is set: returns ProxyProviderKeyStorage (read-only)
 * - Otherwise: returns the direct ProviderKeyStorage singleton
 *
 * @plan PLAN-20250214-CREDPROXY.P36
 */
export function createProviderKeyStorage() {
    const socketPath = process.env.LLXPRT_CREDENTIAL_SOCKET;
    if (socketPath) {
        if (!proxyKeyStorage) {
            const client = new ProxySocketClient(socketPath);
            proxyKeyStorage = new ProxyProviderKeyStorage(client);
        }
        return proxyKeyStorage;
    }
    else {
        if (!directKeyStorage) {
            directKeyStorage = getProviderKeyStorage();
        }
        return directKeyStorage;
    }
}
/**
 * Resets factory singletons. Used for test isolation.
 */
export function resetFactorySingletons() {
    proxyTokenStore = undefined;
    directTokenStore = undefined;
    proxyKeyStorage = undefined;
    directKeyStorage = undefined;
}
//# sourceMappingURL=credential-store-factory.js.map