import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { useShareDialogInteraction } from "../ComponentInteractionContext.js";
import { GranteeUserIcon } from "../GranteeIcons.js";
import { getGranteeItemTestId, getGranteeLabel } from "../utils.js";
import { GranularPermissionsDropdownWithBubble } from "./GranularPermissionsDropdown.js";
import { getGranteePossibilities } from "./permissionsLogic.js";
import { usePermissionsDropdownState } from "./usePermissionsDropdownState.js";
export function GranularGranteeUserItem({ grantee, currentUserPermissions, isSharedObjectLocked, onChange, onDelete, mode, id, isGranteeShareLoading, }) {
    const { email } = grantee;
    const { isDropdownOpen, toggleDropdown } = usePermissionsDropdownState();
    const { permissionsDropdownOpenInteraction } = useShareDialogInteraction();
    const intl = useIntl();
    const itemClassName = cx({ "s-share-dialog-current-user": grantee.isCurrentUser }, "s-share-dialog-grantee-item", "gd-share-dialog-grantee-item", getGranteeItemTestId(grantee), { "is-active": isDropdownOpen });
    const label = useMemo(() => {
        return getGranteeLabel(grantee, intl);
    }, [grantee, intl]);
    const renderSubtitle = useMemo(() => email && email !== label, [email, label]);
    const granteePossibilities = useMemo(() => getGranteePossibilities(grantee, currentUserPermissions, isSharedObjectLocked), [grantee, currentUserPermissions, isSharedObjectLocked]);
    const handleToggleDropdown = useCallback(() => {
        toggleDropdown();
        if (!isDropdownOpen) {
            permissionsDropdownOpenInteraction(grantee, mode === "ShareGrantee", granteePossibilities.assign.effective);
        }
    }, [
        toggleDropdown,
        isDropdownOpen,
        grantee,
        mode,
        permissionsDropdownOpenInteraction,
        granteePossibilities,
    ]);
    return (_jsxs("div", { className: itemClassName, children: [
            _jsx(GranularPermissionsDropdownWithBubble, { grantee: grantee, granteePossibilities: granteePossibilities, handleToggleDropdown: handleToggleDropdown, onChange: onChange ?? (() => { }), onDelete: onDelete, isDropdownDisabled: !granteePossibilities.change.enabled || isGranteeShareLoading, bubbleTextId: granteePossibilities.change.tooltip, className: "gd-grantee-granular-permission", triggerClassName: "gd-grantee-granular-permission-bubble-trigger", mode: mode, accessibilityConfig: {
                    ariaDescribedBy: id,
                } }), _jsxs("div", { id: id, className: "gd-grantee-content", children: [
                    _jsx("div", { className: "gd-grantee-content-label", children: label }), renderSubtitle ? (_jsx("div", { className: "gd-grantee-content-label gd-grantee-content-email", children: email })) : null] }), _jsx(GranteeUserIcon, {})
        ] }));
}
