import { padInsertedTextWhitespace, preserveSelectionBoundaryWhitespace, } from "../utils/textInsertionWhitespace";
/**
 * Builds the bridge command that replaces a canvas text selection with
 * AI-generated text, or `null` when the range did not come from the bridge.
 *
 * Canvas selections carry the *rendered* selection text (uppercased by CSS
 * `text-transform`, block breaks flattened to "\n"), so the generated text
 * cannot be spliced into the raw field value with an `indexOf` — in rich
 * text fields the raw value additionally contains markup the rendered text
 * never matches. The editing host instead restores the original selection
 * from the payload and performs a native `insertText`, which preserves the
 * surrounding markup and reports the new value through the regular
 * inline-edit pipeline.
 */
export function buildBridgeInsertTextCommand(range, newText) {
    if (!range?.elementKey || !range.fieldId)
        return null;
    const value = range.text
        ? preserveSelectionBoundaryWhitespace(range.text, newText)
        : padInsertedTextWhitespace(newText, range.contextBefore?.slice(-1) ?? "", range.contextAfter?.charAt(0) ?? "");
    return {
        elementKey: range.elementKey,
        fieldId: range.fieldId,
        command: "insertText",
        value,
        selectedText: range.text,
        selectionStartOffset: range.startOffset,
        selectionEndOffset: range.endOffset,
    };
}
//# sourceMappingURL=bridgeInlineTextReplace.js.map