import { config as loadEnv } from 'dotenv';
import { readFileSync } from 'fs';
import { homedir } from 'os';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { loadPairingIdentity } from './pairing-identity.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
loadEnv({ path: join(__dirname, '..', '.env') });
export function loadConfig() {
    const identity = loadPairingIdentity();
    const relayRoomFromEnv = process.env.RELAY_ROOM_ID?.trim() || 'default';
    return {
        cdpUrl: process.env.CDP_URL ?? 'http://127.0.0.1:9222',
        serverHost: process.env.SERVER_HOST ?? '127.0.0.1',
        serverPort: parseInt(process.env.SERVER_PORT ?? '3847', 10),
        pollIntervalMs: parseInt(process.env.POLL_INTERVAL_MS ?? '400', 10),
        debounceMs: parseInt(process.env.DEBOUNCE_MS ?? '150', 10),
        windowPollIntervalMs: parseInt(process.env.WINDOW_POLL_INTERVAL_MS ?? '8000', 10),
        webappPassword: process.env.WEBAPP_PASSWORD ?? '',
        cursorProjectsDir: process.env.CURSOR_PROJECTS_DIR?.trim() ||
            join(homedir(), '.cursor', 'projects'),
        openaiApiKey: process.env.OPENAI_API_KEY?.trim() ||
            process.env.OPENAPI_KEY?.trim() ||
            '',
        openaiTranscribeModel: process.env.OPENAI_TRANSCRIBE_MODEL?.trim() || 'whisper-1',
        focusSyncEnabled: process.env.FOCUS_SYNC_ENABLED !== '0',
        focusSyncIntervalMs: parseInt(process.env.FOCUS_SYNC_INTERVAL_MS ?? '1500', 10),
        relayUrl: process.env.RELAY_URL?.trim() ?? '',
        relayToken: process.env.RELAY_TOKEN?.trim() ?? '',
        relayRoomId: identity?.roomId ?? relayRoomFromEnv,
        pairingClientToken: identity?.clientToken,
        keepAwakeEnabled: process.platform === 'darwin' && process.env.KEEP_AWAKE?.trim() !== '0',
        relayKeepaliveMs: Math.max(0, parseInt(process.env.RELAY_KEEPALIVE_MS ?? '20000', 10) || 0),
    };
}
export function loadSelectors() {
    const raw = readFileSync(join(__dirname, '..', 'selectors.json'), 'utf-8');
    return JSON.parse(raw);
}
