import { getNetworkDiscoveryService } from "./networkDiscovery.js";
import { serverLogger } from "../utils/serverLogger.js";
export function createNetworkDiscoveryRoutes(app, appState) {
    const discoveryService = getNetworkDiscoveryService(appState);
    // Get discovery status
    app.get("/api/discovery/status", (req, res)=>{
        try {
            const status = discoveryService.getStatus();
            res.json(status);
        } catch (error) {
            serverLogger.error("[DiscoveryRoutes] Failed to get status:", error);
            res.status(500).json({
                error: "Failed to get discovery status"
            });
        }
    });
    // Start advertising
    app.post("/api/discovery/start", async (req, res)=>{
        try {
            await discoveryService.startAdvertising();
            res.json({
                success: true,
                message: "Started advertising on network"
            });
        } catch (error) {
            serverLogger.error("[DiscoveryRoutes] Failed to start advertising:", error);
            res.status(500).json({
                error: "Failed to start advertising"
            });
        }
    });
    // Stop advertising
    app.post("/api/discovery/stop", async (req, res)=>{
        try {
            await discoveryService.stopAdvertising();
            res.json({
                success: true,
                message: "Stopped advertising"
            });
        } catch (error) {
            serverLogger.error("[DiscoveryRoutes] Failed to stop advertising:", error);
            res.status(500).json({
                error: "Failed to stop advertising"
            });
        }
    });
    // Browse for other servers
    app.get("/api/discovery/browse", async (req, res)=>{
        try {
            const servers = await discoveryService.browseForServers();
            res.json({
                servers
            });
        } catch (error) {
            serverLogger.error("[DiscoveryRoutes] Failed to browse:", error);
            res.status(500).json({
                error: "Failed to browse for servers"
            });
        }
    });
    // Update discovery settings
    app.post("/api/discovery/settings", async (req, res)=>{
        try {
            const { enableDiscovery, serverName } = req.body;
            const settings = appState.getSettings();
            if (enableDiscovery !== undefined) {
                settings.enableDiscovery = enableDiscovery;
            }
            if (serverName !== undefined) {
                settings.serverName = serverName;
            }
            appState.updateSettings(settings);
            // Restart advertising with new settings
            if (settings.enableDiscovery) {
                await discoveryService.stopAdvertising();
                await discoveryService.startAdvertising();
            } else {
                await discoveryService.stopAdvertising();
            }
            res.json({
                success: true,
                settings: {
                    enableDiscovery: settings.enableDiscovery,
                    serverName: settings.serverName
                }
            });
        } catch (error) {
            serverLogger.error("[DiscoveryRoutes] Failed to update settings:", error);
            res.status(500).json({
                error: "Failed to update settings"
            });
        }
    });
    serverLogger.info("[DiscoveryRoutes] Network discovery routes initialized");
}

//# sourceMappingURL=networkDiscoveryRoutes.js.map