"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyUpdateTree = exports._keyUpdateTree = void 0;
const write_1 = require("../client/event/keyboard/write");
const parser_1 = require("../spec/parser");
const clone_1 = require("./clone");
const _keyUpdateTree = (root, path, value, key, selectionStart, selectionEnd, shiftKey) => {
    const data = (0, parser_1.getTree)(value);
    const valid = (0, parser_1._isValidTree)(data);
    const lastIndex = path[path.length - 1];
    let preventDefault = false;
    let nextRoot = (0, clone_1.clone)(root);
    let nextPath = (0, clone_1.clone)(path);
    let nextSelectionStart = selectionStart;
    let nextSelectionEnd = selectionEnd;
    let nextDirection = undefined;
    const moveLeft = () => {
        const parentPath = (0, parser_1.getParentPath)(path);
        const leftLeafPath = (0, parser_1._getNextLeafPath)(root, path, -1);
        if (leftLeafPath) {
            preventDefault = true;
            const leftLeafNode = (0, parser_1._getNodeAtPath)(nextRoot, leftLeafPath);
            if (value === '') {
                nextRoot = (0, parser_1._removeNodeAt)(nextRoot, path);
            }
            let nextNode;
            nextPath = leftLeafPath;
            nextNode = leftLeafNode;
            nextSelectionStart = leftLeafNode.value.length;
            nextSelectionEnd = nextSelectionStart;
            if (!nextPath || !(0, parser_1._getNodeAtPath)(nextRoot, nextPath)) {
                const parentPath = (0, parser_1.getParentPath)(path);
                nextPath = parentPath;
            }
            else {
                if (leftLeafNode.type === parser_1.TreeNodeType.ArrayLiteral ||
                    leftLeafNode.type === parser_1.TreeNodeType.ObjectLiteral) {
                    nextPath = [...nextPath, leftLeafNode.children.length];
                    if (!leftLeafNode.children.length) {
                        nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                        nextSelectionStart = 0;
                        nextSelectionEnd = nextSelectionStart;
                    }
                }
            }
        }
        else {
            if (parentPath) {
                const parent = (0, parser_1._getNodeAtPath)(root, parentPath);
                if (parent.type === parser_1.TreeNodeType.ArrayLiteral ||
                    parent.type === parser_1.TreeNodeType.ObjectLiteral) {
                    const grandParentPath = (0, parser_1.getParentPath)(parentPath);
                    if (grandParentPath) {
                        const grandParent = (0, parser_1._getNodeAtPath)(root, grandParentPath);
                        if (grandParent) {
                            if (grandParent.type === parser_1.TreeNodeType.ArrayLiteral ||
                                grandParent.type === parser_1.TreeNodeType.ObjectLiteral) {
                                preventDefault = true;
                                nextPath = [...grandParentPath, 0];
                                nextRoot = (0, parser_1._insertNodeAt)(root, nextPath, (0, parser_1.getTree)(''));
                                nextSelectionStart = 0;
                                nextSelectionEnd = 0;
                            }
                        }
                    }
                }
            }
        }
    };
    const moveRight = () => {
        const rightLeafPath = (0, parser_1._getNextLeafPath)(root, path, 1);
        const parentPath = (0, parser_1.getParentPath)(path);
        if (rightLeafPath) {
            if (value === '') {
                nextRoot = (0, parser_1._removeNodeAt)(nextRoot, path);
                nextPath =
                    (0, parser_1._getNextLeafPath)(nextRoot, path, 1) ??
                        (0, parser_1._getNextLeafPath)(nextRoot, [], 1);
                if (nextPath) {
                    const nextPathNode = (0, parser_1._getNodeAtPath)(nextRoot, nextPath);
                    if (nextPathNode) {
                        if (nextPathNode.type === parser_1.TreeNodeType.ArrayLiteral ||
                            nextPathNode.type === parser_1.TreeNodeType.ObjectLiteral) {
                            nextPath = [...nextPath, 0];
                        }
                    }
                }
            }
            else {
                nextPath = rightLeafPath;
                if (!nextPath || !(0, parser_1._getNodeAtPath)(nextRoot, nextPath)) {
                    nextPath = parentPath;
                }
                const nextFocusNode = (0, parser_1._getNodeAtPath)(nextRoot, nextPath);
                if (nextFocusNode &&
                    (nextFocusNode.type === parser_1.TreeNodeType.ArrayLiteral ||
                        nextFocusNode.type === parser_1.TreeNodeType.ObjectLiteral) &&
                    nextFocusNode.children.length === 0) {
                    preventDefault = true;
                    nextPath = [...nextPath, 0];
                }
            }
            preventDefault = true;
            nextSelectionStart = 0;
            nextSelectionEnd = 0;
        }
        else {
            if (parentPath) {
                const parent = (0, parser_1._getNodeAtPath)(root, parentPath);
                if (parent.type === parser_1.TreeNodeType.ArrayLiteral ||
                    parent.type === parser_1.TreeNodeType.ObjectLiteral) {
                    const grandParentPath = (0, parser_1.getParentPath)(parentPath);
                    if (value === '') {
                        nextRoot = (0, parser_1._removeNodeAt)(nextRoot, path);
                    }
                    if (grandParentPath) {
                        const grandParent = (0, parser_1._getNodeAtPath)(root, grandParentPath);
                        if (grandParent) {
                            if (grandParent.type === parser_1.TreeNodeType.ArrayLiteral ||
                                grandParent.type === parser_1.TreeNodeType.ObjectLiteral) {
                                preventDefault = true;
                                nextPath = [...grandParentPath, grandParent.children.length];
                                nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                                nextSelectionStart = 0;
                                nextSelectionEnd = 0;
                            }
                        }
                    }
                }
            }
        }
    };
    if (key === 'Backspace') {
        if (value === '' && path.length > 0) {
            const parentPath = (0, parser_1.getParentPath)(path);
            const parent = (0, parser_1._getNodeAtPath)(root, parentPath);
            let invert = false;
            if (parent.type === parser_1.TreeNodeType.ArrayLiteral ||
                parent.type === parser_1.TreeNodeType.ObjectLiteral) {
                if (parent.children.length === 0) {
                    // `{|}` + Backspace = ``
                    preventDefault = true;
                    nextRoot = (0, parser_1._updateNodeAt)(root, parentPath, (0, parser_1.getTree)(''));
                    nextPath = parentPath;
                }
                else {
                    preventDefault = true;
                    nextRoot = (0, parser_1._removeNodeAt)(root, path);
                    nextPath = (0, parser_1._getNextLeafPath)(root, path, -1);
                    if (!nextPath) {
                        nextPath = (0, parser_1._getNextLeafPath)(nextRoot, path, 1);
                        if (nextPath) {
                            invert = true;
                        }
                    }
                    if (!nextPath) {
                        nextPath = [0];
                    }
                }
            }
            else if (parent.type === parser_1.TreeNodeType.KeyValue) {
                preventDefault = true;
                nextRoot = (0, parser_1._removeNodeAt)(root, path);
                nextPath = parentPath;
                invert = lastIndex === 0;
            }
            let nextFocusNode = (0, parser_1._getNodeAtPath)(nextRoot, nextPath);
            if (nextFocusNode) {
                let lastLeafPath = (0, parser_1._getLastLeafPath)(nextFocusNode);
                let lastLeaf = (0, parser_1._getLastLeaf)(nextFocusNode);
                preventDefault = true;
                if ((0, parser_1.isCompositeType)(lastLeaf.type)) {
                    const leafPath = [...nextPath, ...lastLeafPath];
                    lastLeaf = (0, parser_1._insertNodeAt)(lastLeaf, [0], (0, parser_1.getTree)(''));
                    nextRoot = (0, parser_1._updateNodeAt)(nextRoot, leafPath, lastLeaf);
                    nextPath = [...leafPath, 0];
                    nextSelectionStart = 0;
                    nextSelectionEnd = 0;
                }
                else {
                    if (invert) {
                        nextSelectionStart = 0;
                        nextSelectionEnd = nextSelectionStart;
                    }
                    else {
                        nextSelectionStart = lastLeaf.value.length;
                        nextSelectionEnd = nextSelectionStart;
                    }
                }
            }
        }
        else if ((value === `""` || value === `''` || value === '``') &&
            selectionStart === 1) {
            preventDefault = true;
            nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(''));
        }
        else {
            const parentPath = (0, parser_1.getParentPath)(path);
            if (parentPath) {
                const parent = (0, parser_1._getNodeAtPath)(root, parentPath);
                const parentLastIndex = parentPath[parentPath.length - 1];
                if (parent.type === parser_1.TreeNodeType.KeyValue) {
                    if (lastIndex === 0) {
                        if (selectionStart === 0 && selectionEnd === 0) {
                            if (parentLastIndex > 0) {
                                moveLeft();
                            }
                        }
                        else {
                            if (selectionEnd - selectionStart === value.length ||
                                (selectionStart === selectionEnd &&
                                    selectionStart === 1 &&
                                    value.length === 1)) {
                                preventDefault = true;
                                nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(''));
                                nextPath = path;
                                nextSelectionStart = 0;
                                nextSelectionEnd = 0;
                            }
                        }
                    }
                    else if (lastIndex === 1) {
                        if (selectionStart === 0 && selectionEnd === 0) {
                            const key = (0, parser_1._getNodeAtPath)(parent, [0]);
                            const nextValue = key.value + value;
                            nextRoot = (0, parser_1._updateNodeAt)(root, parentPath, (0, parser_1.getTree)(nextValue));
                            preventDefault = true;
                            nextPath = parentPath;
                            nextSelectionStart = key.value.length;
                            nextSelectionEnd = nextSelectionStart;
                        }
                    }
                }
                else if (parent.type === parser_1.TreeNodeType.ArrayLiteral) {
                    if (lastIndex > 0) {
                        if (selectionStart === 0 && selectionEnd === 0) {
                            const leftPath = (0, parser_1._getNextSiblingPath)(nextRoot, path, -1);
                            const left = (0, parser_1._getNodeAtPath)(nextRoot, leftPath);
                            const nextValue = left.value + value;
                            preventDefault = true;
                            nextRoot = (0, parser_1._removeNodeAt)(nextRoot, path);
                            nextRoot = (0, parser_1._updateNodeAt)(nextRoot, leftPath, (0, parser_1.getTree)(nextValue));
                            nextPath = leftPath;
                            nextSelectionStart = left.value.length;
                            nextSelectionEnd = nextSelectionStart;
                        }
                    }
                    else {
                        if (selectionStart === 0 && selectionEnd === 0) {
                            preventDefault = true;
                            const nextValue = parent.children
                                .map(({ value }) => value)
                                .join(',');
                            nextRoot = (0, parser_1._updateNodeAt)(nextRoot, parentPath, (0, parser_1.getTree)(nextValue));
                            nextPath = parentPath;
                            nextSelectionStart = 0;
                            nextSelectionEnd = 0;
                        }
                    }
                }
                else if (parent.type === parser_1.TreeNodeType.ObjectLiteral) {
                    if (lastIndex > 0 && selectionStart === 0 && selectionEnd === 0) {
                        const previous = (0, parser_1._getNodeAtPath)(parent, [lastIndex - 1]);
                        const previousPath = [...parentPath, lastIndex - 1];
                        nextRoot = (0, parser_1._removeNodeAt)(root, path);
                        nextRoot = (0, parser_1._updateNodeAt)(nextRoot, previousPath, (0, parser_1.getTree)(previous.value + value));
                        if (previous.type === parser_1.TreeNodeType.KeyValue) {
                            nextPath = [...previousPath, 1];
                        }
                        else {
                            nextPath = previousPath;
                        }
                        preventDefault = true;
                        nextSelectionStart = previous.value.length;
                        nextSelectionEnd = nextSelectionStart;
                    }
                    else {
                        const textState = (0, write_1.processBackspace)({
                            value,
                            selectionStart,
                            selectionEnd,
                            selectionDirection: 'none',
                        }, { ctrlKey: false, altKey: false, shiftKey: false });
                        const nextValue = (0, parser_1.getTree)(textState.value);
                        nextRoot = (0, parser_1._updateNodeAt)(root, path, nextValue);
                        nextSelectionStart = textState.selectionStart;
                        nextSelectionEnd = textState.selectionEnd;
                        const nextNode = (0, parser_1._getNodeAtPath)(nextRoot, path);
                        if (nextNode) {
                            if (nextNode.type === parser_1.TreeNodeType.KeyValue) {
                                preventDefault = true;
                                nextPath = [...path, 0];
                            }
                        }
                    }
                }
            }
        }
    }
    else if (key === 'ArrowLeft') {
        if (selectionStart === 0 && selectionStart === selectionEnd) {
            moveLeft();
        }
    }
    else if (key === 'ArrowRight') {
        if (selectionStart === value.length && selectionStart === selectionEnd) {
            moveRight();
        }
    }
    else if (key === 'ArrowUp') {
        //
    }
    else if (key === 'ArrowDown') {
        //
    }
    else if (key === 'Tab') {
        //
    }
    else if (key === ',') {
        const parentPath = (0, parser_1.getParentPath)(path);
        if (parentPath) {
            const parent = (0, parser_1._getNodeAtPath)(root, parentPath);
            if ((0, parser_1.isCompositeType)(parent.type) &&
                (data.type !== parser_1.TreeNodeType.StringLiteral ||
                    selectionStart === value.length)) {
                if (parent.type === parser_1.TreeNodeType.ArrayLiteral ||
                    parent.type === parser_1.TreeNodeType.ObjectLiteral) {
                    preventDefault = true;
                    if (value === '') {
                        //
                    }
                    else {
                        const first = value.substring(0, selectionStart);
                        const second = value.substring(selectionEnd);
                        const rightPath = (0, clone_1.clone)(nextPath);
                        rightPath[rightPath.length - 1] += 1;
                        const hasRight = !!(0, parser_1._getNodeAtPath)(nextRoot, rightPath);
                        const insert = selectionStart > 0 && (selectionStart < value.length || !hasRight);
                        if (hasRight || insert) {
                            nextPath[nextPath.length - 1] += 1;
                        }
                        if (insert) {
                            nextRoot = (0, parser_1._updateNodeAt)(nextRoot, path, (0, parser_1.getTree)(first));
                            nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(second));
                        }
                        nextSelectionStart = 0;
                        nextSelectionEnd = 0;
                    }
                }
                else if (parent.type === parser_1.TreeNodeType.KeyValue) {
                    preventDefault = true;
                    const parentPathLastIndex = parentPath[parentPath.length - 1];
                    const grandParentPath = (0, parser_1.getParentPath)(parentPath);
                    if (selectionStart === value.length) {
                        nextPath = [...grandParentPath, parentPathLastIndex + 1];
                        nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                        nextSelectionStart = 0;
                        nextSelectionEnd = 0;
                    }
                    else {
                        if (value === '') {
                            nextRoot = (0, parser_1._removeNodeAt)(root, path);
                        }
                        nextPath = [...parentPath];
                        if (selectionStart > 0 || selectionStart !== selectionEnd) {
                            nextPath[nextPath.length - 1] += 1;
                        }
                        nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                        nextSelectionStart = 0;
                        nextSelectionEnd = 0;
                    }
                }
            }
        }
    }
    else if (key === ']' || key === '}') {
        if (valid || value === '') {
            let parentPath = (0, parser_1.getParentPath)(path);
            if (parentPath) {
                const parent = (0, parser_1._getNodeAtPath)(root, parentPath);
                const grandpaPath = (0, parser_1.getParentPath)(parentPath);
                if ((parent.type === parser_1.TreeNodeType.ArrayLiteral && key === ']') ||
                    (parent.type === parser_1.TreeNodeType.ObjectLiteral && key === '}')) {
                    if (value === '') {
                        preventDefault = true;
                        nextRoot = (0, parser_1._removeNodeAt)(nextRoot, path);
                    }
                    if (grandpaPath) {
                        const grandpa = (0, parser_1._getNodeAtPath)(nextRoot, grandpaPath);
                        if (grandpa.type === parser_1.TreeNodeType.ArrayLiteral ||
                            grandpa.type === parser_1.TreeNodeType.ObjectLiteral) {
                            preventDefault = true;
                            nextPath = [...parentPath];
                            nextPath[nextPath.length - 1] += 1;
                            nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                            nextSelectionStart = 0;
                            nextSelectionEnd = 0;
                        }
                        else if (grandpa.type === parser_1.TreeNodeType.KeyValue) {
                            preventDefault = true;
                            nextPath = [...grandpaPath];
                            nextPath[nextPath.length - 1] += 1;
                            nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                            nextSelectionStart = 0;
                            nextSelectionEnd = 0;
                        }
                    }
                    else {
                        preventDefault = true;
                    }
                }
                else if (parent.type === parser_1.TreeNodeType.KeyValue && key === '}') {
                    const grangrandpaPath = (0, parser_1.getParentPath)(grandpaPath);
                    if (grangrandpaPath) {
                        preventDefault = true;
                        if (value === '') {
                            nextRoot = (0, parser_1._removeNodeAt)(root, path);
                        }
                        nextPath = [...grandpaPath];
                        nextPath[nextPath.length - 1] += 1;
                        const grangrandpa = (0, parser_1._getNodeAtPath)(nextRoot, grangrandpaPath);
                        if (grangrandpa.type === parser_1.TreeNodeType.ArrayLiteral ||
                            grangrandpa.type === parser_1.TreeNodeType.ObjectLiteral) {
                            preventDefault = true;
                            nextPath = [...grandpaPath];
                            nextPath[nextPath.length - 1] += 1;
                            nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                            nextSelectionStart = 0;
                            nextSelectionEnd = 0;
                        }
                        else if (grangrandpa.type === parser_1.TreeNodeType.KeyValue) {
                            preventDefault = true;
                            nextPath = [...grangrandpaPath];
                            nextPath[nextPath.length - 1] += 1;
                            nextRoot = (0, parser_1._insertNodeAt)(nextRoot, nextPath, (0, parser_1.getTree)(''));
                            nextSelectionStart = 0;
                            nextSelectionEnd = 0;
                        }
                    }
                    else {
                        const node = (0, parser_1._getNodeAtPath)(root, path);
                        if (selectionStart === node.value.length) {
                            preventDefault = true;
                        }
                    }
                }
            }
            else {
                if (value === '') {
                    preventDefault = true;
                    nextPath = [...path, 0];
                    nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(shiftKey ? '{}' : `[]`));
                    nextSelectionStart = 1;
                    nextSelectionEnd = 1;
                }
            }
        }
        else if (!valid) {
            preventDefault = true;
        }
    }
    else if (key === ':') {
        const parent = (0, parser_1._getParent)(root, path);
        const node = (0, parser_1._getNodeAtPath)(root, path);
        if (parent) {
            if (parent.type === parser_1.TreeNodeType.ObjectLiteral) {
                if (node) {
                    if (node.type === parser_1.TreeNodeType.StringLiteral &&
                        selectionStart === selectionEnd &&
                        selectionStart !== 0 &&
                        selectionStart !== value.length) {
                        //
                    }
                    else {
                        const start = `${data.value.substring(0, selectionStart)}`;
                        const nextNode = (0, parser_1.getTree)(`${start}:${data.value.substring(selectionEnd)}`);
                        if ((0, parser_1._isValidObjKeyType)((0, parser_1.getTree)(start, false, true))) {
                            preventDefault = true;
                            nextRoot = (0, parser_1._updateNodeAt)(root, path, nextNode);
                            nextPath = [...path, 1];
                            nextSelectionStart = 0;
                            nextSelectionEnd = 0;
                        }
                    }
                }
                else if (parent.type === parser_1.TreeNodeType.ObjectLiteral ||
                    parent.type === parser_1.TreeNodeType.ArrayLiteral) {
                    const parentPath = (0, parser_1.getParentPath)(path);
                    preventDefault = true;
                    nextPath = [...parentPath, 0, 1];
                    nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(':'));
                    nextSelectionStart = 0;
                    nextSelectionEnd = 0;
                }
            }
            else if (parent.type === parser_1.TreeNodeType.KeyValue) {
                if (lastIndex === 0 &&
                    selectionStart === selectionEnd &&
                    selectionStart === value.length) {
                    const parentPath = (0, parser_1.getParentPath)(path);
                    preventDefault = true;
                    nextPath = [...parentPath, 1];
                    nextSelectionStart = 0;
                    nextSelectionEnd = 0;
                }
            }
        }
    }
    else if (key === '[' || key === '{') {
        if (value === '') {
            const parentPath = (0, parser_1.getParentPath)(path);
            const parent = parentPath && (0, parser_1._getNodeAtPath)(root, parentPath);
            if (!parent ||
                ((parent.type !== parser_1.TreeNodeType.KeyValue ||
                    (parent.type === parser_1.TreeNodeType.KeyValue && lastIndex !== 0)) &&
                    parent.type !== parser_1.TreeNodeType.ObjectLiteral)) {
                preventDefault = true;
                nextPath = [...path, 0];
                nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(key === '{' ? '{}' : `[]`));
                nextSelectionStart = 0;
                nextSelectionEnd = 0;
            }
        }
        else if (selectionStart === 0 && selectionEnd === value.length) {
            preventDefault = true;
            const nextValue = key === `[` ? `[${value}]` : `{${value}}`;
            nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(nextValue));
            const nextParent = (0, parser_1._getNodeAtPath)(nextRoot, path);
            if ((key === '[' && nextParent.type === parser_1.TreeNodeType.ArrayLiteral) ||
                (key === '{' && nextParent.type === parser_1.TreeNodeType.ObjectLiteral)) {
                nextPath = (0, parser_1._getNextNodePath)(nextRoot, path, 1);
                nextSelectionStart = 0;
                nextSelectionEnd = value.length;
                nextDirection = 'forward';
            }
            else {
                nextPath = (0, clone_1.clone)(path);
                nextSelectionStart = 1;
                nextSelectionEnd = value.length + 1;
                nextDirection = 'forward';
            }
        }
    }
    else if (key === `'` || key === `"`) {
        if (value === '') {
            preventDefault = true;
            const nextValue = key === `'` ? `''` : `""`;
            nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(nextValue));
            nextPath = path;
            nextSelectionStart = 1;
            nextSelectionEnd = 1;
        }
        else if (selectionStart === 0 && selectionEnd === value.length) {
            preventDefault = true;
            const nextValue = key === `'` ? `'${value}'` : `"${value}"`;
            nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(nextValue));
            nextSelectionStart = 1;
            nextSelectionEnd = value.length + 1;
            nextDirection = 'forward';
        }
        else {
            const enclosing = key === `'` ? `'` : `"`;
            if (value.endsWith(enclosing) &&
                selectionStart === value.length - 1 &&
                value[selectionStart - 1] !== '\\') {
                preventDefault = true;
                nextSelectionStart = value.length;
                nextSelectionEnd = nextSelectionStart;
            }
            else {
                const textState = (0, write_1.processKeydown)({
                    value,
                    selectionStart,
                    selectionEnd,
                    selectionDirection: 'none',
                }, { ctrlKey: false, altKey: false, shiftKey: false }, key);
                const nextValue = (0, parser_1.getTree)(textState.value);
                nextRoot = (0, parser_1._updateNodeAt)(root, path, nextValue);
                nextSelectionStart = textState.selectionStart;
                nextSelectionEnd = textState.selectionEnd;
                const nextNode = (0, parser_1._getNodeAtPath)(nextRoot, path);
                if (!nextNode) {
                    preventDefault = true;
                    nextPath = (0, parser_1.getParentPath)(path);
                }
            }
        }
    }
    else if (key === `\``) {
        if (!shiftKey) {
            if (value === '') {
                preventDefault = true;
                const nextValue = `\`\``;
                nextRoot = (0, parser_1._updateNodeAt)(root, path, (0, parser_1.getTree)(nextValue));
                nextPath = path;
                nextSelectionStart = 1;
                nextSelectionEnd = nextSelectionStart;
            }
            else {
                const enclosing = `\``;
                if (value.endsWith(enclosing) &&
                    selectionStart === value.length - 1 &&
                    value[selectionStart - 1] !== '\\') {
                    preventDefault = true;
                    nextSelectionStart = value.length;
                    nextSelectionEnd = nextSelectionStart;
                }
            }
        }
    }
    return [
        preventDefault,
        {
            nextRoot,
            nextPath,
            nextSelectionStart,
            nextSelectionEnd,
            nextDirection,
        },
    ];
};
exports._keyUpdateTree = _keyUpdateTree;
const keyUpdateTree = (root, focus, value, key, selectionStart, selectionEnd, shiftKey) => {
    const _root = (0, parser_1.getTree)(root);
    const [preventDefault, { nextRoot: _nextRoot, nextPath, nextSelectionStart, nextSelectionEnd },] = (0, exports._keyUpdateTree)(_root, focus, value, key, selectionStart, selectionEnd, shiftKey);
    return [
        preventDefault,
        {
            nextRoot: _nextRoot.value,
            nextPath,
            nextSelectionStart,
            nextSelectionEnd,
        },
    ];
};
exports.keyUpdateTree = keyUpdateTree;
//# sourceMappingURL=keyUpdateTree.js.map