/*---------------------------------------------------------------------------------------------
 * 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 { UiCore } from "@itwin/core-react";
import { ProgressLinear, Text, toaster } from "@itwin/itwinui-react";
import { UiFramework } from "../UiFramework";
import { MessageManager } from "../messages/MessageManager";
/**
 * Hook to render an Activity message.
 * @internal
 */
function useActivityMessage({ activityMessageInfo, dismissActivityMessage, cancelActivityMessage, }) {
    const [cancelLabel] = React.useState(UiCore.translate("dialog.cancel"));
    const recentToast = React.useRef();
    React.useEffect(() => {
        toaster.setSettings({ placement: "bottom" });
    }, []);
    React.useEffect(() => {
        var _a, _b;
        if (activityMessageInfo === null || activityMessageInfo === void 0 ? void 0 : activityMessageInfo.restored) {
            recentToast.current = toaster.informational(React.createElement(ActivityMessageContent, { initialActivityMessageInfo: activityMessageInfo }), {
                onRemove: dismissActivityMessage,
                type: "persisting",
                link: ((_a = activityMessageInfo === null || activityMessageInfo === void 0 ? void 0 : activityMessageInfo.details) === null || _a === void 0 ? void 0 : _a.supportsCancellation) &&
                    cancelActivityMessage
                    ? { title: cancelLabel, onClick: cancelActivityMessage }
                    : undefined,
            });
        }
        if (!activityMessageInfo) {
            (_b = recentToast.current) === null || _b === void 0 ? void 0 : _b.close();
        }
    }, [
        activityMessageInfo,
        cancelActivityMessage,
        cancelLabel,
        dismissActivityMessage,
    ]);
}
/**
 * Component wrapping the `useActivityMessage` hook to use in class components.
 * @internal
 */
export function ActivityMessageRenderer() {
    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 });
    return React.createElement(React.Fragment, null);
}
/**
 * Component used to show and update activity message content.
 * @internal
 */
function ActivityMessageContent({ initialActivityMessageInfo, }) {
    var _a, _b;
    const [percentCompleteLabel] = React.useState(UiFramework.translate("activityCenter.percentComplete"));
    const [activityMessageInfo, setActivityMessageInfo] = React.useState(initialActivityMessageInfo);
    React.useEffect(() => {
        const handleActivityMessageUpdatedEvent = (args) => {
            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))),
        !!((_a = activityMessageInfo.details) === null || _a === void 0 ? void 0 : _a.showPercentInMessage) && (React.createElement(Text, { variant: "small" }, `${activityMessageInfo.percentage} ${percentCompleteLabel}`)),
        ((_b = activityMessageInfo.details) === null || _b === void 0 ? void 0 : _b.showProgressBar) && (React.createElement(ProgressLinear, { value: activityMessageInfo === null || activityMessageInfo === void 0 ? void 0 : activityMessageInfo.percentage }))));
}
//# sourceMappingURL=ActivityMessage.js.map