import { openServerSentEventStream } from './sse-stream.js';
export function buildContractInput(primaryKey, primaryValue, input) {
    return {
        [primaryKey]: primaryValue,
        ...(input ?? {}),
    };
}
export function requireContractRoute(routes, routeId, kind) {
    const route = routes.find((candidate) => candidate.id === routeId);
    if (!route) {
        throw new Error(`Unknown ${kind} "${routeId}"`);
    }
    return route;
}
export function invokeContractRoute(transport, route, input, options = {}) {
    const resolved = transport.resolveContractRequest(route.method, route.path, input);
    return transport.requestJson(resolved.url, {
        method: resolved.method,
        body: resolved.body,
        headers: options.headers,
        signal: options.signal,
    });
}
export async function openContractRouteStream(transport, route, input, options) {
    const resolved = transport.resolveContractRequest(route.method, route.path, input);
    return await openServerSentEventStream(transport.fetchImpl, resolved.url, options.handlers, {
        authToken: transport.authToken,
        headers: options.headers,
        signal: options.signal,
    });
}
