import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { useCallback } from "react";
import { useBackendStrict, useWorkspaceStrict } from "@gooddata/sdk-ui";
import { useToastMessage } from "@gooddata/sdk-ui-kit";
import { messages } from "../../../locales.js";
import { changeSharing } from "../../../model/commands/dashboard.js";
import { useDashboardDispatch, useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useDashboardCommandProcessing } from "../../../model/react/useDashboardCommandProcessing.js";
import { useDashboardUserInteraction } from "../../../model/react/useDashboardUserInteraction.js";
import { selectEnableDashboardShareDialogLink } from "../../../model/store/config/configSelectors.js";
import { selectDashboardPermissions } from "../../../model/store/dashboardPermissions/dashboardPermissionsSelectors.js";
import { selectPersistedDashboard } from "../../../model/store/meta/metaSelectors.js";
import { selectCanManageWorkspace } from "../../../model/store/permissions/permissionsSelectors.js";
import { selectFilterContextFilters } from "../../../model/store/tabs/filterContext/filterContextSelectors.js";
import { selectIsDashboardShareLinkVisible, selectIsShareGrantVisible, } from "../../../model/store/topBar/topBarSelectors.js";
import { uiActions } from "../../../model/store/ui/index.js";
import { selectIsShareDialogOpen } from "../../../model/store/ui/uiSelectors.js";
import { selectCurrentUser } from "../../../model/store/user/userSelectors.js";
import { ShareDialog } from "../../shareDialog/ShareDialog.js";
const useShareDialogDashboardHeader = () => {
    const dispatch = useDashboardDispatch();
    const { addSuccess, addError } = useToastMessage();
    const { shareDialogInteraction } = useDashboardUserInteraction();
    const isShareDialogOpen = useDashboardSelector(selectIsShareDialogOpen);
    const persistedDashboard = useDashboardSelector(selectPersistedDashboard);
    const currentUser = useDashboardSelector(selectCurrentUser);
    const isWorkspaceManager = useDashboardSelector(selectCanManageWorkspace);
    const dashboardPermissions = useDashboardSelector(selectDashboardPermissions);
    const dashboardFilters = useDashboardSelector(selectFilterContextFilters);
    const isShareGrantHidden = !useDashboardSelector(selectIsShareGrantVisible);
    const isDashboardShareDialogLinkEnabled = useDashboardSelector(selectEnableDashboardShareDialogLink);
    const showDashboardShareLink = useDashboardSelector(selectIsDashboardShareLinkVisible);
    const backend = useBackendStrict();
    const workspace = useWorkspaceStrict();
    const applyShareGrantOnSelect = isDashboardShareDialogLinkEnabled;
    const { run: runChangeSharing, status } = useDashboardCommandProcessing({
        commandCreator: changeSharing,
        successEvent: "GDC.DASH/EVT.SHARING.CHANGED",
        errorEvent: "GDC.DASH/EVT.COMMAND.FAILED",
        onSuccess: () => {
            addSuccess(messages.messagesSharingChangedSuccess);
        },
        onError: () => {
            addError(messages.messagesSharingChangedError);
        },
    });
    const isGranteeShareLoading = status === "running";
    const closeShareDialog = useCallback(() => dispatch(uiActions.closeShareDialog()), [dispatch]);
    const onCloseShareDialog = useCallback(() => {
        closeShareDialog();
    }, [closeShareDialog]);
    const onApplyShareDialog = useCallback((payload, closeOnApply = true) => {
        if (closeOnApply) {
            closeShareDialog();
        }
        runChangeSharing(payload);
    }, [closeShareDialog, runChangeSharing]);
    const onErrorShareDialog = useCallback(() => {
        dispatch(uiActions.closeShareDialog());
        addError(messages.messagesSharingDialogError);
    }, [dispatch, addError]);
    const onShareLinkCopy = useCallback((shareLink) => {
        void navigator.clipboard.writeText(shareLink);
        addSuccess(messages.messagesShareLinkCopied);
    }, [addSuccess]);
    return {
        backend,
        workspace,
        isShareDialogOpen,
        persistedDashboard,
        currentUser,
        onCloseShareDialog,
        onApplyShareDialog,
        onErrorShareDialog,
        onInteractionShareDialog: shareDialogInteraction,
        isLockingSupported: isWorkspaceManager,
        isCurrentUserWorkspaceManager: isWorkspaceManager,
        dashboardPermissions,
        dashboardFilters,
        isShareGrantHidden,
        applyShareGrantOnSelect,
        showDashboardShareLink,
        onShareLinkCopy,
        isGranteeShareLoading,
    };
};
/**
 * @internal
 */
export function ShareDialogDashboardHeader() {
    const { backend, workspace, isShareDialogOpen, persistedDashboard, currentUser, onCloseShareDialog, onApplyShareDialog, onErrorShareDialog, onInteractionShareDialog, isLockingSupported, isCurrentUserWorkspaceManager, dashboardPermissions, dashboardFilters, isShareGrantHidden, applyShareGrantOnSelect, showDashboardShareLink, onShareLinkCopy, isGranteeShareLoading, } = useShareDialogDashboardHeader();
    if (!isShareDialogOpen) {
        return null;
    }
    const currentUserPermissions = {
        canEditAffectedObject: dashboardPermissions.canEditDashboard,
        canEditLockedAffectedObject: dashboardPermissions.canEditLockedDashboard,
        canShareAffectedObject: dashboardPermissions.canShareDashboard,
        canShareLockedAffectedObject: dashboardPermissions.canShareLockedDashboard,
        canViewAffectedObject: dashboardPermissions.canViewDashboard,
    };
    return (_jsx(ShareDialog, { backend: backend, workspace: workspace, isVisible: isShareDialogOpen, currentUser: currentUser, sharedObject: persistedDashboard, onCancel: onCloseShareDialog, onApply: onApplyShareDialog, onError: onErrorShareDialog, isLockingSupported: isLockingSupported, isCurrentUserWorkspaceManager: isCurrentUserWorkspaceManager, currentUserPermissions: currentUserPermissions, dashboardFilters: dashboardFilters, onInteraction: onInteractionShareDialog, isShareGrantHidden: isShareGrantHidden, applyShareGrantOnSelect: applyShareGrantOnSelect, showDashboardShareLink: showDashboardShareLink, onShareLinkCopy: onShareLinkCopy, isGranteeShareLoading: isGranteeShareLoading }));
}
//# sourceMappingURL=ShareDialogDashboardHeader.js.map