import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useCallback } from "react";
import { isWidget } from "@gooddata/sdk-model";
import { buildAutomationUrl, navigate, useWorkspaceStrict } from "@gooddata/sdk-ui";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useDashboardAlerts } from "../../../model/react/useDashboardAlerting/useDashboardAlerts.js";
import { useWorkspaceUsers } from "../../../model/react/useWorkspaceUsers.js";
import { selectEnableAutomationManagement, selectExternalRecipient, selectIsEmbedded, selectSettings, } from "../../../model/store/config/configSelectors.js";
import { selectDashboardId } from "../../../model/store/meta/metaSelectors.js";
import { AlertingDialog } from "../alerting/AlertingDialog.js";
import { AlertingManagementDialog } from "../alerting/AlertingManagementDialog.js";
import { AlertingDialogContextProvider } from "../contexts/AlertingDialogContext.js";
import { AlertingManagementDialogContextProvider } from "../contexts/AlertingManagementDialogContext.js";
import { AutomationsContextProvider } from "../contexts/AutomationsContext.js";
import { useBuildAlertingDialogContext } from "./hooks/useBuildAlertingDialogContext.js";
import { useBuildAlertingManagementDialogContext } from "./hooks/useBuildAlertingManagementDialogContext.js";
import { useBuildAutomationsContext } from "./hooks/useBuildAutomationsContext.js";
/**
 * Provides AutomationsContext to its children, built from dashboard Redux state.
 * Wraps the alerting dialog subtree (both the create/edit and management dialogs reach
 * AutomationsContext through this provider).
 *
 * @internal
 */
export function AlertingAutomationsProvider({ children }) {
    const automationsCtx = useBuildAutomationsContext();
    return _jsx(AutomationsContextProvider, { value: automationsCtx, children: children });
}
/**
 * Connector component that reads from the dashboard Redux store and wires up
 * the alerting dialog tree (create/edit and management) via context providers.
 *
 * This is the primary bridge between dashboard store state and the alerting
 * dialog tree. One transitive exception remains: useAutomationAlertParameters
 * (in shared/automationFilters) still reads the store via useDashboardSelector,
 * so alerting is not yet fully decoupled. That coupling is an explicit
 * carve-out frozen in the `automationFilters` allowlist of .dependency-cruiser.js
 * (see the GDP-3167 note there) and is tracked for a Phase 3 move behind this
 * connector.
 *
 * AutomationsContext is provided by AlertingAutomationsProvider, which wraps this
 * connector (see AlertingDialogProvider).
 *
 * @internal
 */
