import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback } from "react";
import cx from "classnames";
import { FormattedMessage, defineMessage, useIntl } from "react-intl";
import { buildDashboardUrl, useBackend, useWorkspace } from "@gooddata/sdk-ui";
import { Automations } from "@gooddata/sdk-ui-ext";
import { Button, ContentDivider, Dialog, Hyperlink, OverlayController, OverlayControllerProvider, Typography, UiTooltip, useId, } from "@gooddata/sdk-ui-kit";
import { messages } from "../../../locales.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectEnableAccessibilityMode, selectExternalRecipient, selectIsEmbedded, selectIsWhiteLabeled, selectTimezone, } from "../../../model/store/config/configSelectors.js";
import { selectInsightByWidgetRef } from "../../../model/store/insights/insightsSelectors.js";
import { selectDashboardId, selectDashboardTitle } from "../../../model/store/meta/metaSelectors.js";
import { selectCanCreateAutomation } from "../../../model/store/permissions/permissionsSelectors.js";
import { selectWidgetByRef } from "../../../model/store/tabs/layout/layoutSelectors.js";
import { selectAutomationsInvalidationId, selectIsAlertingDialogOpen, selectIsAlertingManagementDialogContext, } from "../../../model/store/ui/uiSelectors.js";
import { AUTOMATIONS_COLUMN_CONFIG, AUTOMATIONS_MAX_HEIGHT } from "../../constants/automations.js";
import { DASHBOARD_DIALOG_OVERS_Z_INDEX } from "../../constants/zIndex.js";
import { ALERTING_DIALOG_ID } from "../DefaultAlertingDialog/constants.js";
import { isMobileView } from "../DefaultAlertingDialog/utils/responsive.js";
import { useAlertingDialogAccessibility } from "../hooks/useAlertingDialogAccessibility.js";
import { useGetSupportedMeasures } from "../hooks/useGetSupportedMeasures.js";
const overlayController = OverlayController.getInstance(DASHBOARD_DIALOG_OVERS_Z_INDEX);
/**
 * @internal
 */
