#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const http_1 = __importDefault(require("http"));
const ws_1 = require("ws");
const cors_1 = __importDefault(require("cors"));
const serviceManager_1 = require("./serviceManager");
const services_1 = __importDefault(require("./api/services"));
const auth_1 = __importDefault(require("./api/auth"));
const registry_1 = __importDefault(require("./api/registry"));
const PORT = 9998;
async function main() {
    await serviceManager_1.serviceManager.connect();
    const app = (0, express_1.default)();
    const server = http_1.default.createServer(app);
    const wss = new ws_1.WebSocketServer({ server });
    app.use((0, cors_1.default)({
        origin: '*', // Allow all
        methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
        credentials: true,
    }));
    app.use(express_1.default.json());
    // API Routers
    app.use('/services', services_1.default);
    app.use('/auth', (0, auth_1.default)(wss));
    app.use('/registry', registry_1.default);
    app.get('/health', (req, res) => {
        res.status(200).json({ status: 'ok' });
    });
    wss.on('connection', (ws) => {
        console.log('Client connected to daemon WebSocket');
        ws.on('close', () => console.log('Client disconnected from daemon WebSocket'));
        ws.on('error', (error) => console.error('WebSocket error:', error));
    });
    server.listen(PORT, () => {
        console.log(`Repoburg Platform Daemon listening on http://localhost:${PORT}`);
    });
}
main().catch(err => {
    console.error("Failed to start daemon:", err);
    process.exit(1);
});
