import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import { BackendProvider, IntlWrapper, UnexpectedSdkError, WorkspaceProvider, useBackendStrict, useWorkspaceStrict, } from "@gooddata/sdk-ui";
import { ComponentInteractionProvider } from "./ShareDialogBase/ComponentInteractionContext.js";
import { ComponentLabelsProvider } from "./ShareDialogBase/ComponentLabelsContext.js";
import { ShareDialogBase } from "./ShareDialogBase/ShareDialogBase.js";
import { mapGranteesToGranularAccessGrantees, mapGranteesToShareStatus, mapOwnerToGrantee, mapSharedObjectToAffectedSharedObject, mapUserToInactiveOwner, } from "./shareDialogMappers.js";
/**
 * @internal
 */
export function ShareDialog({ backend, workspace, locale, sharedObject, currentUser, onApply, onCancel, onError, onInteraction = () => { }, isLockingSupported, isCurrentUserWorkspaceManager, isGranteeShareLoading, labels, currentUserPermissions, dashboardFilters, dashboardParameters, dashboardLinkBasePath, isShareGrantHidden, applyShareGrantOnSelect, showDashboardShareLink, onShareLinkCopy, }) {
    const effectiveBackend = useBackendStrict(backend);
    const effectiveWorkspace = useWorkspaceStrict(workspace);
    const areGranularPermissionsSupported = effectiveBackend.capabilities.supportsGranularAccessControl;
    const isLeniencyControlSupported = !effectiveBackend.capabilities.usesStrictAccessControl;
    const isMetadataObjectLockingSupported = effectiveBackend.capabilities.supportsMetadataObjectLocking;
    const canWorkspaceManagerSeeEverySharedObject = effectiveBackend.capabilities.canWorkspaceManagerSeeEverySharedObject;
    const { createdBy } = sharedObject;
    const { ref: currentUserRef } = currentUser;
    const onShareDialogBaseError = useCallback((err) => {
        onError?.(new UnexpectedSdkError(err.message, err));
    }, [onError]);
    const owner = useMemo(() => {
        if (areGranularPermissionsSupported) {
            return undefined;
        }
        if (createdBy) {
            return mapOwnerToGrantee(createdBy, currentUserRef);
        }
        return mapUserToInactiveOwner();
    }, [createdBy, currentUserRef, areGranularPermissionsSupported]);
    const onSubmit = useCallback((grantees, granteesToAdd, granteesToDelete, isUnderLenientControl, isLocked, closeOnApply) => {
        const shareStatus = mapGranteesToShareStatus(grantees, granteesToAdd, granteesToDelete);
        const isUnderStrictControl = shareStatus !== "public" && !isUnderLenientControl;
        const addAccess = mapGranteesToGranularAccessGrantees(granteesToAdd, true);
        const revokeAccess = mapGranteesToGranularAccessGrantees(granteesToDelete);
        onApply({
            shareStatus,
            isUnderStrictControl,
            isLocked,
            granteesToAdd: addAccess,
            granteesToDelete: revokeAccess,
        }, closeOnApply);
    }, [onApply]);
    const affectedSharedObject = useMemo(() => {
        return mapSharedObjectToAffectedSharedObject(sharedObject, owner, isLockingSupported, isLeniencyControlSupported, areGranularPermissionsSupported, isMetadataObjectLockingSupported, canWorkspaceManagerSeeEverySharedObject);
    }, [
        sharedObject,
        owner,
        isLockingSupported,
        isLeniencyControlSupported,
        areGranularPermissionsSupported,
        isMetadataObjectLockingSupported,
        canWorkspaceManagerSeeEverySharedObject,
    ]);
    return (_jsx(IntlWrapper, { locale: locale, children: _jsx(BackendProvider, { backend: effectiveBackend, children: _jsx(WorkspaceProvider, { workspace: effectiveWorkspace, children: _jsx(ComponentLabelsProvider, { labels: labels, children: _jsx(ComponentInteractionProvider, { onInteraction: onInteraction, currentUser: currentUser, currentUserPermissions: currentUserPermissions, isCurrentUserWorkspaceManager: isCurrentUserWorkspaceManager, sharedObjectStatus: affectedSharedObject.shareStatus, isSharedObjectLocked: affectedSharedObject.isLocked, children: _jsx(ShareDialogBase, { currentUser: currentUser, sharedObject: affectedSharedObject, isCurrentUserWorkspaceManager: isCurrentUserWorkspaceManager, currentUserPermissions: currentUserPermissions, dashboardFilters: dashboardFilters, dashboardParameters: dashboardParameters, dashboardLinkBasePath: dashboardLinkBasePath, onCancel: onCancel, onSubmit: onSubmit, onError: onShareDialogBaseError, isShareGrantHidden: isShareGrantHidden, applyShareGrantOnSelect: applyShareGrantOnSelect, showDashboardShareLink: showDashboardShareLink, onShareLinkCopy: onShareLinkCopy, isGranteeShareLoading: isGranteeShareLoading }) }) }) }) }) }));
}
