import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useMemo } from "react";
import { FormattedMessage, defineMessage, useIntl } from "react-intl";
import { AddButton, Button, Dialog, Hyperlink, OverlayController, OverlayControllerProvider, Typography, 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 { selectIsWhiteLabeled } from "../../../../model/store/config/configSelectors.js";
import { selectEntitlementMaxAutomations, selectEntitlementUnlimitedAutomations, } from "../../../../model/store/entitlements/entitlementsSelectors.js";
import { selectCanCreateAutomation } from "../../../../model/store/permissions/permissionsSelectors.js";
import { selectExecutionTimestamp } from "../../../../model/store/ui/uiSelectors.js";
import { selectCurrentUser } from "../../../../model/store/user/userSelectors.js";
import { DASHBOARD_DIALOG_OVERS_Z_INDEX } from "../../../constants/zIndex.js";
import { useScheduleEmailDialogAccessibility } from "../hooks/useScheduleEmailDialogAccessibility.js";
import { isMobileView } from "../utils/responsive.js";
import { ScheduledEmails } from "./components/ScheduledEmailsList.js";
const overlayController = OverlayController.getInstance(DASHBOARD_DIALOG_OVERS_Z_INDEX);
export function DefaultScheduledEmailManagementDialogContentBasic({ onAdd, onClose, onDelete, onEdit, isLoadingScheduleData, automations, notificationChannels, }) {
    const intl = useIntl();
    const isMobile = isMobileView();
    const currentUser = useDashboardSelector(selectCurrentUser);
    const canCreateAutomation = useDashboardSelector(selectCanCreateAutomation);
    const isWhiteLabeled = useDashboardSelector(selectIsWhiteLabeled);
    const isExecutionTimestampMode = !!useDashboardSelector(selectExecutionTimestamp);
    const maxAutomationsEntitlement = useDashboardSelector(selectEntitlementMaxAutomations);
    const unlimitedAutomationsEntitlement = useDashboardSelector(selectEntitlementUnlimitedAutomations);
    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 helpTextId = isMobile
        ? defineMessage({ id: "dialogs.schedule.email.footer.title.short" }).id
        : defineMessage({ id: "dialogs.schedule.email.footer.title" }).id;
    const titleElementId = useId();
    return (_jsx(Dialog, { displayCloseButton: true, autofocusOnOpen: true, onCancel: onClose, className: "gd-notifications-channels-management-dialog s-scheduled-email-management-dialog", accessibilityConfig: { titleElementId, isModal: true }, returnFocusAfterClose: true, returnFocusTo: returnFocusTo, 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" }) }) }), _jsxs("div", { className: "gd-notifications-channels-content", children: [
                        _jsxs("div", { className: "gd-notifications-channels-content-header", children: [
                                _jsx(Typography, { tagName: "h3", children: intl.formatMessage({ id: messages.scheduleManagementListTitle.id }) }), canCreateAutomation ? (_jsx(AddButton, { onClick: onAdd, isDisabled: isAddButtonDisabled, title: _jsx(FormattedMessage, { id: messages.scheduleManagementCreate.id }), tooltip: maxAutomationsReached ? (_jsx(FormattedMessage, { id: messages.scheduleManagementCreateTooMany.id })) : isExecutionTimestampMode ? (_jsx(FormattedMessage, { id: messages.scheduleManagementExecutionTimestampMode.id })) : undefined })) : null] }), _jsx(ScheduledEmails, { onDelete: onDelete, onEdit: onEdit, isLoading: isLoadingScheduleData, scheduledEmails: automations, currentUserEmail: currentUser?.email, noSchedulesMessageId: messages.scheduleManagementNoSchedules.id, notificationChannels: notificationChannels })
                    ] }), _jsx("div", { className: "gd-content-divider" }), _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", children: _jsx(Button, { onClick: onClose, className: "gd-button-secondary s-close-button", value: intl.formatMessage({ id: "close" }) }) })
                    ] })
            ] }) }));
}
