/**
 * Configuration management for 4runr-os
 * Stores settings in ~/.4runr/config.json
 */
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
const CONFIG_DIR = path.join(os.homedir(), '.4runr');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
const DEFAULT_CONFIG = {
    autoUpdate: true,
    updateCheck: true,
};
/**
 * Ensure config directory exists
 */
function ensureConfigDir() {
    if (!fs.existsSync(CONFIG_DIR)) {
        fs.mkdirSync(CONFIG_DIR, { recursive: true });
    }
}
/**
 * Load config from file (ENCRYPTED + VALIDATED)
 */
export function loadConfig() {
    ensureConfigDir();
    if (!fs.existsSync(CONFIG_FILE)) {
        return { ...DEFAULT_CONFIG };
    }
    try {
        const encrypted = fs.readFileSync(CONFIG_FILE, 'utf-8');
        // Try to decrypt (handles both encrypted and legacy unencrypted)
        let config;
        try {
            const { decryptJson, isEncrypted } = require('./security/encryption.js');
            const { validateConfig } = require('./security/validation.js');
            if (isEncrypted(encrypted)) {
                // New encrypted format
                const decrypted = decryptJson(encrypted);
                const validation = validateConfig(decrypted);
                if (!validation.valid || !validation.data) {
                    return { ...DEFAULT_CONFIG };
                }
                config = validation.data;
            }
            else {
                // Legacy unencrypted format
                const parsed = JSON.parse(encrypted);
                const validation = validateConfig(parsed);
                if (!validation.valid || !validation.data) {
                    return { ...DEFAULT_CONFIG };
                }
                config = validation.data;
                // Re-save with encryption
                saveConfig(config);
            }
        }
        catch {
            // Fallback to unencrypted (legacy)
            config = JSON.parse(encrypted);
        }
        return { ...DEFAULT_CONFIG, ...config };
    }
    catch {
        return { ...DEFAULT_CONFIG };
    }
}
/**
 * Save config to file (ENCRYPTED)
 */
export function saveConfig(config) {
    ensureConfigDir();
    try {
        const merged = { ...DEFAULT_CONFIG, ...loadConfig(), ...config };
        // SECURITY: Encrypt config data
        const { encryptJson } = require('./security/encryption.js');
        const encrypted = encryptJson(merged);
        fs.writeFileSync(CONFIG_FILE, encrypted, { encoding: 'utf-8', mode: 0o600 });
    }
    catch (error) {
        throw new Error(`Failed to save config: ${error}`);
    }
}
/**
 * Get a config value
 */
export function getConfig(key) {
    const config = loadConfig();
    return config[key];
}
/**
 * Set a config value
 */
export function setConfig(key, value) {
    const config = loadConfig();
    config[key] = value;
    saveConfig(config);
}
/**
 * Check if auto-update is enabled
 */
export function isAutoUpdateEnabled() {
    return getConfig('autoUpdate') !== false;
}
/**
 * Check if update check is enabled
 */
export function isUpdateCheckEnabled() {
    return getConfig('updateCheck') !== false;
}
/**
 * Set auto-update enabled/disabled
 */
export function setAutoUpdate(enabled) {
    setConfig('autoUpdate', enabled);
}
/**
 * Set update check enabled/disabled
 */
export function setUpdateCheck(enabled) {
    setConfig('updateCheck', enabled);
}
//# sourceMappingURL=config.js.map