import { StructuredError } from '@agentuity/adapter';
import { z } from 'zod/v4';
export const CoderErrorCodeSchema = z
    .enum([
    'CODER_SESSION_NOT_FOUND',
    'CODER_SESSION_ARCHIVED',
    'CODER_SESSION_CONFLICT',
    'CODER_INVALID_REQUEST',
])
    .describe('Machine-readable error codes returned by coder APIs');
export const CoderResponseError = StructuredError('CoderResponseError')();
export const CoderSessionNotFoundError = StructuredError('CoderSessionNotFoundError')();
export const CoderSessionArchivedError = StructuredError('CoderSessionArchivedError')();
export const CoderSessionConflictError = StructuredError('CoderSessionConflictError')();
export const CoderErrorContextSchema = z
    .object({
    sessionId: z.string().optional().describe('Coder session id involved in the operation'),
    requestSessionId: z
        .string()
        .nullish()
        .describe('Request trace session id from x-session-id header'),
})
    .describe('Context used when mapping coder API errors');
export function throwCoderError(resp, context) {
    const code = resp.code;
    switch (code) {
        case 'CODER_SESSION_NOT_FOUND':
            throw new CoderSessionNotFoundError({
                message: resp.message,
                coderSessionId: context.sessionId ?? '',
            });
        case 'CODER_SESSION_ARCHIVED':
            throw new CoderSessionArchivedError({
                message: resp.message,
                coderSessionId: context.sessionId,
            });
        case 'CODER_SESSION_CONFLICT':
            throw new CoderSessionConflictError({
                message: resp.message,
                coderSessionId: context.sessionId,
            });
        default:
            throw new CoderResponseError({
                message: resp.message,
                coderSessionId: context.sessionId,
                requestSessionId: context.requestSessionId,
                code,
            });
    }
}
export function normalizeCoderUrl(url) {
    return url.replace(/\/$/, '');
}
export function withOrgId(params, orgId) {
    if (orgId) {
        params.set('orgId', orgId);
    }
    return params;
}
//# sourceMappingURL=util.js.map