import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
export function getConfigDir() {
    return process.env.AE_TRACKING_HOME || path.join(os.homedir(), '.ae-tracking');
}
const CONFIG_FILE = () => path.join(getConfigDir(), 'config.json');
function load() {
    try {
        return JSON.parse(fs.readFileSync(CONFIG_FILE(), 'utf8'));
    }
    catch {
        return {};
    }
}
function save(c) {
    fs.mkdirSync(getConfigDir(), { recursive: true });
    fs.writeFileSync(CONFIG_FILE(), JSON.stringify(c, null, 2));
}
export function getActiveHost() { return load().activeHost ?? ''; }
export function setActiveHost(host) { const c = load(); c.activeHost = host; save(c); }
export function extractHostname(url) {
    try {
        return new URL(url).hostname;
    }
    catch {
        return url;
    }
}
