import { UMB_USER_GROUP_ENTITY_TYPE } from '../../entity.js';
import { UserGroupService } from '../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
/**
 * A data source for the UserGroup that fetches data from the server
 * @class UmbUserGroupCollectionServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbUserGroupCollectionServerDataSource {
    #host;
    /**
     * Creates an instance of UmbUserGroupCollectionServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbUserGroupCollectionServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    async getCollection(filter) {
        const filterText = filter.filter ?? filter.query ?? undefined;
        const { data, error } = await tryExecute(this.#host, UserGroupService.getFilterUserGroup({ query: { skip: filter.skip, take: filter.take, filter: filterText } }));
        if (data) {
            const mappedItems = data.items.map((item) => {
                const userGroup = {
                    alias: item.alias,
                    aliasCanBeChanged: item.aliasCanBeChanged,
                    documentRootAccess: item.documentRootAccess,
                    documentStartNode: item.documentStartNode ? { unique: item.documentStartNode.id } : null,
                    entityType: UMB_USER_GROUP_ENTITY_TYPE,
                    fallbackPermissions: item.fallbackPermissions,
                    hasAccessToAllLanguages: item.hasAccessToAllLanguages,
                    icon: item.icon || null,
                    isDeletable: item.isDeletable,
                    languages: item.languages,
                    mediaRootAccess: item.mediaRootAccess,
                    mediaStartNode: item.mediaStartNode ? { unique: item.mediaStartNode.id } : null,
                    name: item.name,
                    description: item.description || null,
                    permissions: item.permissions,
                    sections: item.sections,
                    unique: item.id,
                };
                return userGroup;
            });
            return { data: { items: mappedItems, total: data.total } };
        }
        return { error };
    }
}
