import React, { useContext, useEffect, useState, } from "react";
import { NotificationService, } from "../../../api/notifications";
import { C3UserConfigurationContext } from "../../c3-user-configuration/c3-user-configuration-provider";
export const C3NotificationContext = React.createContext({
    enabled: false,
    notifications: [],
    markAsRead: () => undefined,
    markAllAsRead: () => undefined,
    dismiss: () => undefined,
    dismissAll: () => undefined,
    analytics: () => undefined,
});
const C3NotificationProvider = ({ children, }) => {
    const [notifications, setNotifications] = useState([]);
    const [isFetched, setFetched] = useState(false);
    const [activeOrganizationId, setActiveOrganizationId] = useState("");
    const [isEventStreamAvailable, setEventStreamAvailable] = useState(false);
    const config = useContext(C3UserConfigurationContext);
    const enabled = !!config && !!config.activeOrganizationId && !!config.userToken;
    // if the organization changes, we need to reset the state
    if (enabled && config.activeOrganizationId !== activeOrganizationId) {
        setActiveOrganizationId(config.activeOrganizationId);
        setFetched(false);
        setNotifications([]);
    }
    if (enabled && !isFetched && config.userToken) {
        NotificationService.getNotifications(config).then(({ result }) => {
            if (result)
                setNotifications(result);
            setFetched(true);
        });
    }
    if (enabled && isFetched && !isEventStreamAvailable) {
        setEventStreamAvailable(true);
        NotificationService.notificationsStream(config, (notification) => {
            if (notification.orgId === activeOrganizationId) {
                setNotifications(NotificationService.updateNotifications(notifications, notification));
            }
        });
    }
    useEffect(() => {
        if (enabled && config.userToken) {
            NotificationService.getNotifications(config).then(({ result }) => {
                if (result)
                    setNotifications(result);
                setFetched(true);
            });
        }
    }, [config?.userToken]);
    const changeState = (newState, { uuid, state }) => {
        if (enabled && state !== newState) {
            setNotifications(NotificationService.updateNotificationState(notifications, uuid, newState));
            NotificationService.changeState(config, uuid, newState);
        }
    };
    const changeManyStates = (newState, notifications) => {
        if (enabled) {
            const notificationIds = notifications
                .filter((notification) => notification.state !== newState)
                .map((notification) => notification.uuid);
            if (notificationIds.length > 0) {
                setNotifications((allNotifications) => NotificationService.updateNotificationStates(allNotifications, notificationIds, newState));
                if (newState === "dismiss") {
                    NotificationService.dismissAll(config, config.activeOrganizationId);
                    return;
                }
                if (newState === "read") {
                    NotificationService.markManyAsRead(config, notificationIds);
                }
            }
        }
    };
    const markAsRead = (notification) => changeState("read", notification);
    const markAllAsRead = (notifications) => changeManyStates("read", notifications);
    const dismiss = (notification) => changeState("dismiss", notification);
    const dismissAll = (notifications) => changeManyStates("dismiss", notifications);
    const analytics = (event, id) => {
        if (enabled && config.userToken) {
            NotificationService.sendAnalyticsEvent(config, {
                event,
                id,
            });
        }
    };
    return (React.createElement(C3NotificationContext.Provider, { value: {
            enabled,
            notifications,
            markAsRead,
            markAllAsRead,
            dismiss,
            dismissAll,
            analytics,
        } }, children));
};
export function withNotifications(Component) {
    return function WithNamespace(props) {
        return (React.createElement(C3NotificationProvider, null,
            React.createElement(Component, { ...props })));
    };
}
export default C3NotificationProvider;
