import { getTextNodesFromElement } from '@blockeditor/inline';
import { applyCarets, getCurrentTextSelectionCarets } from '../utils.js';
export const changeTextSelectionToBlockStartEnd = (ctx, next) => {
    const { tail, focusBlock } = ctx;
    if (!focusBlock) {
        return;
    }
    const texts = getTextNodesFromElement(focusBlock);
    const text = tail ? texts[texts.length - 1] : texts[0];
    if (!text) {
        return;
    }
    const nextEndCaret = {
        node: text,
        offset: tail ? text.textContent?.length ?? 0 : 0,
    };
    const currentTextSelectionCarets = getCurrentTextSelectionCarets();
    if (!currentTextSelectionCarets) {
        return;
    }
    const { startCaret, endCaret } = currentTextSelectionCarets;
    if (endCaret.node === nextEndCaret.node &&
        endCaret.offset === nextEndCaret.offset) {
        return;
    }
    const result = applyCarets(startCaret, nextEndCaret);
    if (!result) {
        return;
    }
    return next();
};
//# sourceMappingURL=change-text-selection-to-block-start-end.js.map