/**
 * Utilities for extracting plain text context around a selection.
 * Used by AI text edit to display context in the dialog.
 */
const DEFAULT_CONTEXT_SIZE = 100;
/**
 * Slate uses various zero-width and invisible characters internally.
 * This regex matches them so we can strip them from displayed text.
 */
const SLATE_INVISIBLE_CHARS_REGEX = /[\u200B\u200C\u200D\uFEFF\u2060]/g;
/**
 * Sanitize text by removing Slate's internal invisible characters.
 * @param text - Text that may contain invisible characters
 * @returns Clean text with invisible characters removed
 */
export function sanitizeSlateText(text) {
    return text.replace(SLATE_INVISIBLE_CHARS_REGEX, "");
}
/**
 * Extract plain text context from a DOM container using global text offsets.
 * Works for contenteditable elements and Slate editor rendered output.
 *
 * @param container - The DOM container element
 * @param globalStartOffset - Global text offset where selection starts
 * @param globalEndOffset - Global text offset where selection ends
 * @param contextSize - Number of characters of context to extract (default 100)
 * @returns Object with contextBefore and contextAfter strings
 */
export function extractDOMSelectionContext(container, globalStartOffset, globalEndOffset, contextSize = DEFAULT_CONTEXT_SIZE) {
    // Get all text content from the container
    const fullText = container.textContent || "";
    // Extract context before the selection
    const beforeStart = Math.max(0, globalStartOffset - contextSize);
    let contextBefore = fullText.slice(beforeStart, globalStartOffset);
    // Extract context after the selection
    let contextAfter = fullText.slice(globalEndOffset, globalEndOffset + contextSize);
    // Sanitize Slate's invisible characters from the context
    contextBefore = sanitizeSlateText(contextBefore);
    contextAfter = sanitizeSlateText(contextAfter);
    return { contextBefore, contextAfter };
}
/**
 * Extract plain text context from a string value using offsets.
 * Works for input and textarea elements.
 *
 * @param value - The full text value
 * @param startOffset - Offset where selection starts
 * @param endOffset - Offset where selection ends
 * @param contextSize - Number of characters of context to extract (default 100)
 * @returns Object with contextBefore and contextAfter strings
 */
export function extractPlainTextContext(value, startOffset, endOffset, contextSize = DEFAULT_CONTEXT_SIZE) {
    // Extract context before the selection
    const beforeStart = Math.max(0, startOffset - contextSize);
    let contextBefore = value.slice(beforeStart, startOffset);
    // Extract context after the selection
    let contextAfter = value.slice(endOffset, endOffset + contextSize);
    // Sanitize any invisible characters (for consistency with DOM extraction)
    contextBefore = sanitizeSlateText(contextBefore);
    contextAfter = sanitizeSlateText(contextAfter);
    return { contextBefore, contextAfter };
}
//# sourceMappingURL=selectionContext.js.map