import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2025 GoodData Corporation
import { useCallback, useMemo, useRef, useState } from "react";
import { invariant } from "ts-invariant";
import { assertNever } from "@gooddata/sdk-model";
import { Overlay, UiFocusManager, alignConfigToAlignPoint } from "@gooddata/sdk-ui-kit";
import { DefaultNotificationsPanel, } from "./DefaultNotificationsPanel.js";
import { DefaultNotificationsPanelHeader, } from "./DefaultNotificationsPanelHeader.js";
import { OrganizationProvider } from "../@staging/OrganizationContext/OrganizationContext.js";
import { NotificationsProvider, useNotificationsContext } from "../data/NotificationsContext.js";
import { IntlWrapper } from "../localization/IntlWrapper.js";
import { DefaultNotification, } from "../Notification/DefaultNotification.js";
import { DefaultNotificationsList, } from "../NotificationsList/DefaultNotificationsList.js";
import { DefaultNotificationsListEmptyState, } from "../NotificationsList/DefaultNotificationsListEmptyState.js";
import { DefaultNotificationsListErrorState, } from "../NotificationsList/DefaultNotificationsListErrorState.js";
import { DefaultNotificationSkeletonItem, } from "../NotificationsList/DefaultSkeletonItem.js";
const ALIGN_POINTS = [
    alignConfigToAlignPoint({ triggerAlignPoint: "bottom-right", overlayAlignPoint: "top-right" }),
    alignConfigToAlignPoint({ triggerAlignPoint: "bottom-left", overlayAlignPoint: "top-left" }),
    alignConfigToAlignPoint({ triggerAlignPoint: "top-right", overlayAlignPoint: "bottom-right" }),
    alignConfigToAlignPoint({ triggerAlignPoint: "top-left", overlayAlignPoint: "bottom-left" }),
];
/**
 * 10 minutes in milliseconds.
 */
const TEN_MINUTES = 1000 * 60 * 10;
/**
 * @public
 */
export function NotificationsPanel(props) {
    const { locale, refreshInterval = TEN_MINUTES, itemsPerPage = 50, backend, workspace, enableScheduleNotifications = false, } = props;
    return (_jsx(OrganizationProvider, { children: _jsx(NotificationsProvider, { backend: backend, workspace: workspace, refreshInterval: refreshInterval, itemsPerPage: itemsPerPage, enableScheduleNotifications: enableScheduleNotifications, children: _jsx(IntlWrapper, { locale: locale, children: _jsx(NotificationsPanelController, { ...props }) }) }) }));
}
/**
 * @internal
 */
