"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const os_1 = require("os");
const path_1 = require("path");
const fs_1 = require("fs");
const DEFAULT_CONFIG = {
    server: {
        port: 3001,
        host: 'localhost'
    },
    web: {
        port: 5173
    },
    database: {
        path: (0, path_1.join)((0, os_1.homedir)(), '.one-logger', 'data', 'one-logger.db')
    },
    logging: {
        level: 'info'
    }
};
class ConfigManager {
    config;
    configPath;
    configDir;
    constructor() {
        this.configDir = (0, path_1.join)((0, os_1.homedir)(), '.one-logger');
        this.configPath = (0, path_1.join)(this.configDir, 'config.json');
        this.config = this.loadConfig();
    }
    /**
     * Load configuration from file or create default
     */
    loadConfig() {
        try {
            // Ensure config directory exists
            if (!(0, fs_1.existsSync)(this.configDir)) {
                (0, fs_1.mkdirSync)(this.configDir, { recursive: true });
            }
            // Ensure data directory exists
            const dataDir = (0, path_1.join)(this.configDir, 'data');
            if (!(0, fs_1.existsSync)(dataDir)) {
                (0, fs_1.mkdirSync)(dataDir, { recursive: true });
            }
            // Load existing config or create default
            if ((0, fs_1.existsSync)(this.configPath)) {
                const configData = (0, fs_1.readFileSync)(this.configPath, 'utf-8');
                const loadedConfig = JSON.parse(configData);
                // Merge with defaults to ensure all properties exist
                return this.mergeWithDefaults(loadedConfig);
            }
            else {
                // Create default config file
                this.saveConfig(DEFAULT_CONFIG);
                return { ...DEFAULT_CONFIG };
            }
        }
        catch (error) {
            console.warn('Failed to load config, using defaults:', error);
            return { ...DEFAULT_CONFIG };
        }
    }
    /**
     * Merge loaded config with defaults to ensure all properties exist
     */
    mergeWithDefaults(loadedConfig) {
        return {
            server: {
                ...DEFAULT_CONFIG.server,
                ...loadedConfig.server
            },
            web: {
                ...DEFAULT_CONFIG.web,
                ...loadedConfig.web
            },
            database: {
                ...DEFAULT_CONFIG.database,
                ...loadedConfig.database
            },
            logging: {
                ...DEFAULT_CONFIG.logging,
                ...loadedConfig.logging
            }
        };
    }
    /**
     * Save configuration to file
     */
    saveConfig(config) {
        try {
            (0, fs_1.writeFileSync)(this.configPath, JSON.stringify(config, null, 2));
        }
        catch (error) {
            console.error('Failed to save config:', error);
        }
    }
    /**
     * Get the full configuration
     */
    getConfig() {
        return { ...this.config };
    }
    /**
     * Update configuration
     */
    updateConfig(updates) {
        this.config = this.mergeWithDefaults({ ...this.config, ...updates });
        this.saveConfig(this.config);
    }
    /**
     * Get server port
     */
    getServerPort() {
        return process.env.ONE_LOGGER_SERVER_PORT ?
            parseInt(process.env.ONE_LOGGER_SERVER_PORT) :
            this.config.server.port;
    }
    /**
     * Get server host
     */
    getServerHost() {
        return process.env.ONE_LOGGER_SERVER_HOST || this.config.server.host;
    }
    /**
     * Get web port
     */
    getWebPort() {
        return process.env.ONE_LOGGER_WEB_PORT ?
            parseInt(process.env.ONE_LOGGER_WEB_PORT) :
            this.config.web.port;
    }
    /**
     * Get database path
     */
    getDatabasePath() {
        return process.env.ONE_LOGGER_DB_PATH || this.config.database.path;
    }
    /**
     * Get logging level
     */
    getLoggingLevel() {
        return process.env.ONE_LOGGER_LOG_LEVEL || this.config.logging.level;
    }
    /**
     * Get config directory path
     */
    getConfigDir() {
        return this.configDir;
    }
    /**
     * Get config file path
     */
    getConfigPath() {
        return this.configPath;
    }
    /**
     * Reset configuration to defaults
     */
    resetToDefaults() {
        this.config = { ...DEFAULT_CONFIG };
        this.saveConfig(this.config);
    }
}
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=configManager.js.map