import { logger } from "../../utils/logger.js";
import { sendProjectAdded, sendProjectRemoved } from "../../gateway/messages/project-sync.js";
let providers = null;
export function setProjectSyncProviders(p) {
    providers = p;
}
export async function projectSyncRoutes(app) {
    app.post("/project-added", async (request, reply) => {
        if (!providers) {
            return reply.status(503).send({ error: "Daemon not fully initialized" });
        }
        const client = providers.getGatewayClient();
        if (!client || !client.connected) {
            return reply.status(200).send({ sent: false, reason: "not_connected" });
        }
        const { projectId, name, path } = request.body;
        if (!projectId || !name || !path) {
            return reply.status(400).send({ error: "Missing projectId, name, or path" });
        }
        try {
            // Start file watcher for the new project
            if (providers.startWatcher) {
                providers.startWatcher(projectId, path);
                logger.info(`File watcher started for project ${projectId} at ${path}`);
            }
            const sent = sendProjectAdded(client, { projectId, name, path });
            return reply.status(200).send({ sent, watcherStarted: !!providers.startWatcher });
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            logger.error(`Failed to send project_added: ${message}`);
            return reply.status(500).send({ error: message });
        }
    });
    app.post("/project-removed", async (request, reply) => {
        if (!providers) {
            return reply.status(503).send({ error: "Daemon not fully initialized" });
        }
        const client = providers.getGatewayClient();
        if (!client || !client.connected) {
            return reply.status(200).send({ sent: false, reason: "not_connected" });
        }
        const { projectId } = request.body;
        if (!projectId) {
            return reply.status(400).send({ error: "Missing projectId" });
        }
        try {
            // Stop file watcher for the removed project
            if (providers.stopWatcher) {
                await providers.stopWatcher(projectId);
                logger.info(`File watcher stopped for project ${projectId}`);
            }
            const sent = sendProjectRemoved(client, projectId);
            return reply.status(200).send({ sent, watcherStopped: !!providers.stopWatcher });
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            logger.error(`Failed to send project_removed: ${message}`);
            return reply.status(500).send({ error: message });
        }
    });
}
//# sourceMappingURL=project-sync.js.map