import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2025 GoodData Corporation
import { useMemo, useRef } from "react";
import { useIntl } from "react-intl";
import { serializeObjRef } from "@gooddata/sdk-model";
import { GranteeItemComponent } from "./GranteeItem.js";
import { GranteeListEmpty } from "./GranteeListEmpty.js";
import { ADD_GRANTEE_ID, ADD_GRANTEE_SELECT_ID, sortGranteeList } from "./utils.js";
/**
 * @internal
 */
export function GranteeList({ grantees, mode, areGranularPermissionsSupported, currentUserPermissions, isSharedObjectLocked, isGranteeShareLoading, onDelete, onChange, }) {
    const intl = useIntl();
    const listRef = useRef(null);
    const sortedGrantees = useMemo(() => {
        return sortGranteeList(grantees, intl);
    }, [grantees, intl]);
    const handleOnDelete = (grantee) => {
        onDelete(grantee);
        // Programatically return focus to the list after item is removed
        const listElement = listRef.current;
        if (grantees.length > 1 && listElement) {
            // Set tabindex to make it focusable and add it to tab order temporarily
            listElement.tabIndex = -1;
            listElement.focus();
        }
        else {
            const elementId = mode === "ShareGrantee" ? ADD_GRANTEE_ID : ADD_GRANTEE_SELECT_ID;
            const element = document.getElementById(elementId);
            element?.focus();
        }
    };
    if (grantees.length === 0) {
        return _jsx(GranteeListEmpty, {});
    }
    return (_jsx("div", { onBlur: (e) => e.currentTarget.removeAttribute("tabindex"), ref: listRef, className: "gd-share-dialog-grantee-list", children: sortedGrantees.map((grantee) => {
            return (_jsx(GranteeItemComponent, { currentUserPermissions: currentUserPermissions, isSharedObjectLocked: isSharedObjectLocked, grantee: grantee, mode: mode, areGranularPermissionsSupported: areGranularPermissionsSupported, onDelete: handleOnDelete, onChange: onChange, isGranteeShareLoading: isGranteeShareLoading }, serializeObjRef(grantee.id)));
        }) }));
}
