import { createServer } from '../server/api.js';
import { normalizeWebDashboardMode } from '../server/web-dashboard.js';
// Catch anything that would cause a silent exit
process.on('uncaughtException', (err) => {
    console.error('\n[codragraph serve] Uncaught exception:', err.message);
    if (process.env.DEBUG)
        console.error(err.stack);
    process.exit(1);
});
process.on('unhandledRejection', (reason) => {
    console.error('\n[codragraph serve] Unhandled rejection:', reason?.message || reason);
    if (process.env.DEBUG)
        console.error(reason?.stack);
    process.exit(1);
});
export const serveCommand = async (options) => {
    const port = Number(options?.port ?? 4747);
    // Default to 'localhost' so the OS decides whether to bind to 127.0.0.1 or
    // ::1 based on system configuration, avoiding spurious CORS errors when the
    // hosted frontend at codragraph.vercel.app connects to localhost.
    const host = options?.host ?? 'localhost';
    try {
        const web = normalizeWebDashboardMode(options?.web);
        await createServer(port, host, { web });
    }
    catch (err) {
        console.error(`\nFailed to start CodraGraph server:\n`);
        console.error(`  ${err.message || err}\n`);
        if (err.code === 'EADDRINUSE') {
            console.error(`  Port ${port} is already in use. Either:`);
            console.error(`    1. Stop the other process using port ${port}`);
            console.error(`    2. Use a different port: codragraph serve --port 4748\n`);
        }
        if (err.stack && process.env.DEBUG) {
            console.error(err.stack);
        }
        process.exit(1);
    }
};
