import { PathFinder } from '@blockeditor/block-std';
import { assertExists } from '@blockeditor/global/utils';
function getPrevSibling(std, path, filter) {
    const view = std.view;
    const nextView = view.findPrev(path, node => {
        if (node.type !== 'block') {
            return;
        }
        if (filter && !filter(node.view)) {
            return;
        }
        return true;
    });
    if (!nextView)
        return null;
    return view.viewFromPath('block', nextView.path);
}
function getLastGrandChild(std, blockElement) {
    const view = std.view;
    let output = blockElement;
    view.walkThrough((node, _index, parent) => {
        if (node.children.filter(n => n.type === 'block').length === 0 &&
            parent.children.filter(n => n.type === 'block').at(-1) === node) {
            output = node.view;
            return true;
        }
        return;
    }, blockElement.path);
    return output;
}
function getPrevBlock(std, path, filter) {
    const view = std.view;
    let prev = getPrevSibling(std, path, filter);
    if (!prev) {
        return null;
    }
    const block = view.viewFromPath('block', path);
    if (!block)
        return null;
    if (!prev.contains(block)) {
        prev = getLastGrandChild(std, prev);
    }
    if (prev && !PathFinder.equals(prev.path, path)) {
        return prev;
    }
    return null;
}
export const getPrevBlockCommand = (ctx, next) => {
    const path = ctx.path ?? ctx.currentSelectionPath;
    assertExists(path, '`path` is required, you need to pass it in args or ctx before adding this command to the pipeline.');
    const prevBlock = getPrevBlock(ctx.std, path, ctx.filter);
    if (prevBlock) {
        next({ prevBlock });
    }
};
//# sourceMappingURL=get-prev-block.js.map