import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2021-2025 GoodData Corporation
import { useCallback } from "react";
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { AddGranteeSelect } from "./AddGranteeSelect.js";
import { GranteeList } from "./GranteeList.js";
import { isGranteeGroup, isGranteeRules, isGranteeUser, } from "./types.js";
/**
 * In case of user and group, we need to make sure, that the added grantee has some default granular permission.
 */
const enrichGranteeWithDefaultPermission = (grantee) => {
    const defaultPermissions = {
        permissions: ["VIEW"],
        inheritedPermissions: [],
    };
    if (isGranteeUser(grantee)) {
        return {
            ...grantee,
            ...defaultPermissions,
            type: "granularUser",
        };
    }
    else if (isGranteeRules(grantee)) {
        return {
            ...grantee,
            ...defaultPermissions,
            type: "allWorkspaceUsers",
        };
    }
    else {
        return {
            ...grantee,
            ...defaultPermissions,
            type: "granularGroup",
        };
    }
};
/**
 * @internal
 */
export function AddGranteeContent({ appliedGrantees, currentUser, addedGrantees, areGranularPermissionsSupported, currentUserPermissions, isSharedObjectLocked, sharedObjectRef, isGranteeShareLoading, onDelete, onAddUserOrGroups, onGranularGranteeChange, }) {
    const onSelectGrantee = useCallback((grantee) => {
        if (!appliedGrantees.some((g) => areObjRefsEqual(g.id, grantee.id))) {
            const isGranularGrantee = areGranularPermissionsSupported &&
                (isGranteeUser(grantee) || isGranteeGroup(grantee) || isGranteeRules(grantee));
            if (isGranularGrantee) {
                onAddUserOrGroups(enrichGranteeWithDefaultPermission(grantee));
            }
            else {
                onAddUserOrGroups(grantee);
            }
        }
    }, [appliedGrantees, onAddUserOrGroups, areGranularPermissionsSupported]);
    return (_jsxs(_Fragment, { children: [
            _jsx(AddGranteeSelect, { currentUser: currentUser, appliedGrantees: appliedGrantees, sharedObjectRef: sharedObjectRef, onSelectGrantee: onSelectGrantee }), _jsx(GranteeList, { currentUserPermissions: currentUserPermissions, isSharedObjectLocked: isSharedObjectLocked, grantees: addedGrantees, mode: "AddGrantee", areGranularPermissionsSupported: areGranularPermissionsSupported, onDelete: onDelete, onChange: onGranularGranteeChange, isGranteeShareLoading: isGranteeShareLoading })
        ] }));
}
