// (C) 2023-2025 GoodData Corporation
import { messages } from "../../locales.js";
import { extractUserGroupName, sortByName } from "../../utils.js";
const createErrorOption = (intl) => {
    return [
        {
            isDisabled: true,
            type: "error",
            label: intl.formatMessage(messages.searchUserGroupError),
        },
    ];
};
export const loadUserGroupOptionsPromise = (backend, organizationId, intl) => async (inputValue) => {
    try {
        return await backend
            .organization(organizationId)
            .users()
            .getUserGroups()
            .then((userGroups) => userGroups.filter((userGroup) => inputValue === "" ||
            extractUserGroupName(userGroup)?.toLowerCase().includes(inputValue.toLowerCase())))
            .then((userGroups) => userGroups
            .map((userGroup) => ({
            id: userGroup.id,
            title: extractUserGroupName(userGroup) ?? "",
        }))
            .sort(sortByName))
            .then((matchingUserGroups) => matchingUserGroups.map((userGroup) => {
            return {
                label: userGroup.title,
                value: userGroup,
            };
        }));
    }
    catch {
        return createErrorOption(intl);
    }
};
//# sourceMappingURL=loadUserGroupOptionsPromise.js.map