import { UMB_DICTIONARY_ENTITY_TYPE, UMB_DICTIONARY_ROOT_ENTITY_TYPE } from '../entity.js';
import { UmbTreeServerDataSourceBase } from '../../core/tree/index.js';
import { DictionaryService } from '../../core/backend-api/index.js';
/**
 * A data source for the Dictionary tree that fetches data from the server
 * @class UmbDictionaryTreeServerDataSource
 * @implements {UmbTreeDataSource}
 */
export class UmbDictionaryTreeServerDataSource extends UmbTreeServerDataSourceBase {
    /**
     * Creates an instance of UmbDictionaryTreeServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbDictionaryTreeServerDataSource
     */
    constructor(host) {
        super(host, {
            getRootItems,
            getChildrenOf,
            getAncestorsOf,
            mapper,
        });
    }
}
const getRootItems = (args) => 
// eslint-disable-next-line local-rules/no-direct-api-import
DictionaryService.getTreeDictionaryRoot({ query: { skip: args.skip, take: args.take } });
const getChildrenOf = (args) => {
    if (args.parent.unique === null) {
        return getRootItems(args);
    }
    else {
        // eslint-disable-next-line local-rules/no-direct-api-import
        return DictionaryService.getTreeDictionaryChildren({
            query: { parentId: args.parent.unique, skip: args.skip, take: args.take },
        });
    }
};
const getAncestorsOf = (args) => 
// eslint-disable-next-line local-rules/no-direct-api-import
DictionaryService.getTreeDictionaryAncestors({
    query: { descendantId: args.treeItem.unique },
});
const mapper = (item) => {
    return {
        unique: item.id,
        parent: {
            unique: item.parent?.id || null,
            entityType: item.parent ? UMB_DICTIONARY_ENTITY_TYPE : UMB_DICTIONARY_ROOT_ENTITY_TYPE,
        },
        name: item.name,
        entityType: UMB_DICTIONARY_ENTITY_TYPE,
        hasChildren: item.hasChildren,
        isFolder: false,
        icon: 'icon-book-alt',
    };
};
