/*---------------------------------------------------------------------------------------------
 * 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 * as React from "react";
import { MessageRenderer as CoreMessageRenderer } from "@itwin/core-react";
import { ProgressLinear, Text, useToaster } from "@itwin/itwinui-react";
import { MessageManager } from "../messages/MessageManager";
import { OutputMessagePriority, OutputMessageType } from "@itwin/core-frontend";
import { useTranslation } from "../hooks/useTranslation";
/** Hook to render an Activity message.
 * @internal
 */
function useActivityMessage({ activityMessageInfo, dismissActivityMessage, cancelActivityMessage, }) {
    const toaster = useToaster();
    const { translate } = 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
 */
export function MessageRenderer() {
    const [activityMessageInfo, setActivityMessageInfo] = React.useState();
    React.useEffect(() => {
        return MessageManager.onActivityMessageUpdatedEvent.addListener((args) => {
            setActivityMessageInfo(args);
        });
    }, []);
    React.useEffect(() => {
        return MessageManager.onActivityMessageCancelledEvent.addListener(() => {
            setActivityMessageInfo(undefined);
        });
    }, []);
    const cancelActivityMessage = React.useCallback(() => {
        MessageManager.endActivityMessage(false);
    }, []);
    useActivityMessage({ activityMessageInfo, cancelActivityMessage });
    useDisplayMessage();
    return null;
}
/** Component used to show and update activity message content.
 * @internal
 */
function ActivityMessageContent({ initialActivityMessageInfo, }) {
    const { translate } = 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.onActivityMessageUpdatedEvent.addListener(handleActivityMessageUpdatedEvent);
    }, []);
    return (React.createElement(React.Fragment, null,
        activityMessageInfo.message && (React.createElement(Text, null,
            React.createElement(React.Fragment, null, activityMessageInfo.message.reactNode ||
                activityMessageInfo.message))),
        !!activityMessageInfo.details?.showPercentInMessage && (React.createElement(Text, { variant: "small" }, `${activityMessageInfo.percentage} ${percentCompleteLabel}`)),
        activityMessageInfo.details?.showProgressBar && (React.createElement(ProgressLinear, { value: activityMessageInfo?.percentage }))));
}
function useDisplayMessage() {
    const toaster = useToaster();
    React.useEffect(() => {
        return MessageManager.onDisplayMessage.addListener((args) => {
            const { message, options, settings, animateOutToElement } = args;
            const toastOptions = {
                hasCloseButton: true,
                duration: message.displayTime.milliseconds,
                type: message.msgType === OutputMessageType.Sticky
                    ? "persisting"
                    : "temporary",
                animateOutTo: animateOutToElement,
                ...options,
            };
            toaster.setSettings({
                placement: "bottom",
                order: "ascending",
                ...settings,
            });
            const content = (React.createElement(React.Fragment, null,
                React.createElement(CoreMessageRenderer, { message: message.briefMessage }),
                message.detailedMessage && (React.createElement(Text, { variant: "small" },
                    React.createElement(CoreMessageRenderer, { message: message.detailedMessage })))));
            let toast;
            switch (message.priority) {
                case OutputMessagePriority.Warning: {
                    toast = toaster.warning(content, toastOptions);
                    break;
                }
                case OutputMessagePriority.Info: {
                    toast = toaster.informational(content, toastOptions);
                    break;
                }
                case OutputMessagePriority.Error:
                case OutputMessagePriority.Fatal: {
                    toast = toaster.negative(content, toastOptions);
                    break;
                }
                default:
                    toast = toaster.positive(content, toastOptions);
            }
            args.close = toast.close;
        });
    }, [toaster]);
}
//# sourceMappingURL=MessageRenderer.js.map