/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Notification
 */
import "./StatusMessageRenderer.scss";
import * as React from "react";
import { MessageManager } from "./MessageManager";
import { useActivityMessage } from "./ActivityMessage";
/** Message Popup React component that renders one or more Toast or Sticky messages and an Activity message without a StatusBar.
 * @note This component was formerly named MessageRenderer in previous releases.
 * @public
 * @deprecated in 3.3. Use `toaster` from iTwinui-react to display status messages.
 */
export function StatusMessageRenderer({ closeMessage, cancelActivityMessage: cancelActivityMessageProp, dismissActivityMessage, }) {
    const messages = React.useRef([]);
    const [activityMessageInfo, setActivityMessageInfo] = React.useState(undefined);
    const updateMessages = () => {
        const updatedMessages = [...messages.current];
        messages.current.forEach((m) => {
            if (!MessageManager.activeMessageManager.messages.some((msg) => m.id === msg.id)) {
                m.close();
                const index = updatedMessages.findIndex((msg) => msg.id === m.id);
                updatedMessages.splice(index, 1);
            }
        });
        messages.current = updatedMessages;
    };
    React.useEffect(() => {
        const handleMessageAddedEvent = ({ message }) => {
            updateMessages();
            const messagesToAdd = MessageManager.activeMessageManager.messages.filter((msg) => !messages.current.find((m) => m.id === msg.id));
            messagesToAdd.forEach((msg) => {
                const displayedMessage = MessageManager.displayMessage(message, { onRemove: () => onRemove(msg.id) }, { placement: "top", order: "descending" });
                if (!!displayedMessage)
                    messages.current.push({ close: displayedMessage.close, id: msg.id });
            });
        };
        return MessageManager.onMessageAddedEvent.addListener(handleMessageAddedEvent);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    React.useEffect(() => {
        /** Respond to clearing the message list */
        return MessageManager.onMessagesUpdatedEvent.addListener(updateMessages);
    }, []);
    React.useEffect(() => {
        const handleActivityMessageUpdatedEvent = (args) => {
            setActivityMessageInfo(args);
        };
        return MessageManager.onActivityMessageUpdatedEvent.addListener(handleActivityMessageUpdatedEvent);
    }, []);
    React.useEffect(() => {
        const handleActivityMessageCancelledEvent = () => {
            setActivityMessageInfo(undefined);
        };
        return MessageManager.onActivityMessageCancelledEvent.addListener(handleActivityMessageCancelledEvent);
    }, []);
    const onRemove = React.useCallback((id) => {
        MessageManager.activeMessageManager.remove(id);
        MessageManager.updateMessages();
        closeMessage === null || closeMessage === void 0 ? void 0 : closeMessage(id);
    }, [closeMessage]);
    const cancelActivityMessage = React.useCallback(() => {
        MessageManager.endActivityMessage(false);
        cancelActivityMessageProp && cancelActivityMessageProp();
    }, [cancelActivityMessageProp]);
    useActivityMessage({ activityMessageInfo, cancelActivityMessage, dismissActivityMessage });
    return React.createElement(React.Fragment, null);
}
//# sourceMappingURL=StatusMessageRenderer.js.map