// (C) 2023-2025 GoodData Corporation
import { messages } from "../../locales.js";
import { extractUserName } from "../../utils.js";
const createErrorOption = (intl) => {
    return [
        {
            isDisabled: true,
            type: "error",
            label: intl.formatMessage(messages.searchUserError),
        },
    ];
};
export const loadUsersOptionsPromise = (backend, organizationId, intl) => async (search, _options, { page }) => {
    try {
        return await backend
            .organization(organizationId)
            .users()
            .getUsersQuery()
            .withFilter({
            name: search,
        })
            .withPage(page)
            .query()
            .then((result) => {
            return {
                options: result.items.map((item) => {
                    const userMember = {
                        id: item.id,
                        title: extractUserName(item) ?? "",
                        email: item.email ?? "",
                    };
                    return {
                        label: item.fullName || item.id,
                        value: userMember,
                    };
                }),
                hasMore: result.items.length > 0,
                additional: {
                    page: page + 1,
                },
            };
        });
    }
    catch {
        return {
            options: createErrorOption(intl),
            hasMore: false,
            additional: {
                page: 0,
            },
        };
    }
};
//# sourceMappingURL=loadUsersOptionsPromise.js.map