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