"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerManager = void 0;
const server_js_1 = require("./server.js");
const configRepository_js_1 = require("../repositories/configRepository.js");
const serverLogger_js_1 = require("./serverLogger.js");
// Create a server-specific logger
const serverLogger = new serverLogger_js_1.ServerLogger('APIServer');
class ServerManager {
    static instance;
    serverInstance = null;
    configRepo = new configRepository_js_1.ConfigRepository();
    // Private constructor for singleton pattern
    constructor() { }
    // Get singleton instance
    static getInstance() {
        if (!ServerManager.instance) {
            ServerManager.instance = new ServerManager();
        }
        return ServerManager.instance;
    }
    // Start the server
    async startServer() {
        try {
            // Check if server is already running
            if (this.serverInstance) {
                serverLogger.log('Server is already running.');
                return;
            }
            // Use ConfigRepository for config access
            const enabledConfig = await this.configRepo.getValue('server.enabled');
            const isEnabled = enabledConfig !== 'false'; // Default to true if not set
            if (!isEnabled) {
                serverLogger.log('Server is disabled in settings, not starting');
                return;
            }
            this.serverInstance = await (0, server_js_1.startProjectServer)({
                log: serverLogger.log.bind(serverLogger),
                error: serverLogger.error.bind(serverLogger)
            });
        }
        catch (error) {
            serverLogger.error('Failed to start server:', error);
        }
    }
    // Stop the server
    async stopServer() {
        try {
            if (this.serverInstance) {
                await new Promise((resolve, reject) => {
                    this.serverInstance.close((err) => {
                        if (err)
                            reject(err);
                        else
                            resolve();
                    });
                });
                this.serverInstance = null;
                serverLogger.log('Server stopped due to configuration change');
                return { success: true };
            }
            serverLogger.log('Server was not running');
            return { success: false };
        }
        catch (error) {
            serverLogger.error('Failed to stop server:', error);
            return { success: false };
        }
    }
    // Restart the server
    async restartServer() {
        try {
            // First stop the server if it's running
            await this.stopServer();
            // Then start it again with new config
            serverLogger.log('Restarting server due to configuration change');
            this.serverInstance = await (0, server_js_1.startProjectServer)({
                log: serverLogger.log.bind(serverLogger),
                error: serverLogger.error.bind(serverLogger)
            });
            return { success: true };
        }
        catch (error) {
            serverLogger.error('Failed to restart server:', error);
            return { success: false };
        }
    }
    // Get logs by type - exposed for IPC handlers
    getLogs(type) {
        return serverLogger.getLogs(type);
    }
    // Clear logs by type - exposed for IPC handlers
    clearLogs(type) {
        return serverLogger.clearLogs(type);
    }
}
exports.ServerManager = ServerManager;
//# sourceMappingURL=serverManager.js.map