import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useIntl } from "react-intl";
import { olpPermissionMessages } from "../../locales.js";
import { bem } from "../@utils/bem.js";
import { UiButton } from "../UiButton/UiButton.js";
import { UiIcon } from "../UiIcon/UiIcon.js";
import { UiMoreOptionsMenu } from "../UiMoreOptionsMenu/UiMoreOptionsMenu.js";
import { UiPermissionMenu } from "../UiPermissionMenu/UiPermissionMenu.js";
import { UiTooltip } from "../UiTooltip/UiTooltip.js";
const { b, e } = bem("gd-ui-kit-grantee-row-controls");
/**
 * Per-row controls in the OLP share dialog: a permission trigger
 * ({@link UiPermissionMenu}) plus a "⋯" menu ({@link UiMoreOptionsMenu}) for
 * labels access and Transfer ownership, with an optional inherited-permission
 * warning badge.
 *
 * @internal
 */
export function UiGranteeRowControls({ labels, selectedLabelIds, permissionLevel, effectivePermission, onLabelsChange, onPermissionChange, onTransferOwnership, onRemoveAccess, isDisabled, dataTestId, }) {
    const intl = useIntl();
    const hasLabels = labels.length > 0;
    // EDIT is display-only: the dialog can't assign or change it, and offering the
    // VIEW/SHARE menu on an EDIT row would silently downgrade the grant on pick.
    // So an EDIT row shows a static, non-interactive "Can edit" label instead of
    // the permission dropdown.
    const isReadOnlyLevel = permissionLevel === "EDIT";
    const permissionTriggerText = intl.formatMessage(isReadOnlyLevel
        ? olpPermissionMessages.canEdit
        : permissionLevel === "SHARE"
            ? olpPermissionMessages.canViewAndShare
            : olpPermissionMessages.canView);
    // A read-only level has no permission dropdown to host Remove access, so that
    // action moves into the ⋯ menu for those rows (dropdown rows keep it in the menu).
    const removeInMoreOptions = isReadOnlyLevel ? onRemoveAccess : undefined;
    // Nothing to put in the ⋯ menu → drop it.
    const hasMoreOptions = hasLabels || !!onTransferOwnership || !!removeInMoreOptions;
    // Guard on permissionLevel too, so the badge can't show when the assigned
    // level already matches the inherited one.
    const isInheritedHigherPermission = permissionLevel === "VIEW" && effectivePermission === "SHARE";
    const effectiveTooltip = isInheritedHigherPermission
        ? intl.formatMessage(olpPermissionMessages.effectivePermissionTooltipShare)
        : undefined;
    return (_jsxs("div", { className: b(), "data-testid": dataTestId, children: [effectiveTooltip ? (_jsx(UiTooltip, { triggerBy: ["hover", "focus"], content: effectiveTooltip, anchor: _jsx("span", { className: e("effective-warning"), role: "img", "aria-label": intl.formatMessage(olpPermissionMessages.effectivePermissionAriaLabel), tabIndex: 0, children: _jsx(UiIcon, { type: "warning", size: 16, color: "warning" }) }) })) : null, permissionLevel === "EDIT" ? (_jsx("span", { className: e("readonly-permission"), children: permissionTriggerText })) : (_jsx(UiPermissionMenu, { anchor: _jsx(UiButton, { label: permissionTriggerText, size: "small", variant: "dropdownInline", iconAfter: "navigateDown", isDisabled: isDisabled }), selectedLevel: permissionLevel, onPermissionChange: onPermissionChange, onRemoveAccess: onRemoveAccess })), hasMoreOptions ? (_jsx(UiMoreOptionsMenu, { labels: hasLabels ? labels : undefined, selectedLabelIds: selectedLabelIds, onLabelsChange: onLabelsChange, onTransferOwnership: onTransferOwnership, onRemoveAccess: removeInMoreOptions, isDisabled: isDisabled })) : null] }));
}
