import { useCallback, useEffect, useRef } from "react";
import { useEditContextRef } from "../client/editContext";
export function useFormFieldCaretPresence({ field, inputRef, readOnly, }) {
    const editContextRef = useEditContextRef();
    const lastSentKeyRef = useRef("clear");
    const pendingPublishFrameRef = useRef(null);
    const clearCaretPresence = useCallback(() => {
        if (pendingPublishFrameRef.current != null) {
            window.cancelAnimationFrame(pendingPublishFrameRef.current);
            pendingPublishFrameRef.current = null;
        }
        const editor = editContextRef.current;
        if (!editor || lastSentKeyRef.current === "clear")
            return;
        editor.sendSocketMessage({
            type: "caret-position",
            payload: { offset: null, source: "form-field" },
        });
        lastSentKeyRef.current = "clear";
    }, [editContextRef]);
    const publishCaretPresence = useCallback((element = inputRef.current) => {
        const editor = editContextRef.current;
        const fieldItem = field.descriptor.item;
        if (!editor || !element || readOnly || !fieldItem?.id) {
            clearCaretPresence();
            return;
        }
        const startOffset = element.selectionStart;
        const endOffset = element.selectionEnd;
        if (startOffset == null ||
            endOffset == null ||
            startOffset !== endOffset) {
            clearCaretPresence();
            return;
        }
        const nextKey = [
            field.id,
            fieldItem.id,
            fieldItem.language,
            fieldItem.version,
            startOffset,
        ].join(":");
        if (lastSentKeyRef.current === nextKey)
            return;
        editor.sendSocketMessage({
            type: "caret-position",
            payload: {
                fieldId: field.id,
                item: fieldItem,
                offset: startOffset,
                value: element.value,
                source: "form-field",
            },
        });
        lastSentKeyRef.current = nextKey;
    }, [
        clearCaretPresence,
        editContextRef,
        field.descriptor.item,
        field.id,
        inputRef,
        readOnly,
    ]);
    const publishCaretPresenceAfterSelectionSettles = useCallback((element = inputRef.current) => {
        if (pendingPublishFrameRef.current != null) {
            window.cancelAnimationFrame(pendingPublishFrameRef.current);
        }
        pendingPublishFrameRef.current = window.requestAnimationFrame(() => {
            pendingPublishFrameRef.current = null;
            publishCaretPresence(element);
        });
    }, [inputRef, publishCaretPresence]);
    useEffect(() => {
        return () => {
            if (pendingPublishFrameRef.current != null) {
                window.cancelAnimationFrame(pendingPublishFrameRef.current);
            }
            clearCaretPresence();
        };
    }, [clearCaretPresence]);
    useEffect(() => {
        if (readOnly) {
            clearCaretPresence();
        }
    }, [clearCaretPresence, readOnly]);
    return {
        clearCaretPresence,
        publishCaretPresence,
        publishCaretPresenceAfterSelectionSettles,
    };
}
//# sourceMappingURL=useFormFieldCaretPresence.js.map