import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useMemo } from "react";
import { compact } from "lodash-es";
import { useIntl } from "react-intl";
import { ConfirmDialogBase } from "../../ConfirmDialogBase.js";
import { ContentDivider } from "../../ContentDivider.js";
import { AdminInformationMessage } from "./AdminInformationMessage.js";
import { SharedObjectLockControl } from "./SharedObjectLockControl.js";
import { SharedObjectUnderLenientControl } from "./SharedObjectUnderLenientControl.js";
import { ShareGranteeContent } from "./ShareGranteeContent.js";
import { ShareLink } from "./ShareLink.js";
import { ADD_GRANTEE_ID } from "./utils.js";
/**
 * @internal
 */
export function ShareGranteeBase({ isLoading, isLockedNow, isUnderLenientControlNow, grantees, sharedObject, isDirty, currentUserPermissions, dashboardFilters, dashboardParameters, dashboardLinkBasePath, isShareGrantHidden, applyShareGrantOnSelect, showDashboardShareLink, isGranteeShareLoading, onCancel, onSubmit, onGranteeDelete, onGranularGranteeChange, onAddGranteeButtonClick, onLockChange, onUnderLenientControlChange, isCurrentUserWorkspaceManager, onShareLinkCopy, }) {
    const { owner, isLeniencyControlSupported, isLockingSupported, areGranularPermissionsSupported, isMetadataObjectLockingSupported, isLocked, canWorkspaceManagerSeeEverySharedObject, } = sharedObject;
    const intl = useIntl();
    const granteeList = useMemo(() => {
        return compact([owner, ...grantees]);
    }, [grantees, owner]);
    const isUsingTabs = window.location.hash.includes("/tab/");
    const dialogLabels = useMemo(() => {
        const shareDashboardTitle = intl.formatMessage({ id: "shareDialog.share.dashboard.title" });
        const shareGranteeTitle = intl.formatMessage({ id: "shareDialog.share.grantee.title" });
        const shareDashboardListTitle = intl.formatMessage({ id: "shareDialog.share.dashboard.list.title" });
        const shareGranteeListTitle = intl.formatMessage({ id: "shareDialog.share.grantee.list.title" });
        const cancelButtonText = intl.formatMessage({ id: "cancel" });
        const closeButtonText = intl.formatMessage({ id: "close" });
        return {
            headline: showDashboardShareLink ? shareDashboardTitle : shareGranteeTitle,
            shareGrantHeadline: showDashboardShareLink ? shareDashboardListTitle : shareGranteeListTitle,
            cancelButtonText: applyShareGrantOnSelect ? closeButtonText : cancelButtonText,
            linkHeadline: intl.formatMessage({ id: "shareDialog.share.link.title" }),
            linkHelperText: isUsingTabs
                ? intl.formatMessage({ id: "shareDialog.share.link.helperTextWithTabs" })
                : intl.formatMessage({ id: "shareDialog.share.link.helperText" }),
            linkButtonLabel: intl.formatMessage({ id: "shareDialog.share.link.buttonText" }),
            submitButtonText: intl.formatMessage({ id: "save" }),
        };
    }, [intl, showDashboardShareLink, applyShareGrantOnSelect, isUsingTabs]);
    const shouldDisplayAdminMessage = useMemo(() => canWorkspaceManagerSeeEverySharedObject && isCurrentUserWorkspaceManager && !isLoading, [canWorkspaceManagerSeeEverySharedObject, isCurrentUserWorkspaceManager, isLoading]);
    return (_jsxs(ConfirmDialogBase, { className: "gd-share-dialog s-gd-share-grantees", displayCloseButton: true, isPositive: true, isSubmitDisabled: !isDirty, headline: dialogLabels.headline, cancelButtonText: dialogLabels.cancelButtonText, submitButtonText: dialogLabels.submitButtonText, onCancel: onCancel, onSubmit: onSubmit, hideSubmitButton: applyShareGrantOnSelect, initialFocus: ADD_GRANTEE_ID, children: [
            _jsx(AdminInformationMessage, { isVisible: shouldDisplayAdminMessage ?? false }), isShareGrantHidden ? null : (_jsx(ShareGranteeContent, { currentUserPermissions: currentUserPermissions, isSharedObjectLocked: isLocked, isLoading: isLoading, grantees: granteeList, areGranularPermissionsSupported: areGranularPermissionsSupported, onAddGrantee: onAddGranteeButtonClick, onDelete: onGranteeDelete, onChange: onGranularGranteeChange, isGranteeShareLoading: isGranteeShareLoading, applyShareGrantOnSelect: !!applyShareGrantOnSelect, headline: dialogLabels.shareGrantHeadline })), showDashboardShareLink ? (_jsx(ShareLink, { dashboardFilters: dashboardFilters, dashboardParameters: dashboardParameters, dashboardLinkBasePath: dashboardLinkBasePath, onShareLinkCopy: onShareLinkCopy, headline: dialogLabels.linkHeadline, helperText: dialogLabels.linkHelperText, buttonLabel: dialogLabels.linkButtonLabel })) : null, _jsx(ContentDivider, { className: "gd-share-dialog-content-divider" }), _jsx(SharedObjectUnderLenientControl, { isUnderLenientControl: isUnderLenientControlNow, isLeniencyControlSupported: isLeniencyControlSupported, onUnderLenientControlChange: onUnderLenientControlChange }), isMetadataObjectLockingSupported ? (_jsx(SharedObjectLockControl, { isLocked: isLockedNow, isLockingSupported: isLockingSupported, onLockChange: onLockChange })) : null] }));
}
