/**
 * Sort a list by its `position` field ascending (nulls treated as 0).
 * Returns a new array; the input is not mutated.
 */
export const sortByPosition = (items) => [...items].sort((a, b) => (a.position ?? 0) - (b.position ?? 0));
/**
 * Depth-first flatten of a tree's descendants (root excluded), preserving each
 * level's order. Works on any node exposing a nullable `children` array.
 */
export const flattenTree = (root) => {
    if (!root?.children?.length)
        return [];
    const result = [];
    const stack = [...root.children].reverse();
    while (stack.length) {
        const node = stack.pop();
        if (!node)
            break;
        result.push(node);
        if (node.children)
            stack.push(...[...node.children].reverse());
    }
    return result;
};
/**
 * Map every node id in a tree to its 1-based depth (root = 1). Works on any
 * node exposing an `id` and a nullable `children` array.
 */
export const buildDepthMap = (tree, depth = 1, map = new Map()) => {
    if (!tree)
        return map;
    map.set(tree.id, depth);
    tree.children?.forEach(child => {
        buildDepthMap(child, depth + 1, map);
    });
    return map;
};
