"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHarperHttpHandler = createHarperHttpHandler;
/**
 * Harper-HTTP adapter for the MCP transport core (application port, #614).
 *
 * Maps Harper's HTTP handler signature `(request, nextHandler) => { status,
 * headers, body }` to/from the transport's normalized Request/Response.
 * Auth runs upstream via `{ after: 'authentication' }` on the registration,
 * so `request.user` is already populated when this handler fires.
 *
 * SSE responses (reserved for #619) are returned as `{ body: iterable,
 * headers: { 'content-type': 'text/event-stream' } }`. Harper's serializer
 * at `server/serverHelpers/contentTypes.ts:128-162` picks the SSE writer
 * automatically and pipes the iterable to the wire.
 */
const transport_ts_1 = require("../transport.js");
function createHarperHttpHandler(profile) {
    return async function mcpHarperHttpHandler(request, nextHandler) {
        // WebSocket upgrades aren't ours — let the next handler take it.
        if (request.isWebSocket)
            return nextHandler(request);
        const norm = {
            method: request.method,
            headers: normalizeHeaders(request.headers),
            body: await readBody(request.body),
            user: request.user?.username ?? '',
            userObject: request.user,
            profile,
        };
        const res = await (0, transport_ts_1.handleMcpRequest)(norm);
        return toHarperResponse(res);
    };
}
function normalizeHeaders(headers) {
    const out = {};
    for (const [name, value] of headers) {
        out[name.toLowerCase()] = Array.isArray(value) ? value[0] : value;
    }
    return out;
}
async function readBody(body) {
    if (!body)
        return '';
    const chunks = [];
    for await (const chunk of body) {
        chunks.push(typeof chunk === 'string' ? Buffer.from(chunk, 'utf8') : chunk);
    }
    return Buffer.concat(chunks).toString('utf8');
}
function toHarperResponse(res) {
    const headers = { ...res.headers };
    if (res.jsonBody !== undefined) {
        if (!headers['Content-Type'] && !headers['content-type']) {
            headers['Content-Type'] = 'application/json';
        }
        return {
            status: res.status,
            headers,
            body: JSON.stringify(res.jsonBody),
        };
    }
    if (res.sseIterable !== undefined) {
        if (!headers['Content-Type'] && !headers['content-type']) {
            headers['Content-Type'] = 'text/event-stream';
        }
        if (!headers['Cache-Control'] && !headers['cache-control']) {
            headers['Cache-Control'] = 'no-store';
        }
        return {
            status: res.status,
            headers,
            body: res.sseIterable,
        };
    }
    return { status: res.status, headers };
}
//# sourceMappingURL=harperHttp.js.map