"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = exports.ServerConfig = exports.PATHS = exports.ENV = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const common_1 = require("./common");
// Load environment variables
dotenv_1.default.config();
exports.ENV = {
    isDevelopment: process.env.NODE_ENV === 'development',
    isProduction: process.env.NODE_ENV === 'production',
    platform: process.platform,
    homeDir: process.env.HOME || process.env.USERPROFILE || '',
};
exports.PATHS = {
    getAppDataDir: () => {
        switch (process.platform) {
            case 'win32':
                return path_1.default.join(process.env.APPDATA || '', common_1.APP_INFO.name);
            case 'linux':
                return path_1.default.join(process.env.HOME || '', common_1.APP_INFO.name);
            case 'darwin':
                return path_1.default.join(process.env.HOME || '', 'Library', 'Application Support', common_1.APP_INFO.name);
            default:
                throw new Error(`Unsupported platform: ${process.platform}`);
        }
    },
    getLogsDir: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'logs'),
    getUserConfigPath: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'config.json'),
    getFridayDir: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'Friday'),
    getFridayConfigPath: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'Friday', 'config.json'),
    getFridayDialogHistoryPath: () => path_1.default.join(exports.PATHS.getAppDataDir(), 'Friday', 'session.json'),
};
exports.ServerConfig = {
    port: parseInt(process.env.PORT || common_1.DEFAULT_CONFIG.server.port.toString()),
    database: {
        type: 'sqlite',
        database: path_1.default.join(exports.PATHS.getAppDataDir(), 'database.sqlite'),
    },
};
// 服务器端的配置管理
class ConfigManager {
    constructor() {
        this.config = exports.ServerConfig;
        this.loadUserConfig();
    }
    static getInstance() {
        if (!ConfigManager.instance) {
            ConfigManager.instance = new ConfigManager();
        }
        return ConfigManager.instance;
    }
    loadUserConfig() {
        const userConfigPath = exports.PATHS.getUserConfigPath();
        try {
            if (fs_1.default.existsSync(userConfigPath)) {
                const userConfig = JSON.parse(fs_1.default.readFileSync(userConfigPath, 'utf8'));
                this.config = Object.assign(Object.assign({}, this.config), userConfig);
            }
        }
        catch (error) {
            console.error('Failed to load user config:', error);
        }
    }
    getConfig() {
        return this.config;
    }
    saveConfig() {
        try {
            const userConfigPath = exports.PATHS.getUserConfigPath();
            fs_1.default.mkdirSync(path_1.default.dirname(userConfigPath), { recursive: true });
            fs_1.default.writeFileSync(userConfigPath, JSON.stringify(this.config, null, 2));
        }
        catch (error) {
            console.error('Failed to save config:', error);
        }
    }
    setPort(port) {
        return __awaiter(this, void 0, void 0, function* () {
            this.config = Object.assign(Object.assign({}, this.config), { port });
        });
    }
}
exports.ConfigManager = ConfigManager;
