/**
 * Normalize navigation tree from API for editing (ensure children arrays).
 * @param {unknown[]} raw
 * @returns {object[]}
 */
export function normalizeTreeFromApi(raw) {
  if (!Array.isArray(raw)) return [];

  function walk(node) {
    if (!node || typeof node !== "object") return node;
    const n = { ...node };
    n.children = Array.isArray(n.children) ? n.children.map(walk) : [];
    return n;
  }

  return raw.map(walk);
}

/**
 * Clone tree for PUT /navigation-tree (strip Vue proxies).
 * @param {unknown[]} roots
 * @returns {object[]}
 */
export function cloneDeepForSave(roots) {
  return JSON.parse(JSON.stringify(roots));
}
