/**
 * Identity Store
 *
 * Securely store and retrieve cached identity context locally.
 *
 * STORAGE LOCATION:
 * - ~/.agentics/identity.json (Unix/macOS)
 * - %USERPROFILE%\.agentics\identity.json (Windows)
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
// ============================================================================
// Configuration
// ============================================================================
const AGENTICS_CONFIG_DIR = '.agentics';
const IDENTITY_FILE = 'identity.json';
const DEFAULT_MAX_AGE_MS = 3600000; // 1 hour
// ============================================================================
// Identity Store Implementation
// ============================================================================
export class IdentityStore {
    configDir;
    identityPath;
    constructor(configDir) {
        this.configDir = configDir ?? path.join(os.homedir(), AGENTICS_CONFIG_DIR);
        this.identityPath = path.join(this.configDir, IDENTITY_FILE);
    }
    async save(identity) {
        await this.ensureConfigDir();
        const content = JSON.stringify(identity, null, 2);
        await fs.promises.writeFile(this.identityPath, content, {
            encoding: 'utf-8',
            mode: 0o600,
        });
    }
    async load() {
        try {
            const content = await fs.promises.readFile(this.identityPath, 'utf-8');
            const identity = JSON.parse(content);
            if (!identity.user?.user_id || !identity.org?.org_id || !identity.role) {
                return null;
            }
            return identity;
        }
        catch {
            return null;
        }
    }
    async delete() {
        try {
            await fs.promises.unlink(this.identityPath);
        }
        catch {
            // Ignore if file doesn't exist
        }
    }
    async exists() {
        try {
            await fs.promises.access(this.identityPath, fs.constants.R_OK);
            return true;
        }
        catch {
            return false;
        }
    }
    async isStale(maxAgeMs) {
        const identity = await this.load();
        if (!identity?.resolved_at) {
            return true;
        }
        const resolvedAt = new Date(identity.resolved_at).getTime();
        const age = Date.now() - resolvedAt;
        return age > (maxAgeMs ?? DEFAULT_MAX_AGE_MS);
    }
    getIdentityPath() {
        return this.identityPath;
    }
    async ensureConfigDir() {
        try {
            await fs.promises.mkdir(this.configDir, {
                recursive: true,
                mode: 0o700,
            });
        }
        catch {
            // Directory may already exist
        }
    }
}
// ============================================================================
// Factory Function
// ============================================================================
export function createIdentityStore(configDir) {
    return new IdentityStore(configDir);
}
//# sourceMappingURL=identity-store.js.map