"use client";
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { ChevronDown } from "lucide-react";
import { useState } from "react";
import { separateContentAndContext } from "../../utils/sentinelParser";
/**
 * Extracts the first plain object from a context string.
 * The triggerAction context format is: ["action description string", { formState }]
 */
function parseContextForDisplay(contextString) {
    if (!contextString)
        return {};
    try {
        const parsed = JSON.parse(contextString);
        if (Array.isArray(parsed)) {
            const stateObj = parsed.find((item) => item !== null && typeof item === "object" && !Array.isArray(item));
            return stateObj ?? {};
        }
        if (typeof parsed === "object" && parsed !== null) {
            return parsed;
        }
        return {};
    }
    catch {
        return {};
    }
}
function getEntries(state) {
    // Detect nested (form-named) shape
    const isNested = Object.values(state).some((v) => v !== null &&
        typeof v === "object" &&
        !("value" in v) &&
        Object.values(v).some((f) => f !== null && typeof f === "object" && "value" in f));
    const entries = [];
    if (isNested) {
        for (const [, fields] of Object.entries(state)) {
            if (typeof fields !== "object" || fields === null)
                continue;
            for (const [fieldName, field] of Object.entries(fields)) {
                if (field && typeof field === "object" && "value" in field) {
                    const val = field.value;
                    if (val !== undefined && val !== null && val !== "") {
                        entries.push({ label: fieldName, value: String(val) });
                    }
                }
            }
        }
    }
    else {
        for (const [fieldName, field] of Object.entries(state)) {
            if (field && typeof field === "object" && "value" in field) {
                const val = field.value;
                if (val !== undefined && val !== null && val !== "") {
                    entries.push({ label: fieldName, value: String(val) });
                }
            }
        }
    }
    return entries;
}
function FormDataAccordion({ state }) {
    const [isExpanded, setIsExpanded] = useState(false);
    const entries = getEntries(state);
    if (entries.length === 0)
        return null;
    return (_jsxs("div", { className: "openui-genui-user-message__form-state", children: [_jsxs("button", { className: "openui-genui-user-message__form-state-header", onClick: () => setIsExpanded((v) => !v), type: "button", children: [_jsxs("span", { className: "openui-genui-user-message__form-state-label", children: ["Form data (", entries.length, " ", entries.length === 1 ? "field" : "fields", ")"] }), _jsx(ChevronDown, { size: 14, className: `openui-genui-user-message__form-state-chevron${isExpanded ? " openui-genui-user-message__form-state-chevron--expanded" : ""}` })] }), isExpanded && (_jsx("div", { className: "openui-genui-user-message__form-state-content", children: entries.map(({ label, value }) => (_jsxs("div", { className: "openui-genui-user-message__form-field", children: [_jsxs("span", { className: "openui-genui-user-message__form-field-label", children: [label, ":"] }), _jsx("span", { className: "openui-genui-user-message__form-field-value", children: value })] }, label))) }))] }));
}
/**
 * Renders a user message, handling both plain text messages and
 * inline-formatted messages from form submissions.
 */
export const GenUIUserMessage = ({ message }) => {
    const rawContent = typeof message.content === "string" ? message.content : "";
    const { content: humanText, contextString } = separateContentAndContext(rawContent);
    const formState = parseContextForDisplay(contextString);
    const hasFormData = Object.keys(formState).length > 0;
    return (_jsx("div", { className: "openui-shell-thread-message-user", children: _jsxs("div", { className: "openui-genui-user-message", children: [hasFormData && _jsx(FormDataAccordion, { state: formState }), _jsx("div", { className: "openui-shell-thread-message-user__content", children: humanText && _jsx("div", { children: humanText }) })] }) }));
};
//# sourceMappingURL=GenUIUserMessage.js.map