import { UMB_MEMBER_GROUP_ENTITY_TYPE } from '../../entity.js';
import { tryExecute } from '../../../../core/resources/index.js';
import { MemberGroupService } from '../../../../core/backend-api/index.js';
/**
 * A data source that fetches the member collection data from the server.
 * @class UmbMemberGroupCollectionServerDataSource
 * @implements {UmbCollectionDataSource}
 */
export class UmbMemberGroupCollectionServerDataSource {
    #host;
    /**
     * Creates an instance of UmbMemberGroupCollectionServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbMemberGroupCollectionServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Gets the member collection filtered by the given filter.
     * @param {UmbMemberGroupCollectionFilterModel} filter
     * @returns {*}
     * @memberof UmbMemberGroupCollectionServerDataSource
     */
    async getCollection(query) {
        const { data, error } = await tryExecute(this.#host, MemberGroupService.getMemberGroup({ query }));
        if (error) {
            return { error };
        }
        if (!data) {
            return { data: { items: [], total: 0 } };
        }
        const { items, total } = data;
        const mappedItems = items.map((item) => {
            const memberDetail = {
                entityType: UMB_MEMBER_GROUP_ENTITY_TYPE,
                unique: item.id,
                name: item.name,
            };
            return memberDetail;
        });
        return { data: { items: mappedItems, total } };
    }
}
