// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// engine/auth/config.ts — AuthConfig loading and saving (SPEC-161)
// Persists to data/global/auth-config.json. Never persists tokens.
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { join } from 'node:path';
const CONFIG_DIR = join(process.cwd(), 'data', 'global');
const CONFIG_PATH = join(CONFIG_DIR, 'auth-config.json');
let cached = null;
/**
 * Load auth config from disk. Returns null when no config has been set yet.
 * Config is cached in memory after the first load.
 */
export async function loadAuthConfig() {
    if (cached !== null) {
        return cached;
    }
    try {
        const raw = await readFile(CONFIG_PATH, 'utf-8');
        cached = JSON.parse(raw);
        return cached;
    }
    catch {
        // File does not exist or is unreadable — no config set yet.
        return null;
    }
}
/**
 * Save auth config to disk and update the in-memory cache.
 * Ensures the data/global directory exists before writing.
 */
export async function saveAuthConfig(config) {
    await mkdir(CONFIG_DIR, { recursive: true });
    const serialized = JSON.stringify(config, null, 2);
    await writeFile(CONFIG_PATH, serialized, 'utf-8');
    cached = config;
}
/**
 * Check whether auth has been configured (config file exists and is valid).
 */
export async function isAuthConfigured() {
    const config = await loadAuthConfig();
    return config !== null;
}
/**
 * Reset the in-memory cache. Used in tests and after config deletion.
 */
export function resetAuthConfigCache() {
    cached = null;
}
//# sourceMappingURL=config.js.map