// (C) 2022-2026 GoodData Corporation
import { selectAllAutomationsCount, selectAutomationsError, selectAutomationsIsInitialized, selectAutomationsIsLoading, selectDashboardUserAutomationAlertsInContext, } from "../../store/automations/automationsSelectors.js";
import { selectEnableAlerting, selectIsReadOnly } from "../../store/config/configSelectors.js";
import { selectEntitlementMaxAutomations, selectEntitlementUnlimitedAutomations, } from "../../store/entitlements/entitlementsSelectors.js";
import { selectInsightByWidgetRef } from "../../store/insights/insightsSelectors.js";
import { selectNotificationChannels, selectNotificationChannelsCount, } from "../../store/notificationChannels/notificationChannelsSelectors.js";
import { selectCanCreateAutomation, selectCanManageWorkspace, } from "../../store/permissions/permissionsSelectors.js";
import { selectIsInViewMode } from "../../store/renderMode/renderModeSelectors.js";
import { selectFilterableWidgetByRef } from "../../store/tabs/layout/layoutSelectors.js";
import { selectAlertingDialogContext, selectIsAlertingDialogOpen, selectIsAlertsManagementDialogOpen, selectMenuButtonItemsVisibility, } from "../../store/ui/uiSelectors.js";
import { useDashboardSelector } from "../DashboardStoreProvider.js";
import { DEFAULT_MAX_AUTOMATIONS } from "../useDashboardAutomations/constants.js";
/**
 * @internal
 */
export const useDashboardAlertsData = ({ alertToEdit }) => {
    const isInitialized = useDashboardSelector(selectAutomationsIsInitialized);
    const automations = useDashboardSelector(selectDashboardUserAutomationAlertsInContext(undefined));
    const automationsCount = useDashboardSelector(selectAllAutomationsCount);
    const automationsLoading = useDashboardSelector(selectAutomationsIsLoading);
    const automationsError = useDashboardSelector(selectAutomationsError);
    // Dashboard mode
    const isReadOnly = useDashboardSelector(selectIsReadOnly);
    const isInViewMode = useDashboardSelector(selectIsInViewMode);
    // Visibility configuration
    const menuButtonItemsVisibility = useDashboardSelector(selectMenuButtonItemsVisibility);
    // Feature flags
    const isAlertingEnabled = useDashboardSelector(selectEnableAlerting);
    // Permissions
    const isWorkspaceManager = useDashboardSelector(selectCanManageWorkspace);
    const canCreateAutomation = useDashboardSelector(selectCanCreateAutomation);
    // Entitlements
    const maxAutomationsEntitlement = useDashboardSelector(selectEntitlementMaxAutomations);
    const unlimitedAutomationsEntitlement = useDashboardSelector(selectEntitlementUnlimitedAutomations);
    const maxAutomations = parseInt(maxAutomationsEntitlement?.value ?? DEFAULT_MAX_AUTOMATIONS, 10);
    const notificationChannels = useDashboardSelector(selectNotificationChannels);
    const numberOfAvailableDestinations = useDashboardSelector(selectNotificationChannelsCount);
    const maxAutomationsReached = automationsCount >= maxAutomations && !unlimitedAutomationsEntitlement;
    /**
     * We want to hide scheduling when there are no webhooks unless the user is admin.
     */
    const showDueToNumberOfAvailableDestinations = numberOfAvailableDestinations > 0 || isWorkspaceManager;
    const isAlertingAvailable = isInViewMode &&
        !isReadOnly &&
        isAlertingEnabled &&
        showDueToNumberOfAvailableDestinations &&
        (menuButtonItemsVisibility.alertingButton ?? true);
    // Single Schedule Dialog
    const isAlertingVisible = isAlertingAvailable && canCreateAutomation && !maxAutomationsReached;
    const isAlertDialogOpen = useDashboardSelector(selectIsAlertingDialogOpen) || false;
    const alertDialogContext = useDashboardSelector(selectAlertingDialogContext);
    // List / Management Dialog
    const isAlertManagementVisible = isAlertingAvailable && (canCreateAutomation || automations.length > 0);
    const isAlertManagementDialogOpen = useDashboardSelector(selectIsAlertsManagementDialogOpen) || false;
    // Widget and Insight
    const editWidgetId = alertToEdit?.metadata?.widget;
    const editWidgetRef = editWidgetId ? { identifier: editWidgetId } : undefined;
    const widget = useDashboardSelector(selectFilterableWidgetByRef(alertDialogContext?.widgetRef ?? editWidgetRef));
    const insight = useDashboardSelector(selectInsightByWidgetRef(widget?.ref));
    return {
        // Data
        isInitialized,
        notificationChannels,
        automations,
        automationsLoading,
        automationsError,
        automationsCount,
        numberOfAvailableDestinations,
        widget,
        insight,
        // Single Alert Dialog
        isAlertingVisible,
        isAlertDialogOpen,
        // List / Management Dialog
        isAlertManagementVisible,
        isAlertManagementDialogOpen,
    };
};
//# sourceMappingURL=useDashboardAlertsData.js.map