import { UMB_PARTIAL_VIEW_ENTITY_TYPE, UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE, UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE, } from '../entity.js';
import { UmbServerFilePathUniqueSerializer } from '../../../core/server-file-system/index.js';
import { UmbTreeServerDataSourceBase } from '../../../core/tree/index.js';
import { PartialViewService } from '../../../core/backend-api/index.js';
/**
 * A data source for the PartialView tree that fetches data from the server
 * @class UmbPartialViewTreeServerDataSource
 * @implements {UmbTreeDataSource}
 */
export class UmbPartialViewTreeServerDataSource extends UmbTreeServerDataSourceBase {
    /**
     * Creates an instance of UmbPartialViewTreeServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbPartialViewTreeServerDataSource
     */
    constructor(host) {
        super(host, {
            getRootItems,
            getChildrenOf,
            getAncestorsOf,
            mapper,
        });
    }
}
const getRootItems = (args) => 
// eslint-disable-next-line local-rules/no-direct-api-import
PartialViewService.getTreePartialViewRoot({ query: { skip: args.skip, take: args.take } });
const getChildrenOf = (args) => {
    const parentPath = new UmbServerFilePathUniqueSerializer().toServerPath(args.parent.unique);
    if (parentPath === null) {
        return getRootItems(args);
    }
    else {
        // eslint-disable-next-line local-rules/no-direct-api-import
        return PartialViewService.getTreePartialViewChildren({
            query: { parentPath, skip: args.skip, take: args.take },
        });
    }
};
const getAncestorsOf = (args) => {
    const descendantPath = new UmbServerFilePathUniqueSerializer().toServerPath(args.treeItem.unique);
    if (!descendantPath)
        throw new Error('Descendant path is not available');
    // eslint-disable-next-line local-rules/no-direct-api-import
    return PartialViewService.getTreePartialViewAncestors({
        query: { descendantPath },
    });
};
const mapper = (item) => {
    const serializer = new UmbServerFilePathUniqueSerializer();
    return {
        unique: serializer.toUnique(item.path),
        parent: {
            unique: item.parent ? serializer.toUnique(item.parent.path) : null,
            entityType: item.parent ? UMB_PARTIAL_VIEW_ENTITY_TYPE : UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE,
        },
        entityType: item.isFolder ? UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE : UMB_PARTIAL_VIEW_ENTITY_TYPE,
        name: item.name,
        isFolder: item.isFolder,
        hasChildren: item.hasChildren,
        icon: item.isFolder ? undefined : 'icon-document-html',
    };
};
