function decodeHtmlEntities(value) {
    if (typeof document === "undefined") {
        return value;
    }
    const textarea = document.createElement("textarea");
    textarea.innerHTML = value;
    return textarea.value;
}
function isRecord(value) {
    return !!value && typeof value === "object" && !Array.isArray(value);
}
function looksLikeJson(value) {
    return (value.startsWith("{") || value.startsWith("[") || value.startsWith('"'));
}
function cleanPlainTextError(value) {
    return decodeHtmlEntities(value)
        .replace(/^Failed to start agent:\s*/i, "")
        .replace(/^Unknown error\s*/i, "")
        .replace(/^Error:\s*/i, "")
        .replace(/^Error\s+/i, "")
        .replace(/\s+/g, " ")
        .trim();
}
function extractAgentErrorMessage(value, depth) {
    if (depth > 8 || value === undefined || value === null) {
        return "";
    }
    if (typeof value === "string") {
        const normalized = decodeHtmlEntities(value.replace(/<br\s*\/?>/gi, "\n")).trim();
        if (!normalized) {
            return "";
        }
        const jsonCandidate = normalized
            .replace(/^Failed to start agent:\s*/i, "")
            .replace(/^Error:\s*/i, "")
            .trim();
        if (looksLikeJson(jsonCandidate)) {
            try {
                const parsed = JSON.parse(jsonCandidate);
                const structuredMessage = extractAgentErrorMessage(parsed, depth + 1);
                if (structuredMessage) {
                    return structuredMessage;
                }
            }
            catch {
                // Fall through to plain-text cleanup when the provider error isn't valid JSON.
            }
        }
        const firstMeaningfulLine = normalized
            .split(/\r?\n/)
            .map((line) => cleanPlainTextError(line.trim()))
            .find(Boolean);
        return firstMeaningfulLine || cleanPlainTextError(normalized);
    }
    if (Array.isArray(value)) {
        for (const entry of value) {
            const extracted = extractAgentErrorMessage(entry, depth + 1);
            if (extracted) {
                return extracted;
            }
        }
        return "";
    }
    if (isRecord(value)) {
        const metadata = isRecord(value.metadata) ? value.metadata : null;
        const candidates = [
            metadata?.raw,
            value.raw,
            value.error,
            value.message,
            value.detail,
            value.error_description,
            value.description,
            value.title,
            value.summary,
            value.reason,
        ];
        for (const candidate of candidates) {
            const extracted = extractAgentErrorMessage(candidate, depth + 1);
            if (extracted) {
                return extracted;
            }
        }
        return "";
    }
    return cleanPlainTextError(String(value));
}
export function toUserFacingAgentErrorMessage(value) {
    return extractAgentErrorMessage(value, 0);
}
//# sourceMappingURL=agentErrorMessage.js.map