export function DefaultAlertingManagementDialogContentEnhanced({ onAdd, onClose, onEdit, }) {
    const intl = useIntl();
    const workspace = useWorkspace();
    const backend = useBackend();
    const timezone = useDashboardSelector(selectTimezone);
    const canCreateAutomation = useDashboardSelector(selectCanCreateAutomation);
    const isWhiteLabeled = useDashboardSelector(selectIsWhiteLabeled);
    const isEditDialogOpen = useDashboardSelector(selectIsAlertingDialogOpen);
    const managementDialogContext = useDashboardSelector(selectIsAlertingManagementDialogContext);
    const enableBulkActions = !useDashboardSelector(selectEnableAccessibilityMode);
    const dashboardId = useDashboardSelector(selectDashboardId);
    const dashboardTitle = useDashboardSelector(selectDashboardTitle);
    const externalRecipientOverride = useDashboardSelector(selectExternalRecipient);
    const { returnFocusTo } = useAlertingDialogAccessibility();
    const automationsInvalidationId = useDashboardSelector(selectAutomationsInvalidationId);
    const isEmbedded = useDashboardSelector(selectIsEmbedded);
    const isMobile = isMobileView();
    // Check if widget has metrics for alert creation
    const widget = useDashboardSelector(selectWidgetByRef(managementDialogContext.widgetRef));
    const insight = useDashboardSelector(selectInsightByWidgetRef(widget?.ref));
    const supportedMeasures = useGetSupportedMeasures(managementDialogContext.widgetRef ? insight : undefined);
    const hasMetrics = supportedMeasures.length > 0;
    const availableFilters = externalRecipientOverride
        ? ["dashboard", "status"]
        : undefined;
    const helpTextId = isMobile
        ? defineMessage({ id: "dialogs.alerting.footer.title.short" }).id
        : defineMessage({ id: "dialogs.alerting.footer.title" }).id;
    const dashboardUrlBuilder = useCallback(({ workspaceId, dashboardId, tabId }) => buildDashboardUrl({
        workspaceId,
        dashboardId,
        tabId,
        isEmbedded,
        queryParams: externalRecipientOverride ? { recipient: externalRecipientOverride } : undefined,
    }), [isEmbedded, externalRecipientOverride]);
    const titleElementId = useId();
    return (_jsx(Dialog, { displayCloseButton: true, onCancel: onClose, shouldCloseOnClick: () => false, autofocusOnOpen: true, className: cx("gd-notifications-channels-management-dialog s-alerting-management-dialog", "gd-dialog--wide gd-notifications-channels-management-dialog--wide", "gd-dialog--no-padding"), accessibilityConfig: { titleElementId, isModal: true }, returnFocusTo: returnFocusTo, returnFocusAfterClose: true, refocusKey: isEditDialogOpen, children: _jsxs(OverlayControllerProvider, { overlayController: overlayController, children: [
                _jsx("div", { className: "gd-notifications-channels-management-dialog-title", children: _jsx(Typography, { tagName: "h3", className: "gd-dialog-header", id: titleElementId, children: _jsx(FormattedMessage, { id: "dialogs.alerting.management.title" }) }) }), _jsx(ContentDivider, { className: "gd-content-divider--no-spacing" }), _jsx("div", { className: "gd-notifications-channels-content", children: _jsx(Automations, { workspace: workspace, timezone: timezone, backend: backend, scope: "workspace", type: "alert", maxHeight: AUTOMATIONS_MAX_HEIGHT, isMobileView: isMobile, tableVariant: "small", editAutomation: onEdit, preselectedFilters: {
                            dashboard: dashboardId
                                ? [{ value: dashboardId, label: dashboardTitle }]
                                : undefined,
                            externalRecipients: externalRecipientOverride
                                ? [{ value: externalRecipientOverride }]
                                : undefined,
                        }, enableBulkActions: enableBulkActions, availableFilters: availableFilters, locale: intl.locale, externalInvalidationId: automationsInvalidationId, dashboardUrlBuilder: dashboardUrlBuilder, renderToolbarCustomElement: () => managementDialogContext.widgetRef && canCreateAutomation ? (_jsx(CreateButton, { onClick: onAdd, hasMetrics: hasMetrics, label: intl.formatMessage({
                                id: messages.alertingManagementCreateNew.id,
                            }), tooltipContent: intl.formatMessage({
                                id: messages.alertingCreateNoMeasureTooltip.id,
                            }), accessibilityConfig: {
                                ariaHaspopup: "dialog",
                                ariaControls: ALERTING_DIALOG_ID,
                            } })) : null, selectedColumnDefinitions: AUTOMATIONS_COLUMN_CONFIG }) }), _jsx(ContentDivider, { className: "gd-content-divider--no-spacing" }), _jsxs("div", { className: `gd-buttons${isWhiteLabeled ? " gd-buttons--end" : ""}`, children: [isWhiteLabeled ? null : (_jsx(Hyperlink, { text: intl.formatMessage({ id: helpTextId }), href: "https://www.gooddata.com/docs/cloud/create-dashboards/automation/alerts/", iconClass: "gd-icon-circle-question" })), _jsx("div", { className: "gd-buttons-inner", children: _jsx(Button, { onClick: onClose, className: "gd-button-secondary s-close-button", value: intl.formatMessage({ id: "close" }) }) })
                    ] })
            ] }) }));
}
function CreateButton({ onClick, hasMetrics, label, tooltipContent }) {
    return (_jsx(UiTooltip, { optimalPlacement: true, anchor: _jsx(Button, { onClick: onClick, size: "small", className: "gd-button-action s-add-alert-button", value: label, disabled: !hasMetrics }), disabled: hasMetrics, triggerBy: ["hover"], content: tooltipContent }));
}
//# sourceMappingURL=DefaultAlertingManagementDialogContentEnhanced.js.map