"use strict";
/*---------------------------------------------------------------------------------------------
* 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatusMessageRenderer = void 0;
require("./StatusMessageRenderer.scss");
const React = require("react");
const MessageManager_1 = require("./MessageManager");
const ActivityMessage_1 = require("./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.
 */
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_1.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_1.MessageManager.activeMessageManager.messages.filter((msg) => !messages.current.find((m) => m.id === msg.id));
            messagesToAdd.forEach((msg) => {
                const displayedMessage = MessageManager_1.MessageManager.displayMessage(message, { onRemove: () => onRemove(msg.id) }, { placement: "top", order: "descending" });
                if (!!displayedMessage)
                    messages.current.push({ close: displayedMessage.close, id: msg.id });
            });
        };
        return MessageManager_1.MessageManager.onMessageAddedEvent.addListener(handleMessageAddedEvent);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    React.useEffect(() => {
        /** Respond to clearing the message list */
        return MessageManager_1.MessageManager.onMessagesUpdatedEvent.addListener(updateMessages);
    }, []);
    React.useEffect(() => {
        const handleActivityMessageUpdatedEvent = (args) => {
            setActivityMessageInfo(args);
        };
        return MessageManager_1.MessageManager.onActivityMessageUpdatedEvent.addListener(handleActivityMessageUpdatedEvent);
    }, []);
    React.useEffect(() => {
        const handleActivityMessageCancelledEvent = () => {
            setActivityMessageInfo(undefined);
        };
        return MessageManager_1.MessageManager.onActivityMessageCancelledEvent.addListener(handleActivityMessageCancelledEvent);
    }, []);
    const onRemove = React.useCallback((id) => {
        MessageManager_1.MessageManager.activeMessageManager.remove(id);
        MessageManager_1.MessageManager.updateMessages();
        closeMessage === null || closeMessage === void 0 ? void 0 : closeMessage(id);
    }, [closeMessage]);
    const cancelActivityMessage = React.useCallback(() => {
        MessageManager_1.MessageManager.endActivityMessage(false);
        cancelActivityMessageProp && cancelActivityMessageProp();
    }, [cancelActivityMessageProp]);
    (0, ActivityMessage_1.useActivityMessage)({ activityMessageInfo, cancelActivityMessage, dismissActivityMessage });
    return React.createElement(React.Fragment, null);
}
exports.StatusMessageRenderer = StatusMessageRenderer;
//# sourceMappingURL=StatusMessageRenderer.js.map