/**
 * SAC error handling - Exit codes and error types
 */
export const ExitCode = {
    SUCCESS: 0,
    GENERAL: 1,
    USAGE: 2,
    AUTH: 3,
    QUOTA: 4,
    TIMEOUT: 5,
    NETWORK: 6,
    CONTENT_FILTER: 10,
};
export class SACError extends Error {
    constructor(message, exitCode = ExitCode.GENERAL, hint) {
        super(message);
        this.name = "SACError";
        this.exitCode = exitCode;
        this.hint = hint;
    }
    toJSON() {
        return {
            error: {
                code: this.exitCode,
                message: this.message,
                ...(this.hint ? { hint: this.hint } : {}),
                ...(this.taskId ? { task_id: this.taskId } : {}),
            },
        };
    }
}
export { SACError as CLIError };
export function mapApiError(status, body, url) {
    const apiMsg = body.error?.message || body.message || `HTTP ${status}`;
    const urlHint = url ? `\nURL: ${url}` : "";
    if (status === 401 || status === 403) {
        return new SACError(`Authentication failed (HTTP ${status}).${urlHint}`, ExitCode.AUTH, "Log in via the Seawork app again, or set SAC_API_KEY environment variable.");
    }
    if (status === 429) {
        return new SACError(`Rate limit exceeded. ${apiMsg}`, ExitCode.QUOTA, "Please wait and retry.");
    }
    if (status === 408 || status === 504) {
        return new SACError(`Request timed out (HTTP ${status}).`, ExitCode.TIMEOUT, "Try increasing --timeout or retry later.");
    }
    return new SACError(`API error: ${apiMsg} (HTTP ${status})${urlHint}`, ExitCode.GENERAL);
}
//# sourceMappingURL=errors.js.map