import { UMB_MEMBER_ENTITY_TYPE } from '../entity.js';
import { MemberService } from '../../../core/backend-api/index.js';
import { tryExecute } from '../../../core/resources/index.js';
/**
 * A data source for the Rollback that fetches data from the server
 * @class UmbMemberSearchServerDataSource
 * @implements {RepositoryDetailDataSource}
 */
export class UmbMemberSearchServerDataSource {
    #host;
    /**
     * Creates an instance of UmbMemberSearchServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbMemberSearchServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Get a list of versions for a data
     * @param args
     * @returns {*}
     * @memberof UmbMemberSearchServerDataSource
     */
    async search(args) {
        const { data, error } = await tryExecute(this.#host, MemberService.getItemMemberSearch({
            query: {
                query: args.query,
                allowedMemberTypes: args.allowedContentTypes?.map((memberReference) => memberReference.unique),
                skip: args.paging?.skip,
                take: args.paging?.take,
            },
        }));
        if (data) {
            const mappedItems = data.items.map((item) => {
                return {
                    href: 'section/member-management/workspace/member/edit/' + item.id,
                    entityType: UMB_MEMBER_ENTITY_TYPE,
                    unique: item.id,
                    name: item.variants[0].name || '',
                    kind: item.kind,
                    memberType: {
                        unique: item.memberType.id,
                        icon: item.memberType.icon,
                        collection: item.memberType.collection ? { unique: item.memberType.collection.id } : null,
                    },
                    variants: item.variants.map((variant) => {
                        return {
                            name: variant.name,
                            culture: variant.culture || null,
                        };
                    }),
                };
            });
            return { data: { items: mappedItems, total: data.total } };
        }
        return { error };
    }
}