export function AlertingConnector() {
    const alerts = useDashboardAlerts();
    if (!alerts.isInitialized) {
        return null;
    }
    return _jsx(AlertingConnectorInitialized, { ...alerts });
}
function AlertingConnectorInitialized(alerts) {
    const { isAlertDialogOpen, isAlertManagementDialogOpen } = alerts;
    // Defer store reads and user loading until at least one dialog is open.
    if (!isAlertDialogOpen && !isAlertManagementDialogOpen) {
        return null;
    }
    return _jsx(AlertingConnectorWithData, { alerts: alerts });
}
function AlertingConnectorWithData({ alerts }) {
    const { 
    // Shared Local State
    alertToEdit, 
    // Data
    automations, automationsError, automationsLoading, notificationChannels, 
    // Single Alert Dialog
    isAlertDialogOpen, onAlertingCancel, onAlertingCreateSuccess, onAlertingCreateError, onAlertingSaveSuccess, onAlertingSaveError, 
    // Management / List Dialog
    isAlertManagementDialogOpen, onAlertingManagementClose, onAlertingManagementAdd, onAlertingManagementEdit, onAlertingManagementDeleteSuccess, onAlertingManagementDeleteError, onAlertingManagementPauseSuccess, onAlertingManagementPauseError, widget, insight, } = alerts;
    const workspace = useWorkspaceStrict();
    const enableAutomationManagement = useDashboardSelector(selectEnableAutomationManagement);
    const dashboardId = useDashboardSelector(selectDashboardId);
    const isEmbedded = useDashboardSelector(selectIsEmbedded);
    const externalRecipientOverride = useDashboardSelector(selectExternalRecipient);
    const settings = useDashboardSelector(selectSettings);
    const useHostRoute = Boolean(settings?.enableShellApplication) && Boolean(settings?.enableShellApplication_dashboards);
    // Cross-dashboard edit routing lives in the connector (which may read router + store).
    // The management dialog only invokes this injected callback.
    const handleManagementEdit = useCallback((alert) => {
        const targetDashboardId = alert.dashboard?.id;
        if (enableAutomationManagement && targetDashboardId && targetDashboardId !== dashboardId) {
            navigate(buildAutomationUrl({
                workspaceId: workspace,
                dashboardId: targetDashboardId,
                automationId: alert.id,
                isEmbedded,
                useHostRoute,
                queryParams: externalRecipientOverride
                    ? { recipient: externalRecipientOverride }
                    : undefined,
            }));
            return;
        }
        onAlertingManagementEdit(alert);
    }, [
        onAlertingManagementEdit,
        enableAutomationManagement,
        dashboardId,
        workspace,
        isEmbedded,
        useHostRoute,
        externalRecipientOverride,
    ]);
    const insightWidget = isWidget(widget) ? widget : undefined;
    const alertingCtx = useBuildAlertingDialogContext({
        mode: alertToEdit ? "edit" : "create",
        widget: insightWidget,
        insight,
    });
    const managementCtx = useBuildAlertingManagementDialogContext();
    const isManagementLoading = automationsLoading;
    return (_jsx(AlertingDialogContextProvider, { value: alertingCtx, children: _jsxs(AlertingManagementDialogContextProvider, { value: managementCtx, children: [isAlertManagementDialogOpen ? (
                // TODO(GDP-3167 phase3): automations, notificationChannels, and alertDataError
                // are still prop-threaded here because DefaultAlertingManagementDialog reads
                // them from props, not from AutomationsContext. Once all dialog fields are
                // migrated to read from context, remove these props and the corresponding prop types.
                _jsx(AlertingManagementDialog, { automations: automations, notificationChannels: notificationChannels, alertDataError: automationsError, isLoadingAlertingData: isManagementLoading, onAdd: onAlertingManagementAdd, onEdit: handleManagementEdit, onClose: onAlertingManagementClose, onDeleteSuccess: onAlertingManagementDeleteSuccess, onDeleteError: onAlertingManagementDeleteError, onPauseSuccess: onAlertingManagementPauseSuccess, onPauseError: onAlertingManagementPauseError })) : null, isAlertDialogOpen ? (
                // TODO(GDP-3167 phase3): notificationChannels, users, usersError, and isLoading
                // are still prop-threaded here because DefaultAlertingDialog reads them from props,
                // not from AutomationsContext. Once all dialog fields are migrated to read from
                // context, remove these props and the corresponding prop types.
                _jsx(AlertingDialogWithUsers, { alertToEdit: alertToEdit, notificationChannels: notificationChannels, widget: insightWidget, insight: insight, isLoading: automationsLoading, onCancel: onAlertingCancel, onError: onAlertingCreateError, onSuccess: onAlertingCreateSuccess, onSaveError: onAlertingSaveError, onSaveSuccess: onAlertingSaveSuccess, onDeleteSuccess: onAlertingManagementDeleteSuccess, onDeleteError: onAlertingManagementDeleteError })) : null] }) }));
}
/**
 * Loads workspace users only while the create/edit dialog is mounted. Keeping the
 * `useWorkspaceUsers()` call in this child (rather than in AlertingConnectorWithData) means that
 * opening only the management dialog — which does not consume users — never dispatches the
 * workspace-users load, matching the pre-separation behavior.
 */
function AlertingDialogWithUsers(props) {
    const { users, status: usersStatus, usersError } = useWorkspaceUsers();
    return (_jsx(AlertingDialog, { ...props, users: users ?? [], usersError: usersError, isLoading: props.isLoading || usersStatus === "pending" || usersStatus === "loading" }));
}