function NotificationsPanelController({ NotificationsPanelButton, NotificationsPanel = DefaultNotificationsPanel, NotificationsPanelHeader = DefaultNotificationsPanelHeader, NotificationsList = DefaultNotificationsList, NotificationsListEmptyState = DefaultNotificationsListEmptyState, NotificationsListErrorState = DefaultNotificationsListErrorState, Notification = DefaultNotification, NotificationSkeletonItem = DefaultNotificationSkeletonItem, onNotificationClick, renderInline = false, itemHeight = 52, maxWidth = 370, maxListHeight = 527, itemsGap = 10, itemPadding = 15, skeletonItemsCount = 5, }) {
    const { buttonRef, isOpen, openNotificationsPanel, closeNotificationsPanel, toggleNotificationsPanel, 
    //
    activeView, changeActiveView, 
    //
    markNotificationAsRead, markAllNotificationsAsRead, unreadNotificationsCount, hasUnreadNotifications, activeNotifications, 
    //
    status, error, loadNextPage, hasNextPage, } = useNotificationsPanelController();
    const notificationsPanel = (_jsx(UiFocusManager, { enableFocusTrap: true, enableAutofocus: true, enableReturnFocusOnUnmount: { returnFocusTo: buttonRef }, children: _jsx(NotificationsPanel, { NotificationsPanelHeader: NotificationsPanelHeader, NotificationsList: NotificationsList, NotificationsListEmptyState: NotificationsListEmptyState, NotificationsListErrorState: NotificationsListErrorState, Notification: Notification, NotificationSkeletonItem: NotificationSkeletonItem, toggleNotificationsPanel: toggleNotificationsPanel, openNotificationsPanel: openNotificationsPanel, closeNotificationsPanel: closeNotificationsPanel, activeView: activeView, changeActiveView: changeActiveView, markNotificationAsRead: markNotificationAsRead, markAllNotificationsAsRead: markAllNotificationsAsRead, unreadNotificationsCount: unreadNotificationsCount, hasUnreadNotifications: hasUnreadNotifications, activeNotifications: activeNotifications, onNotificationClick: onNotificationClick, status: status, error: error, loadNextPage: loadNextPage, hasNextPage: hasNextPage, itemHeight: itemHeight, itemsGap: itemsGap, itemPadding: itemPadding, skeletonItemsCount: skeletonItemsCount, maxListHeight: renderInline ? undefined : maxListHeight }) }));
    if (renderInline) {
        return notificationsPanel;
    }
    invariant(NotificationsPanelButton, "If renderInline is not enabled, NotificationsPanelButton is required.");
    return (_jsxs(_Fragment, { children: [_jsx(NotificationsPanelButton, { buttonRef: buttonRef, isNotificationPanelOpen: isOpen, toggleNotificationPanel: toggleNotificationsPanel, openNotificationPanel: openNotificationsPanel, closeNotificationPanel: closeNotificationsPanel, hasUnreadNotifications: hasUnreadNotifications }), isOpen ? (_jsx(Overlay, { isModal: false, alignTo: buttonRef.current, alignPoints: ALIGN_POINTS, closeOnEscape: true, closeOnOutsideClick: true, closeOnParentScroll: false, closeOnMouseDrag: false, onClose: closeNotificationsPanel, width: "100%", maxWidth: maxWidth, children: notificationsPanel })) : null] }));
}
function useNotificationsPanelController() {
    const [isOpen, setIsOpen] = useState(false);
    const openNotificationsPanel = useCallback(() => setIsOpen(true), []);
    const closeNotificationsPanel = useCallback(() => setIsOpen(false), []);
    const toggleNotificationsPanel = useCallback(() => setIsOpen((x) => !x), []);
    const changeActiveView = useCallback((view) => setActiveView(view), []);
    const [activeView, setActiveView] = useState("unread");
    const buttonRef = useRef(null);
    const { notifications, unreadNotifications, unreadNotificationsCount, unreadNotificationsHasNextPage, unreadNotificationsLoadNextPage, unreadNotificationsStatus, unreadNotificationsError, notificationsHasNextPage, notificationsLoadNextPage, notificationsStatus, notificationsError, markAllNotificationsAsRead, markNotificationAsRead, } = useNotificationsContext();
    const activeNotifications = useMemo(() => {
        switch (activeView) {
            case "unread":
                return unreadNotifications;
            case "all":
                return notifications;
            default:
                assertNever(activeView);
                return [];
        }
    }, [activeView, unreadNotifications, notifications]);
    const status = activeView === "unread" ? unreadNotificationsStatus : notificationsStatus;
    const error = activeView === "unread" ? unreadNotificationsError : notificationsError;
    const loadNextPage = activeView === "unread" ? unreadNotificationsLoadNextPage : notificationsLoadNextPage;
    const hasNextPage = activeView === "unread" ? unreadNotificationsHasNextPage : notificationsHasNextPage;
    const hasUnreadNotifications = unreadNotificationsCount > 0;
    return {
        buttonRef,
        isOpen,
        openNotificationsPanel,
        closeNotificationsPanel,
        toggleNotificationsPanel,
        //
        activeView,
        changeActiveView,
        //
        markNotificationAsRead,
        markAllNotificationsAsRead,
        hasUnreadNotifications,
        unreadNotificationsCount,
        activeNotifications,
        //
        status,
        error,
        loadNextPage,
        hasNextPage,
    };
}
//# sourceMappingURL=NotificationsPanel.js.map