"use strict";
/**
 * Wire envelope shared by every daemon HTTP response.
 *
 * Why a uniform envelope: the cockpit's fetchFromDaemon helper inspects
 * `ok` and either returns `data` or throws a DaemonError carrying
 * `error.code` + `error.message`. Keeping these types in one place lets
 * the route modules and the chats-flow code in index.ts stay aligned
 * without each importing fastify or redefining the shape inline.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorResponse = errorResponse;
exports.successResponse = successResponse;
exports.listEnvelope = listEnvelope;
exports.sendError = sendError;
function errorResponse(code, message, details) {
    return {
        ok: false,
        error: details ? { code, message, details } : { code, message },
    };
}
function successResponse(data) {
    return {
        ok: true,
        data,
    };
}
function listEnvelope(items, hasMore = false) {
    return { items, total: items.length, hasMore };
}
/**
 * Default HTTP status per ErrorCode. Routes can override but should
 * very rarely need to — these match standard REST semantics.
 */
const DEFAULT_STATUS = {
    validation: 400,
    bad_request: 400,
    parse_error: 400,
    not_found: 404,
    conflict: 409,
    cli_failed: 502,
    connection_failed: 502,
    openrouter_error: 502,
    db_error: 500,
    stats_error: 500,
    internal: 500,
};
/**
 * Send an error response with the right HTTP status code AND the
 * canonical envelope. Replaces the older pattern of returning the body
 * from `errorResponse(...)` without setting `reply.code`, which left
 * 4xx-shaped errors arriving as HTTP 200.
 *
 * Use this in route handlers from now on. Existing `errorResponse(...)`
 * call-sites that still bare-return the body keep working (HTTP 200 +
 * `ok: false`) until migrated; cockpit + MCP both read the envelope, not
 * the status. New code should use `sendError` so we can drop the legacy
 * pattern in v0.8.
 */
function sendError(reply, code, message, details, statusOverride) {
    const status = statusOverride ?? DEFAULT_STATUS[code] ?? 500;
    reply.code(status);
    return errorResponse(code, message, details);
}
//# sourceMappingURL=api-response.js.map