"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.configManager = exports.ConfigManager = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const os_1 = require("os");
const DEFAULT_CONFIG = {
    apiServer: {
        port: 3001,
        host: 'localhost',
        autoStart: true
    },
    webUI: {
        port: 5173,
        host: 'localhost',
        autoStart: true,
        buildCommand: 'npm run build',
        devCommand: 'npm run dev',
        distPath: 'dist'
    },
    database: {
        path: (0, path_1.join)((0, os_1.homedir)(), '.one-logger', 'data.db')
    },
    logging: {
        level: 'info'
    }
};
class ConfigManager {
    config;
    configPath;
    constructor(configPath) {
        this.configPath = configPath || (0, path_1.join)((0, os_1.homedir)(), '.one-logger', 'config.json');
        this.config = this.loadConfig();
    }
    loadConfig() {
        try {
            if ((0, fs_1.existsSync)(this.configPath)) {
                const configData = (0, fs_1.readFileSync)(this.configPath, 'utf-8');
                const parsedConfig = JSON.parse(configData);
                return { ...DEFAULT_CONFIG, ...parsedConfig };
            }
        }
        catch (error) {
            console.warn(`Failed to load config from ${this.configPath}:`, error);
        }
        // Create default config if it doesn't exist
        this.saveConfig(DEFAULT_CONFIG);
        return { ...DEFAULT_CONFIG };
    }
    saveConfig(config) {
        try {
            const configDir = (0, path_1.dirname)(this.configPath);
            if (!(0, fs_1.existsSync)(configDir)) {
                (0, fs_1.mkdirSync)(configDir, { recursive: true });
            }
            (0, fs_1.writeFileSync)(this.configPath, JSON.stringify(config, null, 2));
        }
        catch (error) {
            console.error(`Failed to save config to ${this.configPath}:`, error);
            throw error;
        }
    }
    getConfig() {
        return { ...this.config };
    }
    updateConfig(updates) {
        this.config = { ...this.config, ...updates };
        this.saveConfig(this.config);
    }
    resetConfig() {
        this.config = { ...DEFAULT_CONFIG };
        this.saveConfig(this.config);
    }
    getApiServerUrl() {
        return `http://${this.config.apiServer.host}:${this.config.apiServer.port}`;
    }
    getWebUIUrl() {
        return `http://${this.config.webUI.host}:${this.config.webUI.port}`;
    }
    getDatabasePath() {
        return this.config.database.path;
    }
    getApiServerPort() {
        return this.config.apiServer.port;
    }
    getWebUIPort() {
        return this.config.webUI.port;
    }
    shouldAutoStartApiServer() {
        return this.config.apiServer.autoStart;
    }
    shouldAutoStartWebUI() {
        return this.config.webUI.autoStart;
    }
    getWebUIBuildCommand() {
        return this.config.webUI.buildCommand;
    }
    getWebUIDevCommand() {
        return this.config.webUI.devCommand;
    }
    getWebUIDistPath() {
        return this.config.webUI.distPath;
    }
    getLoggingLevel() {
        return this.config.logging.level;
    }
    getLoggingFile() {
        return this.config.logging.file;
    }
}
exports.ConfigManager = ConfigManager;
// Singleton instance
exports.configManager = new ConfigManager();
//# sourceMappingURL=configManager.js.map