import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { invariant } from "ts-invariant";
import { Button } from "../../../Button/Button.js";
import { useIdPrefixed } from "../../../utils/useId.js";
import { GranteeGroupIcon, GranteeOwnerRemoveIcon, GranteeRemoveIcon, GranteeUserIcon, GranteeUserInactiveIcon, } from "./GranteeIcons.js";
import { GranularGranteeGroupItem } from "./GranularPermissions/GranularGranteeGroupItem.js";
import { GranularGranteeUserItem } from "./GranularPermissions/GranularGranteeUserItem.js";
import { isGranteeGroup, isGranteeGroupAll, isGranteeRules, isGranteeUser, isGranularGranteeGroup, isGranularGranteeUser, } from "./types.js";
import { getGranteeItemTestId, getGranteeLabel } from "./utils.js";
const granteeUserTitleRenderer = (grantee, intl) => {
    const userName = getGranteeLabel(grantee, intl);
    if (grantee.status === "Inactive") {
        const inactiveLabel = ` (${intl.formatMessage({
            id: "shareDialog.share.grantee.item.user.inactive",
        })})`;
        return (_jsxs(_Fragment, { children: [userName, _jsx("span", { className: "gd-grantee-content-label-inactive", children: inactiveLabel })
            ] }));
    }
    return _jsxs(_Fragment, { children: [" ", userName, " "] });
};
function GranteeUserItem({ grantee, mode, onDelete }) {
    const intl = useIntl();
    const onClick = useCallback(() => {
        onDelete(grantee);
    }, [grantee, onDelete]);
    const itemClassName = cx({ "s-share-dialog-owner": grantee.isOwner, "s-share-dialog-current-user": grantee.isCurrentUser }, "gd-share-dialog-grantee-item", getGranteeItemTestId(grantee));
    return (_jsxs("div", { className: itemClassName, children: [grantee.isOwner ? (_jsx(GranteeOwnerRemoveIcon, {})) : (_jsx(GranteeRemoveIcon, { mode: mode, onClick: onClick })), _jsxs("div", { className: "gd-grantee-content", children: [
                    _jsx("div", { className: "gd-grantee-content-label", children: granteeUserTitleRenderer(grantee, intl) }), _jsx("div", { className: "gd-grantee-content-label gd-grantee-content-email", children: grantee.email })
                ] }), grantee.status === "Active" ? _jsx(GranteeUserIcon, {}) : _jsx(GranteeUserInactiveIcon, {})] }));
}
function GranteeUserInactiveItem({ grantee }) {
    const intl = useIntl();
    const granteeLabel = useMemo(() => {
        return getGranteeLabel(grantee, intl);
    }, [grantee, intl]);
    const itemClassName = cx("gd-share-dialog-grantee-item", "s-share-dialog-inactive-owner", getGranteeItemTestId(grantee));
    return (_jsxs("div", { className: itemClassName, children: [
            _jsx(GranteeOwnerRemoveIcon, {}), _jsxs("div", { className: "gd-grantee-content", children: [
                    _jsx("div", { className: "gd-grantee-content-label-inactive", children: granteeLabel }), _jsx("div", { className: "gd-grantee-content-label-inactive gd-grantee-content-inactive", children: _jsx(FormattedMessage, { id: "shareDialog.share.grantee.item.user.inactive.description" }) })
                ] }), _jsx(GranteeUserInactiveIcon, {})
        ] }));
}
function GranteeGroupItem({ grantee, onDelete, mode }) {
    const intl = useIntl();
    const onClick = useCallback(() => {
        onDelete(grantee);
    }, [grantee, onDelete]);
    const groupName = useMemo(() => getGranteeLabel(grantee, intl), [grantee, intl]);
    const numOfUsers = useMemo(() => {
        if (grantee.memberCount) {
            return intl.formatMessage({
                id: "shareDialog.share.grantee.item.users.count",
            }, { granteeCount: grantee.memberCount });
        }
        return undefined;
    }, [grantee, intl]);
    const itemClassName = cx("gd-share-dialog-grantee-item", getGranteeItemTestId(grantee));
    return (_jsxs("div", { className: itemClassName, children: [
            _jsx(GranteeRemoveIcon, { mode: mode, onClick: onClick }), _jsxs("div", { className: "gd-grantee-content", children: [
                    _jsx("div", { className: "gd-grantee-content-label", children: groupName }), numOfUsers ? (_jsx("div", { className: "gd-grantee-count-button", children: _jsx(Button, { className: "gd-button-link-dimmed gd-button gd-grantee-content-user-count s-grantee-content-user-count", value: numOfUsers }) })) : null] }), _jsx(GranteeGroupIcon, {})
        ] }));
}
/**
 * @internal
 */
export function GranteeItemComponent({ grantee, mode, currentUserPermissions, isSharedObjectLocked, isGranteeShareLoading, onDelete, onChange, }) {
    const granularGranteeItemId = useIdPrefixed("grantee-item");
    if (isGranularGranteeUser(grantee)) {
        return (_jsx(GranularGranteeUserItem, { currentUserPermissions: currentUserPermissions, isSharedObjectLocked: isSharedObjectLocked, grantee: grantee, onChange: onChange, onDelete: onDelete, mode: mode, id: granularGranteeItemId, isGranteeShareLoading: isGranteeShareLoading }));
    }
    else if (isGranularGranteeGroup(grantee) || isGranteeRules(grantee)) {
        return (_jsx(GranularGranteeGroupItem, { currentUserPermissions: currentUserPermissions, isSharedObjectLocked: isSharedObjectLocked, grantee: grantee, onChange: onChange, onDelete: onDelete, mode: mode, id: granularGranteeItemId, isGranteeShareLoading: isGranteeShareLoading }));
    }
    else if (isGranteeUser(grantee)) {
        return _jsx(GranteeUserItem, { grantee: grantee, mode: mode, onDelete: onDelete });
    }
    else if (grantee.type === "inactive_owner") {
        return _jsx(GranteeUserInactiveItem, { grantee: grantee });
    }
    else if (isGranteeGroup(grantee) || isGranteeGroupAll(grantee)) {
        return _jsx(GranteeGroupItem, { grantee: grantee, mode: mode, onDelete: onDelete });
    }
    else {
        invariant(grantee, "Illegal grantee used.");
    }
    return null;
}
