import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
const FW_CONFIG_DIR = path.join(os.homedir(), '.fw');
const CREDENTIALS_FILE = path.join(FW_CONFIG_DIR, 'credentials.json');
export function loadCredentials() {
    if (!fs.existsSync(CREDENTIALS_FILE))
        return null;
    try {
        const data = JSON.parse(fs.readFileSync(CREDENTIALS_FILE, 'utf-8'));
        if (isTokenExpired(data))
            return null;
        return data;
    }
    catch {
        return null;
    }
}
export function saveCredentials(creds) {
    fs.mkdirSync(FW_CONFIG_DIR, { recursive: true });
    fs.writeFileSync(CREDENTIALS_FILE, JSON.stringify(creds, null, 2), 'utf-8');
    try {
        fs.chmodSync(CREDENTIALS_FILE, 0o600);
    }
    catch { /* Windows */ }
}
export function clearCredentials() {
    try {
        fs.unlinkSync(CREDENTIALS_FILE);
    }
    catch { /* not found */ }
}
export function isTokenExpired(creds) {
    return Date.now() > creds.expiresAt;
}
export function getPlatformUrl() {
    const creds = loadCredentials();
    return creds?.platformUrl ?? process.env.FW_PLATFORM_URL ?? 'https://flowweaver.ai';
}
export function isLoggedIn() {
    return loadCredentials() !== null;
}
//# sourceMappingURL=credentials.js.map