import { move } from "../../services/action.js";
export function handleStructureHoverChange(previousHoveredNode, hoveredNode) {
    if (previousHoveredNode) {
        const leaf = previousHoveredNode.querySelector(".structure-tree__node-leaf");
        if (leaf) {
            resetLeafClasses(leaf);
        }
    }
    return hoveredNode.getBoundingClientRect();
}
export async function handleStructureDropChange({ position, hoveredNode, draggedNode, }) {
    const draggedParent = draggedNode.closest("eden-structure-tree");
    if (position === "TOP") {
        await dropSibling(hoveredNode, draggedNode, 0);
    }
    if (position === "BOT") {
        await dropSibling(hoveredNode, draggedNode, 1);
    }
    if (position === "IN") {
        await dropIn(hoveredNode, draggedNode);
    }
    if (draggedParent?.querySelectorAll("eden-structure-tree-node").length === 0) {
        draggedParent.parentElement?.classList.remove("structure-tree__node--has-subs");
        draggedParent.remove();
    }
}
export function cleanupStructureDragHighlights(root) {
    const leafs = Array.from(root.querySelectorAll(".structure-tree__node-leaf"));
    leafs.forEach((leaf) => {
        resetLeafClasses(leaf);
    });
}
async function dropIn(hoveredNode, draggedNode) {
    const navTree = hoveredNode.querySelector("eden-structure-tree");
    if (navTree && hoveredNode.nodeId && draggedNode.nodeId) {
        await move(hoveredNode.nodeId, draggedNode.nodeId, navTree.children.length);
        navTree.insertAdjacentElement("afterend", draggedNode);
    }
    else {
        if (hoveredNode.nodeId && draggedNode.nodeId) {
            await move(hoveredNode.nodeId, draggedNode.nodeId, 0);
        }
        const { default: EdenStructureTree } = await import("./eden-structure-tree.js");
        const newNavTree = new EdenStructureTree();
        const node = hoveredNode.querySelector(".structure-tree__node");
        if (!node)
            return;
        node.classList.add("structure-tree__node--has-subs");
        newNavTree.insertAdjacentElement("afterbegin", draggedNode);
        node.appendChild(newNavTree);
    }
}
async function dropSibling(hoveredNode, draggedNode, posStep) {
    const parentNode = getClosestParentNode(hoveredNode);
    if (!parentNode)
        return;
    const parentTree = parentNode.querySelector("eden-structure-tree");
    const siblingNode = Array.from(parentTree.children);
    const position = siblingNode.indexOf(hoveredNode);
    if (parentNode.nodeId && draggedNode.nodeId) {
        await move(parentNode.nodeId, draggedNode.nodeId, position + posStep);
    }
    if (posStep === 0) {
        hoveredNode.insertAdjacentElement("beforebegin", draggedNode);
    }
    else {
        hoveredNode.insertAdjacentElement("afterend", draggedNode);
    }
}
function getClosestParentNode(node) {
    return node.parentElement?.closest("eden-structure-tree-node");
}
function resetLeafClasses(leaf) {
    leaf.classList.remove("structure-tree__node-leaf--top", "structure-tree__node-leaf--in", "structure-tree__node-leaf--bottom");
}
