import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2023-2025 GoodData Corporation
import { useCallback, useState } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { withBubble } from "@gooddata/sdk-ui-kit";
import { stringUtils } from "@gooddata/util";
import { PermissionsDropdownList } from "./HierarchicalPermissionsDropdownList.js";
import { hierarchicalPermissionMessages } from "./locales.js";
import { useTelemetry } from "../../TelemetryContext.js";
const trackPermissionChange = (trackEvent, subjectType, isHierarchical) => {
    if (subjectType === "user") {
        trackEvent(isHierarchical
            ? "user-permission-changed-to-hierarchy"
            : "user-permission-changed-to-single-workspace");
    }
    else {
        trackEvent(isHierarchical
            ? "group-permission-changed-to-hierarchy"
            : "group-permission-changed-to-single-workspace");
    }
};
function Dropdown({ workspace, subjectType, isDropdownDisabled, isDropdownOpen, toggleDropdown, onChange, className, }) {
    const intl = useIntl();
    const trackEvent = useTelemetry();
    const [selectedPermission, setSelectedPermission] = useState(workspace.isHierarchical);
    const handleOnSelect = (isHierarchical) => {
        onChange({ ...workspace, isHierarchical });
        trackPermissionChange(trackEvent, subjectType, isHierarchical);
        setSelectedPermission(isHierarchical);
    };
    const handleClick = useCallback(() => {
        if (!isDropdownDisabled) {
            toggleDropdown();
        }
    }, [isDropdownDisabled, toggleDropdown]);
    const buttonValue = intl.formatMessage(selectedPermission ? hierarchicalPermissionMessages.enabled : hierarchicalPermissionMessages.disabled);
    return (_jsxs("div", { className: className, children: [_jsx("div", { className: cx("s-user-management-hierarchical-permission-button", "gd-granular-permission-button", "dropdown-button", `gd-granular-hierarchical-permission-button-${stringUtils.simplifyText(workspace.id)}`, {
                    "is-active": isDropdownOpen,
                    "gd-icon-navigateup": !isDropdownDisabled && isDropdownOpen,
                    "gd-icon-navigatedown": !isDropdownDisabled && !isDropdownOpen,
                    disabled: isDropdownDisabled,
                    "gd-icon-right": !isDropdownDisabled,
                }), onClick: handleClick, "aria-label": "Share dialog granular permissions button", children: _jsx("div", { className: "s-user-management-button-title gd-granular-permission-button-title", children: buttonValue }) }), _jsx(PermissionsDropdownList, { selectedPermission: selectedPermission, onSelect: handleOnSelect, toggleDropdown: toggleDropdown, isShowDropdown: isDropdownOpen, alignTo: `.gd-granular-hierarchical-permission-button-${stringUtils.simplifyText(workspace.id)}` })] }));
}
export const HierarchicalPermissionsDropdown = withBubble(Dropdown);
//# sourceMappingURL=HierarchicalPermissionsDropdown.js.map