import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createContext, useContext, useEffect } from 'react';
import { useEditor } from '@tiptap/react';
import useExtensions from './hooks/useExtensions';
import { getTextAreaPlaceholderExtension, isActiveTextAreaEditor, } from './utils/editor';
const TextAreaContext = createContext(undefined);
export const TextAreaHandlers = ({ disabled = false, placeholder, }) => {
    const { editor } = useTextArea();
    useEffect(() => {
        if (!isActiveTextAreaEditor(editor))
            return;
        editor.setEditable(!disabled);
    }, [disabled, editor]);
    useEffect(() => {
        if (placeholder === '' || !isActiveTextAreaEditor(editor))
            return;
        const placeholderExtension = getTextAreaPlaceholderExtension(editor.extensionManager);
        if (!placeholderExtension)
            return;
        placeholderExtension.options.placeholder = placeholder;
        editor.view.dispatch(editor.state.tr);
    }, [editor, placeholder]);
    return null;
};
export const TextAreaProvider = ({ children, content, disabled = false, readOnly = false, handlePaste, handleDrop, onChange, onBlur, onFocus, placeholder, slotProps, immediatelyRender = true, variablesEnabled = false, characterLimit, }) => {
    const isEditable = !disabled && !readOnly;
    const extensions = useExtensions({
        placeholder,
        handlePaste,
        handleDrop,
        slotProps,
        variablesEnabled,
        characterLimit,
    });
    const editor = useEditor({
        extensions,
        content,
        editable: isEditable,
        onUpdate: ({ editor: currentEditor }) => {
            onChange?.(currentEditor.isEmpty ? '' : currentEditor.getHTML());
        },
        onBlur,
        onFocus,
        shouldRerenderOnTransaction: true,
        immediatelyRender,
    });
    return (_jsxs(TextAreaContext.Provider, { value: { editor, slotProps }, children: [children, _jsx(TextAreaHandlers, { disabled: !isEditable, placeholder: placeholder })] }));
};
export const useTextArea = () => {
    const context = useContext(TextAreaContext);
    if (context === undefined) {
        throw new Error('useTextArea must be used within a TextAreaProvider');
    }
    return context;
};
