export function flattenProjectsHierarchy(projects) {
    const byParent = new Map();
    const byId = new Set(projects.map((project) => project.projectId));
    for (const project of projects) {
        const parentId = project.parentProjectId && byId.has(project.parentProjectId)
            ? project.parentProjectId
            : null;
        const siblings = byParent.get(parentId) ?? [];
        siblings.push(project);
        byParent.set(parentId, siblings);
    }
    const result = [];
    const visited = new Set();
    const visit = (parentId, depth) => {
        const children = byParent.get(parentId) ?? [];
        for (const child of children) {
            if (visited.has(child.projectId))
                continue;
            visited.add(child.projectId);
            result.push({ ...child, depth });
            visit(child.projectId, depth + 1);
        }
    };
    visit(null, 0);
    // Recover orphaned/cyclic nodes defensively so they remain selectable.
    for (const project of projects) {
        if (visited.has(project.projectId))
            continue;
        result.push({ ...project, depth: 0 });
    }
    return result;
}
//# sourceMappingURL=projectHierarchy.js.map