import { UMB_USER_ENTITY_TYPE } from '../../entity.js';
import { UserService } from '../../../../core/backend-api/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
/**
 * A data source that fetches the user collection data from the server.
 * @class UmbUserCollectionServerDataSource
 * @implements {UmbCollectionDataSource}
 */
export class UmbUserCollectionServerDataSource {
    #host;
    /**
     * Creates an instance of UmbUserCollectionServerDataSource.
     * @param {UmbControllerHost} host - The controller host for this controller to be appended to
     * @memberof UmbUserCollectionServerDataSource
     */
    constructor(host) {
        this.#host = host;
    }
    /**
     * Gets the user collection filtered by the given filter.
     * @param {UmbUserCollectionFilterModel} filter
     * @returns {*}
     * @memberof UmbUserCollectionServerDataSource
     */
    async getCollection(filter) {
        const { data, error } = await tryExecute(this.#host, UserService.getFilterUser({
            query: {
                filter: filter.filter,
                orderBy: filter.orderBy, // TODO: This is a temporary workaround to avoid a type error.
                orderDirection: filter.orderDirection, // TODO: This is a temporary workaround to avoid a type error.
                skip: filter.skip,
                take: filter.take,
                userGroupIds: filter.userGroupIds,
                userStates: filter.userStates, // TODO: This is a temporary workaround to avoid a type error.
            },
        }));
        if (data) {
            const { items, total } = data;
            const mappedItems = items.map((item) => {
                const userDetail = {
                    entityType: UMB_USER_ENTITY_TYPE,
                    email: item.email,
                    userName: item.userName,
                    name: item.name,
                    userGroupUniques: item.userGroupIds.map((reference) => {
                        return {
                            unique: reference.id,
                        };
                    }),
                    unique: item.id,
                    languageIsoCode: item.languageIsoCode || null,
                    documentStartNodeUniques: item.documentStartNodeIds.map((node) => {
                        return {
                            unique: node.id,
                        };
                    }),
                    mediaStartNodeUniques: item.mediaStartNodeIds.map((node) => {
                        return {
                            unique: node.id,
                        };
                    }),
                    hasDocumentRootAccess: item.hasDocumentRootAccess,
                    hasMediaRootAccess: item.hasMediaRootAccess,
                    avatarUrls: item.avatarUrls,
                    state: item.state,
                    failedLoginAttempts: item.failedLoginAttempts,
                    createDate: item.createDate,
                    updateDate: item.updateDate,
                    lastLoginDate: item.lastLoginDate || null,
                    lastLockoutDate: item.lastLockoutDate || null,
                    lastPasswordChangeDate: item.lastPasswordChangeDate || null,
                    isAdmin: item.isAdmin,
                    kind: item.kind,
                };
                return userDetail;
            });
            return { data: { items: mappedItems, total } };
        }
        return { error };
    }
}
