import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Button } from "../../components/ui/button";
import { Textarea } from "../../components/ui/textarea";
import { Send } from "lucide-react";
import { ActionButton } from "../../components/ActionButton";
export function CommentEditor({ initialText = "", onSave, onCancel, isSaving = false, availableTags = [], selectedTags: initialSelectedTags = [], placeholder = "Write your comment...", autoFocus = true, submitLabel = "Save", cancelLabel = "Cancel", showTagSelector = true, compact = false, onKeyDown, }) {
    const [text, setText] = React.useState(initialText);
    const [selectedTags, setSelectedTags] = React.useState(initialSelectedTags);
    const textareaRef = React.useRef(null);
    React.useEffect(() => {
        if (autoFocus && textareaRef.current) {
            // Focus after a longer delay to ensure the component is fully mounted, especially for dynamically created popovers
            const timer = setTimeout(() => {
                textareaRef.current?.focus();
                // Move cursor to end
                const len = textareaRef.current?.value.length || 0;
                textareaRef.current?.setSelectionRange(len, len);
            }, 200);
            return () => clearTimeout(timer);
        }
    }, [autoFocus]);
    const handleKeyDown = (e) => {
        // Call custom handler first if provided
        if (onKeyDown) {
            onKeyDown(e);
        }
        // Default behavior: Enter to submit (unless Shift is held)
        if (e.key === "Enter" && !e.shiftKey && !e.defaultPrevented) {
            e.preventDefault();
            handleSave();
        }
        // Escape to cancel
        else if (e.key === "Escape" && !e.defaultPrevented) {
            e.preventDefault();
            onCancel();
        }
    };
    const handleSave = () => {
        const trimmedText = text.trim();
        if (trimmedText && !isSaving) {
            onSave(trimmedText, selectedTags);
        }
    };
    return (_jsxs("div", { className: "space-y-3", children: [_jsx(Textarea, { ref: textareaRef, "data-testid": "add-comment-textarea", value: text, onChange: (e) => setText(e.target.value), onKeyDown: handleKeyDown, placeholder: placeholder, className: `resize-none ${compact ? "min-h-[60px]" : "min-h-[100px]"}`, rows: compact ? 2 : 3, disabled: isSaving, autoFocus: autoFocus }), showTagSelector && availableTags.length > 0 && (_jsxs("div", { className: "space-y-2", children: [_jsx("div", { className: "text-muted-foreground text-xs", children: "Tags" }), _jsx("div", { className: "flex flex-wrap gap-2", children: availableTags.map((tag) => {
                            const isSelected = selectedTags.includes(tag.label);
                            return (_jsx(Button, { type: "button", size: "sm", variant: isSelected ? "secondary" : "outline", className: "h-6 px-2 text-xs", onClick: () => {
                                    setSelectedTags((prev) => prev.includes(tag.label)
                                        ? prev.filter((l) => l !== tag.label)
                                        : [...prev, tag.label]);
                                }, style: {
                                    borderColor: isSelected ? tag.color : undefined,
                                    backgroundColor: isSelected ? tag.color + "22" : undefined,
                                }, disabled: isSaving, children: tag.label }, tag.label));
                        }) })] })), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(ActionButton, { variant: "outline", onClick: onCancel, disabled: isSaving, "data-testid": "add-comment-cancel", children: cancelLabel }), _jsxs(ActionButton, { onClick: handleSave, disabled: !text.trim() || isSaving, className: "flex items-center gap-1", "data-testid": "add-comment-submit", children: [compact && _jsx(Send, { size: 14, strokeWidth: 1 }), isSaving ? "Saving..." : submitLabel] })] }), !compact && (_jsx("div", { className: "text-xs text-gray-500", children: "Press Enter to submit, Shift+Enter for new line, Escape to cancel" }))] }));
}
//# sourceMappingURL=CommentEditor.js.map