"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.MessageRenderer = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const MessageManager_1 = require("../messages/MessageManager");
const core_frontend_1 = require("@itwin/core-frontend");
const useTranslation_1 = require("../hooks/useTranslation");
/** Hook to render an Activity message.
 * @internal
 */
function useActivityMessage({ activityMessageInfo, dismissActivityMessage, cancelActivityMessage, }) {
    const toaster = (0, itwinui_react_1.useToaster)();
    const { translate } = (0, useTranslation_1.useTranslation)();
    const recentToast = React.useRef();
    React.useEffect(() => {
        toaster.setSettings({ placement: "bottom" });
    }, [toaster]);
    React.useEffect(() => {
        if (activityMessageInfo?.restored) {
            recentToast.current = toaster.informational(React.createElement(ActivityMessageContent, { initialActivityMessageInfo: activityMessageInfo }), {
                onRemove: dismissActivityMessage,
                type: "persisting",
                link: activityMessageInfo?.details?.supportsCancellation &&
                    cancelActivityMessage
                    ? {
                        title: translate("dialog.cancel"),
                        onClick: cancelActivityMessage,
                    }
                    : undefined,
            });
        }
        if (!activityMessageInfo) {
            recentToast.current?.close();
        }
    }, [
        activityMessageInfo,
        cancelActivityMessage,
        dismissActivityMessage,
        toaster,
        translate,
    ]);
}
/** Component that renders activity, toast and sticky messages dispatched from message manager.
 * @internal
 */
function MessageRenderer() {
    const [activityMessageInfo, setActivityMessageInfo] = React.useState();
    React.useEffect(() => {
        return MessageManager_1.MessageManager.onActivityMessageUpdatedEvent.addListener((args) => {
            setActivityMessageInfo(args);
        });
    }, []);
    React.useEffect(() => {
        return MessageManager_1.MessageManager.onActivityMessageCancelledEvent.addListener(() => {
            setActivityMessageInfo(undefined);
        });
    }, []);
    const cancelActivityMessage = React.useCallback(() => {
        MessageManager_1.MessageManager.endActivityMessage(false);
    }, []);
    useActivityMessage({ activityMessageInfo, cancelActivityMessage });
    useDisplayMessage();
    return null;
}
exports.MessageRenderer = MessageRenderer;
/** Component used to show and update activity message content.
 * @internal
 */
function ActivityMessageContent({ initialActivityMessageInfo, }) {
    const { translate } = (0, useTranslation_1.useTranslation)();
    const percentCompleteLabel = translate("activityCenter.percentComplete");
    const [activityMessageInfo, setActivityMessageInfo] = React.useState(initialActivityMessageInfo);
    React.useEffect(() => {
        const handleActivityMessageUpdatedEvent = (args // eslint-disable-line deprecation/deprecation
        ) => {
            setActivityMessageInfo(args);
        };
        return MessageManager_1.MessageManager.onActivityMessageUpdatedEvent.addListener(handleActivityMessageUpdatedEvent);
    }, []);
    return (React.createElement(React.Fragment, null,
        activityMessageInfo.message && (React.createElement(itwinui_react_1.Text, null,
            React.createElement(React.Fragment, null, activityMessageInfo.message.reactNode ||
                activityMessageInfo.message))),
        !!activityMessageInfo.details?.showPercentInMessage && (React.createElement(itwinui_react_1.Text, { variant: "small" }, `${activityMessageInfo.percentage} ${percentCompleteLabel}`)),
        activityMessageInfo.details?.showProgressBar && (React.createElement(itwinui_react_1.ProgressLinear, { value: activityMessageInfo?.percentage }))));
}
function useDisplayMessage() {
    const toaster = (0, itwinui_react_1.useToaster)();
    React.useEffect(() => {
        return MessageManager_1.MessageManager.onDisplayMessage.addListener((args) => {
            const { message, options, settings, animateOutToElement } = args;
            const toastOptions = {
                hasCloseButton: true,
                duration: message.displayTime.milliseconds,
                type: message.msgType === core_frontend_1.OutputMessageType.Sticky
                    ? "persisting"
                    : "temporary",
                animateOutTo: animateOutToElement,
                ...options,
            };
            toaster.setSettings({
                placement: "bottom",
                order: "ascending",
                ...settings,
            });
            const content = (React.createElement(React.Fragment, null,
                React.createElement(core_react_1.MessageRenderer, { message: message.briefMessage }),
                message.detailedMessage && (React.createElement(itwinui_react_1.Text, { variant: "small" },
                    React.createElement(core_react_1.MessageRenderer, { message: message.detailedMessage })))));
            let toast;
            switch (message.priority) {
                case core_frontend_1.OutputMessagePriority.Warning: {
                    toast = toaster.warning(content, toastOptions);
                    break;
                }
                case core_frontend_1.OutputMessagePriority.Info: {
                    toast = toaster.informational(content, toastOptions);
                    break;
                }
                case core_frontend_1.OutputMessagePriority.Error:
                case core_frontend_1.OutputMessagePriority.Fatal: {
                    toast = toaster.negative(content, toastOptions);
                    break;
                }
                default:
                    toast = toaster.positive(content, toastOptions);
            }
            args.close = toast.close;
        });
    }, [toaster]);
}
//# sourceMappingURL=MessageRenderer.js.map