const statusNames = {
    badrequest: 400,
    unauthorized: 401,
    forbidden: 403,
    notfound: 404,
    requesttimeout: 408,
    conflict: 409,
    payloadtoolarge: 413,
    toomanyrequests: 429,
    internalservererror: 500,
    badgateway: 502,
    serviceunavailable: 503,
    gatewaytimeout: 504,
};
function getStatusCode(error) {
    const numericStatus = error.match(/(?:statusCode|status code|HTTP)\s*["']?\s*[:=]?\s*(\d{3})/i)?.[1];
    if (numericStatus)
        return Number(numericStatus);
    const normalized = error.toLowerCase().replace(/[^a-z]/g, "");
    return Object.entries(statusNames).find(([name]) => normalized.includes(name))?.[1];
}
function getProviderMessage(error) {
    const jsonMessage = error.match(/["']message["']\s*:\s*["']([^"']+)["']/i)?.[1];
    if (jsonMessage)
        return jsonMessage.trim();
    const cleaned = error
        .replace(/^Batch error:\s*/i, "")
        .replace(/^Failed to create embeddings batch:\s*/i, "")
        .replace(/^(?:BadRequest|Unauthorized|Forbidden|NotFound|TooManyRequests|InternalServerError|BadGateway|ServiceUnavailable|GatewayTimeout)\s*[-:]\s*/i, "")
        .replace(/^\{.*$/s, "")
        .trim();
    return cleaned || undefined;
}
function formatProviderMessage(message, status) {
    const statusText = status ? ` (HTTP ${status})` : "";
    return message
        ? `The provider returned “${message}”${statusText}.`
        : `The embedding provider rejected the request${statusText}.`;
}
export function presentEmbeddingError(technicalDetails) {
    const status = getStatusCode(technicalDetails);
    const providerMessage = getProviderMessage(technicalDetails);
    const normalized = technicalDetails.toLowerCase();
    if (normalized.includes("model") &&
        (normalized.includes("not found") ||
            normalized.includes("does not exist") ||
            normalized.includes("unknown model"))) {
        return {
            title: "Embedding model unavailable",
            message: formatProviderMessage(providerMessage, status),
            suggestion: "Check the API model or deployment name and confirm that the provider account can use it.",
            technicalDetails,
        };
    }
    if (status === 404 || normalized.includes("resource not found")) {
        return {
            title: "Embedding endpoint not found",
            message: formatProviderMessage(providerMessage, status ?? 404),
            suggestion: "Check the API base URL and deployment name. For Azure OpenAI v1, the base URL normally ends in /openai/v1.",
            technicalDetails,
        };
    }
    if (status === 401 || status === 403) {
        return {
            title: "Provider authentication failed",
            message: formatProviderMessage(providerMessage, status),
            suggestion: "Check the provider credentials and confirm that they allow embedding requests.",
            technicalDetails,
        };
    }
    if (status === 429 || normalized.includes("rate limit")) {
        return {
            title: "Provider limit reached",
            message: formatProviderMessage(providerMessage, status ?? 429),
            suggestion: "Check the provider quota and billing status, then try the failed items again.",
            technicalDetails,
        };
    }
    if (normalized.includes("timeout") ||
        normalized.includes("timed out") ||
        status === 408 ||
        status === 504) {
        return {
            title: "Embedding request timed out",
            message: formatProviderMessage(providerMessage, status),
            suggestion: "Check provider availability and network connectivity, then try again.",
            technicalDetails,
        };
    }
    if (status === 400) {
        return {
            title: "Embedding request was rejected",
            message: formatProviderMessage(providerMessage, status),
            suggestion: "Check the API model or deployment name, embedding dimensions, and provider configuration.",
            technicalDetails,
        };
    }
    return {
        title: "Embedding request failed",
        message: formatProviderMessage(providerMessage, status),
        suggestion: "Review the provider and model configuration, then try the failed items again.",
        technicalDetails,
    };
}
//# sourceMappingURL=embeddingErrors.js.map