import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import react, { forwardRef, useCallback, useImperativeHandle, useLayoutEffect, useRef } from "react";
import { AutopilotChatResourcePickerProvider, useAutopilotChatResourcePicker } from "../../providers/resource-picker-provider.js";
import { ResourcePickerDropdown } from "./chat-input-resource-picker.js";
import { TipTapEditor } from "./tiptap/index.js";
const chat_input_editor_EditorWithPicker = /*#__PURE__*/ forwardRef(function(props, ref) {
    const { value, placeholder, minRows, maxRows, lineHeight, onChange, onKeyDown, editorRef } = props;
    const dropdownRef = useRef(null);
    const { isOpenRef, drillDown, open, close, handleMentionEnd, setQuery } = useAutopilotChatResourcePicker();
    const drillDownRef = useRef(drillDown);
    const valueRef = useRef(value);
    useLayoutEffect(()=>{
        drillDownRef.current = drillDown;
        valueRef.current = value;
    }, [
        drillDown,
        value
    ]);
    const handleChange = useCallback((newValue)=>{
        if (isOpenRef.current && drillDownRef.current && !newValue.includes('@')) close();
        onChange(newValue);
    }, [
        close,
        onChange
    ]);
    const handleKeyDown = useCallback((event)=>{
        if (isOpenRef.current && dropdownRef.current?.handleKeyDown(event)) return true;
        return onKeyDown?.(event) ?? false;
    }, [
        onKeyDown
    ]);
    const openResourcePicker = useCallback(()=>{
        editorRef.current?.triggerMention();
    }, [
        editorRef
    ]);
    useImperativeHandle(ref, ()=>({
            focus: ()=>editorRef.current?.focus(),
            getSerializedContent: ()=>editorRef.current?.getSerializedContent() ?? valueRef.current,
            clear: ()=>editorRef.current?.clear(),
            openResourcePicker
        }), [
        editorRef,
        openResourcePicker
    ]);
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            /*#__PURE__*/ jsx(TipTapEditor, {
                ref: editorRef,
                value: value,
                placeholder: placeholder,
                minRows: minRows,
                maxRows: maxRows,
                lineHeight: lineHeight,
                onChange: handleChange,
                onKeyDown: handleKeyDown,
                onMentionStart: open,
                onMentionEnd: handleMentionEnd,
                onMentionQueryChange: setQuery
            }),
            /*#__PURE__*/ jsx(ResourcePickerDropdown, {
                ref: dropdownRef
            })
        ]
    });
});
const chat_input_editor_ChatInputEditor = /*#__PURE__*/ react.memo(/*#__PURE__*/ forwardRef(function(props, ref) {
    const editorRef = useRef(null);
    const handleResourceSelect = useCallback((resource, range)=>{
        editorRef.current?.insertResource(resource, range);
        editorRef.current?.focus();
    }, []);
    const handleDrillDown = useCallback(()=>{
        editorRef.current?.clearMentionQuery();
    }, []);
    const handleClose = useCallback(()=>{
        editorRef.current?.exitMention();
    }, []);
    return /*#__PURE__*/ jsx(AutopilotChatResourcePickerProvider, {
        onResourceSelect: handleResourceSelect,
        onDrillDown: handleDrillDown,
        onClose: handleClose,
        children: /*#__PURE__*/ jsx(chat_input_editor_EditorWithPicker, {
            ...props,
            ref: ref,
            editorRef: editorRef
        })
    });
}));
chat_input_editor_ChatInputEditor.displayName = 'ChatInputEditor';
export { chat_input_editor_ChatInputEditor as ChatInputEditor };
