/**
 * SyncServerLauncher — starts the built-in HTTP sync server for a graph host.
 *
 * Extracted from SyncController to keep that class within the 500 LOC ceiling.
 * Called directly by SyncController.serve().
 *
 * @module domain/services/controllers/SyncServerLauncher
 */
import SyncError from "../../errors/SyncError.js";
import HttpSyncServer from "../sync/HttpSyncServer.js";
// ── Constants ────────────────────────────────────────────────────────────────
const DEFAULT_SYNC_SERVER_MAX_BYTES = 4 * 1024 * 1024;
// ── SyncServerLauncher ───────────────────────────────────────────────────────
/**
 * Starts a built-in HTTP sync server for the given graph host.
 *
 * @param host - The WarpRuntime instance providing processSyncRequest and crypto
 * @param options - Server configuration
 * @returns Server handle with close() method and url string
 * @throws SyncError If port is not a number or httpPort adapter is missing
 */
export async function launchSyncServer(host, options) {
    const { port, host: hostname = '127.0.0.1', path = '/sync', maxRequestBytes = DEFAULT_SYNC_SERVER_MAX_BYTES, httpPort, auth, unsafeAllowUnauthenticatedLocalhost, } = options;
    if (typeof port !== 'number') {
        throw new SyncError('serve() requires a numeric port', {
            code: 'E_SYNC_SERVE',
            context: { port },
        });
    }
    if (httpPort === undefined || httpPort === null) {
        throw new SyncError('serve() requires an httpPort adapter', {
            code: 'E_SYNC_SERVE',
            context: {},
        });
    }
    const authConfig = auth
        ? {
            ...auth,
            crypto: host._crypto,
            ...(host._logger ? { logger: host._logger } : {}),
        }
        : undefined;
    assertSyncHostProcessesRequests(host);
    const httpServer = new HttpSyncServer({
        httpPort,
        graph: host,
        ...(host._logger ? { logger: host._logger } : {}),
        path,
        host: hostname,
        maxRequestBytes,
        ...(authConfig !== undefined ? { auth: authConfig } : {}),
        ...(unsafeAllowUnauthenticatedLocalhost !== undefined ? { unsafeAllowUnauthenticatedLocalhost } : {}),
    });
    return await httpServer.listen(port);
}
/**
 * The runtime surface passed to HttpSyncServer expects a peer with a
 * processSyncRequest method. SyncHost acquires that method through
 * prototype wiring at WarpRuntime construction; this assertion
 * declares the compatibility without a value-level cast.
 */
function assertSyncHostProcessesRequests(host) {
    void host;
}
