import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import cx from "classnames";
import { 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 { DEFAULT_MAX_AUTOMATIONS } from "../../../model/react/useDashboardAutomations/constants.js";
import { selectEnableAccessibilityMode, selectExternalRecipient, selectIsEmbedded, selectIsWhiteLabeled, selectLocale, selectTimezone, } from "../../../model/store/config/configSelectors.js";
import { selectEntitlementMaxAutomations, selectEntitlementUnlimitedAutomations, } from "../../../model/store/entitlements/entitlementsSelectors.js";
import { selectDashboardId, selectDashboardTitle } from "../../../model/store/meta/metaSelectors.js";
import { selectCanCreateAutomation } from "../../../model/store/permissions/permissionsSelectors.js";
import { selectAutomationsInvalidationId, selectExecutionTimestamp, selectIsScheduleEmailDialogOpen, } 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 { SCHEDULED_EMAIL_DIALOG_ID } from "../DefaultScheduledEmailDialog/constants.js";
import { useScheduleEmailDialogAccessibility } from "../hooks/useScheduleEmailDialogAccessibility.js";
import { isMobileView } from "../utils/responsive.js";
const overlayController = OverlayController.getInstance(DASHBOARD_DIALOG_OVERS_Z_INDEX);
export function DefaultScheduledEmailManagementDialogContentEnhanced({ onAdd, onClose, onEdit, isLoadingScheduleData, automations, }) {
    const intl = useIntl();
    const isMobile = isMobileView();
    const workspace = useWorkspace();
    const backend = useBackend();
    const timezone = useDashboardSelector(selectTimezone);
    const locale = useDashboardSelector(selectLocale);
    const dashboardId = useDashboardSelector(selectDashboardId);
    const dashboardTitle = useDashboardSelector(selectDashboardTitle);
    const externalRecipientOverride = useDashboardSelector(selectExternalRecipient);
    const enableBulkActions = !useDashboardSelector(selectEnableAccessibilityMode);
    const isEditDialogOpen = useDashboardSelector(selectIsScheduleEmailDialogOpen);
    const canCreateAutomation = useDashboardSelector(selectCanCreateAutomation);
    const isWhiteLabeled = useDashboardSelector(selectIsWhiteLabeled);
    const isExecutionTimestampMode = !!useDashboardSelector(selectExecutionTimestamp);
    const maxAutomationsEntitlement = useDashboardSelector(selectEntitlementMaxAutomations);
    const unlimitedAutomationsEntitlement = useDashboardSelector(selectEntitlementUnlimitedAutomations);
    const automationsInvalidationId = useDashboardSelector(selectAutomationsInvalidationId);
    const isEmbedded = useDashboardSelector(selectIsEmbedded);
    const { returnFocusTo } = useScheduleEmailDialogAccessibility();
    const maxAutomations = parseInt(maxAutomationsEntitlement?.value ?? DEFAULT_MAX_AUTOMATIONS, 10);
    const maxAutomationsReached = automations.length >= maxAutomations && !unlimitedAutomationsEntitlement;
    const isAddButtonDisabled = useMemo(() => isLoadingScheduleData || maxAutomationsReached || isExecutionTimestampMode, [isLoadingScheduleData, maxAutomationsReached, isExecutionTimestampMode]);
    const availableFilters = externalRecipientOverride
        ? ["dashboard", "status"]
        : undefined;
    const helpTextId = isMobile
        ? defineMessage({ id: "dialogs.schedule.email.footer.title.short" }).id
        : defineMessage({ id: "dialogs.schedule.email.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, autofocusOnOpen: true, onCancel: onClose, className: cx("gd-notifications-channels-management-dialog s-scheduled-email-management-dialog", "gd-dialog--wide gd-notifications-channels-management-dialog--wide", "gd-dialog--no-padding"), accessibilityConfig: { titleElementId, isModal: true }, returnFocusAfterClose: true, returnFocusTo: returnFocusTo, 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: intl.formatMessage({ id: "dialogs.schedule.management.title" }) }) }), _jsx(ContentDivider, { className: "gd-content-divider--no-spacing" }), _jsx("div", { className: "gd-notifications-channels-content", children: _jsx(Automations, { workspace: workspace, timezone: timezone, type: "schedule", backend: backend, scope: "workspace", 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: locale, externalInvalidationId: automationsInvalidationId, renderToolbarCustomElement: () => canCreateAutomation ? (_jsx(CreateButton, { onClick: onAdd, isDisabled: isAddButtonDisabled, label: intl.formatMessage({
                                id: messages.scheduleManagementCreateNew.id,
                            }), tooltipContent: maxAutomationsReached
                                ? intl.formatMessage({
                                    id: messages.scheduleManagementCreateTooMany.id,
                                })
                                : isExecutionTimestampMode
                                    ? intl.formatMessage({
                                        id: messages.scheduleManagementExecutionTimestampMode.id,
                                    })
                                    : undefined, isTooltipDisabled: !maxAutomationsReached && !isExecutionTimestampMode, accessibilityConfig: {
                                ariaHaspopup: "dialog",
                                ariaControls: SCHEDULED_EMAIL_DIALOG_ID,
                            } })) : null, selectedColumnDefinitions: AUTOMATIONS_COLUMN_CONFIG, dashboardUrlBuilder: dashboardUrlBuilder }) }), _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/scheduled-exports/#ScheduleExportsinDashboards-ScheduleExport", 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, isDisabled, label, tooltipContent, isTooltipDisabled, accessibilityConfig, }) {
    return (_jsx(UiTooltip, { optimalPlacement: true, anchor: _jsx(Button, { onClick: onClick, disabled: isDisabled, value: label, size: "small", className: "gd-button-action", accessibilityConfig: accessibilityConfig }), disabled: isTooltipDisabled, triggerBy: ["hover"], content: tooltipContent }));
}
//# sourceMappingURL=DefaultScheduledEmailManagementDialogContentEnhanced.js.map