===== ISTEM =====
Solve the following programming task.

TASK: Tree Level Order Traversal (BFS)

Write a function 'levelOrder(root)' that traverses the given node tree (node: { val, children: [] }) breadth-first and returns an array containing the values of the elements at each level.

STARTER CODE (javascript):
function levelOrder(root) {
  // Write your code here
}

module.exports = { levelOrder };


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```javascript
function levelOrder(root) {
  if (!root) return [];

  const result = [];
  const queue = [root];
  let levelSize = queue.length;
  let currentLevel = [];

  while (queue.length > 0 && levelSize > 0) {
    const node = queue.shift();
    currentLevel.push(node.val);
    levelSize--;

    if (node.children) {
      for (const child of node.children) {
        queue.push(child);
      }
    }

    if (levelSize === 0) {
      result.push(currentLevel);
      currentLevel = [];
      levelSize = queue.length;
    }
  }

  return result;
}

module.exports = { levelOrder };
```