"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { ChevronDown } from "lucide-react";
import { useId, useState } from "react";
import { toast } from "sonner";
import { cn } from "../../lib/utils";
const DEFAULT_ERROR_TITLE = "An error occurred";
function textFromUnknown(value) {
    if (typeof value === "string") {
        const trimmed = value.trim();
        return trimmed || undefined;
    }
    if (value instanceof Error) {
        return value.message.trim() || undefined;
    }
    if (value == null)
        return undefined;
    try {
        return JSON.stringify(value, null, 2);
    }
    catch {
        return String(value);
    }
}
function getObjectText(value, key) {
    return key in value ? textFromUnknown(value[key]) : undefined;
}
export function normalizeErrorToastPayload(error) {
    if (error instanceof Error) {
        const title = textFromUnknown(error) ?? DEFAULT_ERROR_TITLE;
        const details = error.stack && error.stack.trim() !== title ? error.stack : undefined;
        return { title, details };
    }
    if (typeof error === "string") {
        return { title: textFromUnknown(error) ?? DEFAULT_ERROR_TITLE };
    }
    if (error && typeof error === "object") {
        const record = error;
        const summary = getObjectText(record, "summary");
        const details = getObjectText(record, "details");
        const rawDetails = getObjectText(record, "rawDetails");
        const detailText = details ?? rawDetails;
        let title = summary ?? DEFAULT_ERROR_TITLE;
        if (detailText && detailText === title) {
            title = DEFAULT_ERROR_TITLE;
        }
        return { title, details: detailText };
    }
    return { title: DEFAULT_ERROR_TITLE };
}
function ExpandableErrorDetails({ details, onToggle, }) {
    const [expanded, setExpanded] = useState(false);
    const detailsId = useId();
    return (_jsxs("div", { className: "mt-1 w-full text-xs", children: [_jsxs("button", { type: "button", "aria-expanded": expanded, "aria-controls": detailsId, className: "text-neutral-grey-50 hover:text-neutral-grey-100 focus-visible:ring-ring/50 inline-flex items-center gap-1 rounded-sm py-0.5 font-medium outline-none focus-visible:ring-2", onClick: (event) => {
                    event.preventDefault();
                    event.stopPropagation();
                    setExpanded((current) => !current);
                    onToggle?.();
                }, children: [_jsx(ChevronDown, { className: cn("h-3 w-3 transition-transform duration-200", expanded && "rotate-180"), strokeWidth: 1.75 }), expanded ? "Hide details" : "Show details"] }), expanded && (_jsx("pre", { id: detailsId, className: "border-border-default bg-neutral-grey-5 text-neutral-grey-100 mt-2 max-h-36 overflow-auto rounded border p-2 font-mono text-[11px] leading-relaxed whitespace-pre-wrap break-words", children: details }))] }));
}
export function showExpandableErrorToast(error) {
    const normalized = normalizeErrorToastPayload(error);
    const details = normalized.details;
    if (!details) {
        toast.error(normalized.title);
        return;
    }
    // Sonner measures the toast once and pins it to that height while hovered
    // (height: var(--initial-height)), so a toast that grew after toggling the
    // details would oscillate between the two heights under the cursor.
    // Re-issuing the toast with the same id on every toggle swaps in a new
    // description element, which is what makes sonner re-measure.
    let toastId;
    const show = () => {
        toastId = toast.error(normalized.title, {
            id: toastId,
            description: _jsx(ExpandableErrorDetails, { details: details, onToggle: show }),
            duration: 10000,
        });
    };
    show();
}
//# sourceMappingURL=errorToast.js.map