"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.CustomActivityMessageContent = exports.CustomActivityMessageRenderer = exports.useActivityMessage = exports.ActivityMessage = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const itwinui_react_1 = require("@itwin/itwinui-react");
const UiFramework_1 = require("../UiFramework");
const MessageManager_1 = require("../messages/MessageManager");
const MessageLabel_1 = require("./MessageLabel");
const HollowIcon_1 = require("./HollowIcon");
/** Activity Message React component
 * @public
 * @deprecated in 3.3. Replaced by Toaster from @itwin/itwinui-react.
 */
function ActivityMessage(props) {
    const messageDetails = props.activityMessageInfo.details;
    const [percentCompleteLabel] = React.useState(UiFramework_1.UiFramework.translate("activityCenter.percentComplete"));
    const [cancelLabel] = React.useState(core_react_1.UiCore.translate("dialog.cancel"));
    return (React.createElement(appui_layout_react_1.Message // eslint-disable-line deprecation/deprecation
    , { status: appui_layout_react_1.Status.Information, icon: React.createElement(HollowIcon_1.HollowIcon, { iconSpec: "icon-info-hollow" }) },
        React.createElement(appui_layout_react_1.MessageLayout, { buttons: (messageDetails && messageDetails.supportsCancellation) ? (React.createElement("div", null,
                React.createElement(appui_layout_react_1.MessageHyperlink, { onClick: props.cancelActivityMessage }, cancelLabel),
                React.createElement("span", null, "\u00A0"),
                React.createElement(appui_layout_react_1.MessageButton, { onClick: props.dismissActivityMessage },
                    React.createElement(core_react_1.Icon, { iconSpec: "icon-close" })))) : (React.createElement(appui_layout_react_1.MessageButton, { onClick: props.dismissActivityMessage },
                React.createElement(core_react_1.Icon, { iconSpec: "icon-close" }))), progress: (messageDetails && messageDetails.showProgressBar) &&
                React.createElement(appui_layout_react_1.MessageProgress, { status: appui_layout_react_1.Status.Information, progress: props.activityMessageInfo.percentage }) },
            React.createElement("div", null,
                React.createElement(MessageLabel_1.MessageLabel, { message: props.activityMessageInfo.message, className: "uifw-statusbar-message-brief" }),
                (messageDetails && messageDetails.showPercentInMessage) &&
                    React.createElement(itwinui_react_1.Small, null, props.activityMessageInfo.percentage + percentCompleteLabel)))));
}
exports.ActivityMessage = ActivityMessage;
/**
 * Hook to render an Activity message.
 * @internal
 */
function useActivityMessage({ activityMessageInfo, dismissActivityMessage, cancelActivityMessage, settings }) {
    const [cancelLabel] = React.useState(core_react_1.UiCore.translate("dialog.cancel"));
    const recentToast = React.useRef();
    React.useEffect(() => {
        itwinui_react_1.toaster.setSettings(settings !== null && settings !== void 0 ? settings : { placement: "top" });
    }, [settings]);
    React.useEffect(() => {
        var _a, _b;
        if (activityMessageInfo === null || activityMessageInfo === void 0 ? void 0 : activityMessageInfo.restored) {
            recentToast.current = itwinui_react_1.toaster.informational(React.createElement(CustomActivityMessageContent, { 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]);
}
exports.useActivityMessage = useActivityMessage;
/**
 * Component wrapping the `useActivityMessage` hook to use in class components.
 * @internal
 */
function CustomActivityMessageRenderer({ activityMessageInfo, dismissActivityMessage, cancelActivityMessage, settings }) {
    useActivityMessage({ activityMessageInfo, cancelActivityMessage, dismissActivityMessage, settings });
    return React.createElement(React.Fragment, null);
}
exports.CustomActivityMessageRenderer = CustomActivityMessageRenderer;
/**
 * Component used to show and update activity message content.
 * @internal
 */
function CustomActivityMessageContent({ 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, activityMessageInfo.message),
        !!((_a = activityMessageInfo.details) === null || _a === void 0 ? void 0 : _a.showPercentInMessage) &&
            React.createElement(itwinui_react_1.Small, null, `${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 })));
}
exports.CustomActivityMessageContent = CustomActivityMessageContent;
//# sourceMappingURL=ActivityMessage.js.map