export class ProviderError {
    name = "ProviderError";
    provider;
    message = "Unknown provider error";
    response;
    constructor(response, provider) {
        const parsed = parseErrorResponse(response);
        if (parsed) {
            this.message = parsed.data.error.message;
            this.name = parsed.data.error.type;
            this.response = parsed;
        }
        else {
            this.response = response;
        }
        this.provider = provider;
    }
    toJSON() {
        return {
            name: this.name,
            provider: this.provider,
            message: this.message,
            response: this.response,
        };
    }
}
export function parseErrorResponse(response) {
    const asAny = response;
    if (asAny &&
        "data" in asAny &&
        "error" in asAny.data) {
        return asAny;
    }
    return undefined;
}
export function normalizeProviderError(error, providerName) {
    const originalError = new ProviderError(error.response, providerName);
    const base = {
        providerName,
        originalError,
    };
    switch (error.type) {
        // Anthropic errors
        case "rate_limit_error":
            return { ...base, type: "rate_limit" };
        case "request_too_large":
            return { ...base, type: "validation_error" };
        case "authentication_error":
            return { ...base, type: "auth_error" };
        case "permission_error":
            return { ...base, type: "auth_error" };
        case "not_found_error":
            return { ...base, type: "not_found" };
        case "api_error":
            return { ...base, type: "api_error" };
        case "invalid_request_error":
            return { ...base, type: "validation_error" };
        case "overloaded_error":
            return { ...base, type: "overloaded" };
        // Bedrock errors
        case "ThrottlingException":
            return { ...base, type: "rate_limit" };
        case "AccessDeniedException":
        case "NotAuthorized":
            return { ...base, type: "auth_error" };
        case "InvalidClientTokenId":
        case "UnrecognizedClientException":
            return { ...base, type: "auth_error" };
        case "ResourceNotFound":
            return { ...base, type: "not_found" };
        case "InternalFailure":
            return { ...base, type: "api_error" };
        case "ValidationError":
        case "InvalidAction":
        case "IncompleteSignature":
            return { ...base, type: "validation_error" };
        case "RequestExpired":
            return { ...base, type: "timeout" };
        case "ServiceUnavailable":
            return { ...base, type: "overloaded" };
        default:
            return { ...base, type: "unknown" };
    }
}
export class NpmInstallError extends Error {
    constructor(message) {
        super(message);
        this.name = "NpmInstallError";
        Object.setPrototypeOf(this, new.target.prototype);
    }
}
//# sourceMappingURL=types.js.map