import { AuthRequiredError, ForbiddenError } from '@/server/middleware/session.js';

/**
 * Shared errorMap for all vendor API routes.
 * Pass as errorMap: vendorAuthErrorMap in defineApi spec.
 * Matches the { ok, code, error, status } contract expected by define-api.ts.
 */
export function vendorAuthErrorMap(
  err: unknown,
): { ok: false; code: string; error: string; status: number } | null {
  if (err instanceof AuthRequiredError)
    return { ok: false, code: 'UNAUTHORIZED', error: 'Unauthorized', status: 401 };
  if (err instanceof ForbiddenError)
    return { ok: false, code: err.code, error: err.message, status: 403 };
  return null;
}