import { extractPlainTextContext } from "../utils/selectionContext";
import { applyExpandedSelectionToContentEditable, applyExpandedSelectionToInput, } from "../utils/expandSelectionAtCaret";
function findEditableContainer(activeEl) {
    if (activeEl.getAttribute("contenteditable") === "true" ||
        activeEl.classList.contains("slate-editable")) {
        return activeEl;
    }
    return (activeEl.closest('[contenteditable="true"]') ??
        activeEl.closest(".slate-editable"));
}
export function prepareInlineAiTextSelection(editContext) {
    if (!editContext) {
        return {};
    }
    const activeEl = document.activeElement;
    if (!activeEl) {
        return {};
    }
    const existingRange = editContext.selectedRange;
    if (existingRange?.text?.trim()) {
        return { selectionRange: existingRange };
    }
    const focusedField = editContext.focusedField;
    if (!focusedField) {
        return {};
    }
    if (activeEl instanceof HTMLInputElement ||
        activeEl instanceof HTMLTextAreaElement) {
        const bounds = applyExpandedSelectionToInput(activeEl);
        if (!bounds?.text) {
            return {};
        }
        const { contextBefore, contextAfter } = extractPlainTextContext(activeEl.value, bounds.start, bounds.end);
        const selectionRange = {
            itemId: focusedField.item.id,
            fieldId: focusedField.fieldId,
            language: focusedField.item.language,
            version: focusedField.item.version,
            startOffset: bounds.start,
            endOffset: bounds.end,
            text: bounds.text,
            element: activeEl,
            contextBefore,
            contextAfter,
        };
        editContext.setSelectedRange(selectionRange);
        return { selectionRange };
    }
    const editableContainer = findEditableContainer(activeEl);
    if (!editableContainer) {
        return {};
    }
    const bounds = applyExpandedSelectionToContentEditable(editableContainer);
    if (!bounds?.text) {
        return {};
    }
    const selection = window.getSelection();
    const range = selection && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
    const clientRect = range?.getBoundingClientRect();
    const selectionRange = {
        itemId: focusedField.item.id,
        fieldId: focusedField.fieldId,
        language: focusedField.item.language,
        version: focusedField.item.version,
        startOffset: bounds.start,
        endOffset: bounds.end,
        text: bounds.text,
        contextBefore: undefined,
        contextAfter: undefined,
        clientRect: clientRect
            ? {
                x: clientRect.left,
                y: clientRect.top,
                width: clientRect.width,
                height: clientRect.height,
                top: clientRect.top,
                right: clientRect.right,
                bottom: clientRect.bottom,
                left: clientRect.left,
            }
            : undefined,
    };
    editContext.setSelectedRange(selectionRange);
    return { selectionRange };
}
//# sourceMappingURL=prepareInlineAiTextSelection.js.map