"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClaudeChatWidget = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const ClaudeChatWidget = ({ lang = 'en', theme = '', onResult, initialMessages = [] }) => {
    const [messages, setMessages] = (0, react_1.useState)(initialMessages);
    const [input, setInput] = (0, react_1.useState)('');
    const [loading, setLoading] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)('');
    const handleSend = async () => {
        if (!input)
            return;
        setLoading(true);
        setError('');
        const newMessages = [...messages, { role: 'user', content: input }];
        setMessages(newMessages);
        try {
            const response = await fetch('/api/llm-cloud/eca-action', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    action_id: 'claude_generate_text',
                    params: { prompt: input, lang },
                }),
            });
            const data = await response.json();
            if (data.result) {
                setMessages([...newMessages, { role: 'claude', content: data.result }]);
                if (onResult)
                    onResult(data.result);
                setInput('');
            }
            else if (data.error) {
                setError(data.error);
            }
        }
        catch (e) {
            setError('Failed to send message.');
        }
        setLoading(false);
    };
    return ((0, jsx_runtime_1.jsxs)("div", { className: `claude-chat-widget ${theme}`.trim(), children: [(0, jsx_runtime_1.jsx)("div", { className: "claude-chat-messages", "aria-live": "polite", children: messages.map((msg, idx) => ((0, jsx_runtime_1.jsxs)("div", { className: msg.role, "aria-label": msg.role, tabIndex: 0, children: [msg.role, ": ", msg.content] }, idx))) }), (0, jsx_runtime_1.jsx)("input", { type: "text", value: input, onChange: e => setInput(e.target.value), placeholder: "Type your message...", disabled: loading, "aria-label": "Chat input", onKeyDown: e => { if (e.key === 'Enter')
                    handleSend(); } }), (0, jsx_runtime_1.jsxs)("button", { onClick: handleSend, disabled: loading || !input, "aria-busy": loading, "aria-label": "Send message to Claude", children: [loading ? (0, jsx_runtime_1.jsx)("span", { className: "spinner", "aria-hidden": "true" }) : null, loading ? 'Sending...' : 'Send'] }), error && (0, jsx_runtime_1.jsx)("div", { className: "error", role: "alert", children: error })] }));
};
exports.ClaudeChatWidget = ClaudeChatWidget;
//# sourceMappingURL=ClaudeChatWidget.js.map