import { MAX_ARTICLES_DEPTH } from '../../LibrariesSidebar/constants';
import { EMPTY_SIDEBAR_MODEL, } from '../../LibrariesSidebar/types/model';
import { buildDepthMap, flattenTree, sortByPosition, } from '../../LibrariesSidebar/utils/tree';
const toSidebarNode = (article, depth, parentId, childCount) => ({
    id: article.id,
    parentId,
    position: article.position ?? 0,
    title: article.title,
    icon: article.icon,
    status: article.status,
    childCount,
    depth,
    isEditor: article.isEditor,
});
/** Root list: GET /knowledge-libraries → PaginatedResponse items. */
export const adaptFromRootArticles = (articles) => ({
    headerTitle: '',
    nodes: sortByPosition(articles).map(article => toSidebarNode(article, 1, null, article.children?.length ?? 0)),
});
/** Nested tree: GET /knowledge-libraries/:id/children-tree → ArticleTree. */
export const adaptFromChildrenTree = (tree, viewParentId) => {
    if (!tree)
        return EMPTY_SIDEBAR_MODEL;
    const depthMap = buildDepthMap(tree);
    const fallbackParentId = viewParentId ?? tree.id;
    const nodes = sortByPosition(flattenTree(tree)).map(article => toSidebarNode(article, depthMap.get(article.id) ?? 1, article.parentId ?? fallbackParentId, article.children?.length ?? 0));
    return { headerTitle: tree.title, nodes };
};
/** Pick root vs nested model after both queries have been adapted. */
export const resolveSidebarModel = (isRoot, rootArticles, childrenModel) => isRoot
    ? adaptFromRootArticles(rootArticles)
    : (childrenModel ?? EMPTY_SIDEBAR_MODEL);
/** Recompute direct-child counts from the flat node list (e.g. after filtering). */
export const recalculateChildCounts = (model) => {
    const nodeIds = new Set(model.nodes.map(node => node.id));
    const childCounts = new Map();
    for (const node of model.nodes) {
        if (node.parentId != null && nodeIds.has(node.parentId)) {
            childCounts.set(node.parentId, (childCounts.get(node.parentId) ?? 0) + 1);
        }
    }
    return {
        headerTitle: model.headerTitle,
        nodes: model.nodes.map(node => ({
            ...node,
            childCount: childCounts.get(node.id) ?? 0,
        })),
    };
};
/** Filter nodes with a consumer-provided predicate and refresh child counts. */
export const applySidebarNodeFilter = (model, predicate, context) => recalculateChildCounts({
    headerTitle: model.headerTitle,
    nodes: model.nodes.filter(node => predicate(node, context)),
});
export { MAX_ARTICLES_DEPTH };
