import { RootConfig, AXMMetaDataDefinitionService } from '@acorex/modules/data-management';
import { AXMEntityCrudServiceImpl } from '@acorex/platform/layout/entity';
import * as i0 from '@angular/core';
import { inject, Injector, runInInjectionContext, Injectable } from '@angular/core';

class AXCQueryCategoryWithItemsQuery {
    constructor() {
        this.injector = inject(Injector);
        this.categoryEntityService = runInInjectionContext(this.injector, () => new AXMEntityCrudServiceImpl(`${RootConfig.module.name}.${RootConfig.entities.metaDataDefinition.name}Category`));
        this.metaDataService = inject(AXMMetaDataDefinitionService);
    }
    fetch() {
        return this.getAllWithItems();
    }
    async getAllWithItems() {
        const rootCategories = await this.categoryEntityService.query({
            take: 1000,
            skip: 0,
            filter: {
                logic: 'and',
                filters: [
                    {
                        field: 'parentId',
                        operator: {
                            type: 'isNull',
                        },
                    },
                ],
            },
        });
        // Build the tree and prune empty categories (no children and no items)
        const builtRoots = await Promise.all(rootCategories.items.map((category) => this.buildCategoryTree(category)));
        const categories = builtRoots.filter((c) => Boolean(c));
        // Aggregate all items across remaining categories
        const allItems = [];
        const collectItems = (nodes) => {
            for (const node of nodes) {
                if (node.items?.length) {
                    allItems.push(...node.items);
                }
                if (node.children?.length) {
                    collectItems(node.children);
                }
            }
        };
        collectItems(categories);
        // Add synthetic 'All' node at the beginning
        const allNode = {
            id: 'all',
            title: 'All',
            childrenCount: 0,
            items: allItems,
            children: [],
            categories: []
        };
        return [allNode, ...categories];
    }
    async buildCategoryTree(category) {
        const itemsResult = await this.metaDataService.query({
            filter: {
                logic: 'and',
                filters: [
                    {
                        field: 'categoryIds',
                        operator: {
                            type: 'contains',
                        },
                        value: category.id,
                    },
                ],
            },
            take: 1000,
            skip: 0,
        });
        const childCategoriesResult = await this.categoryEntityService.query({
            filter: {
                logic: 'and',
                filters: [
                    {
                        field: 'parentId',
                        operator: {
                            type: 'equal',
                        },
                        value: category.id,
                    },
                ],
            },
            take: 1000,
            skip: 0,
        });
        const builtChildren = await Promise.all(childCategoriesResult.items.map((child) => this.buildCategoryTree(child)));
        const children = builtChildren.filter((c) => Boolean(c));
        const node = {
            ...category,
            items: itemsResult.items,
            categories: children,
            children: children,
        };
        // Prune node if it has no items and no children
        if ((!node.items || node.items.length === 0) && (!node.children || node.children.length === 0)) {
            return null;
        }
        return node;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCQueryCategoryWithItemsQuery, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCQueryCategoryWithItemsQuery, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCQueryCategoryWithItemsQuery, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }] });

export { AXCQueryCategoryWithItemsQuery };
//# sourceMappingURL=acorex-connectivity-mock-category-with-items.query-DWW7t6nc.mjs.map
