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