/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module StatusBar
 */
import "./StatusBar.scss";
import * as React from "react";
import { Div } from "@itwin/core-react";
import { Footer } from "@itwin/appui-layout-react";
import { MessageManager } from "../messages/MessageManager";
import { SafeAreaContext } from "../safearea/SafeAreaContext";
import { CustomActivityMessageRenderer } from "../messages/ActivityMessage";
import { UiFramework } from "../UiFramework";
/** Status Bar React component.
 * @public
 */
export class StatusBar extends React.Component {
    /** @internal */
    constructor(props) {
        super(props);
        this.messages = [];
        this._handleMessageAddedEvent = (_args) => {
            this._updateMessages();
            const messagesToAdd = MessageManager.activeMessageManager.messages.filter((msg) => !this.messages.find((m) => m.id === msg.id));
            messagesToAdd.forEach((msg) => {
                const displayedMessage = MessageManager.displayMessage(msg.messageDetails, { onRemove: () => this._closeMessage(msg.id) });
                if (!!displayedMessage)
                    this.messages.push({ close: displayedMessage.close, id: msg.id });
            });
        };
        this._updateMessages = () => {
            const updatedMessages = [...this.messages];
            this.messages.forEach((m) => {
                if (!MessageManager.activeMessageManager.messages.some((msg) => m.id === msg.id)) {
                    m.close();
                    const index = updatedMessages.findIndex((msg) => msg.id === m.id);
                    updatedMessages.splice(index, 1);
                }
            });
            this.messages = updatedMessages;
        };
        /** Respond to clearing the message list */
        this._handleMessagesUpdatedEvent = () => {
            this._updateMessages();
        };
        /**
         * Sets state of the status bar to updated values reflecting activity progress.
         * @param args  New values to set for ActivityMessage
         */
        this._handleActivityMessageUpdatedEvent = (args) => {
            this.setState({
                activityMessageInfo: args,
            });
        };
        /**
         * Hides ActivityMessage after cancellation
         */
        this._handleActivityMessageCancelledEvent = () => {
            this.setState({
                activityMessageInfo: undefined,
            });
        };
        /**
         * Ends canceled process and dismisses ActivityMessage
         */
        this._cancelActivityMessage = () => {
            MessageManager.endActivityMessage(false);
        };
        this._handleOpenWidget = (openWidget) => {
            this.setState({
                openWidget,
            });
        };
        this._closeMessage = (id) => {
            MessageManager.activeMessageManager.remove(id);
            MessageManager.updateMessages();
        };
        this._handleToastTargetRef = (toastTarget) => {
            MessageManager.registerAnimateOutToElement(toastTarget);
        };
        this.state = {
            openWidget: null,
            activityMessageInfo: undefined,
        };
    }
    render() {
        var _a, _b;
        let footerSections = null;
        const widgetControl = this.props.widgetControl;
        // istanbul ignore else
        if (widgetControl && widgetControl.getReactNode) {
            footerSections = widgetControl.getReactNode({
                // eslint-disable-next-line deprecation/deprecation
                isInFooterMode: (_a = this.props.isInFooterMode) !== null && _a !== void 0 ? _a : true,
                openWidget: this.state.openWidget,
                toastTargetRef: this._handleToastTargetRef,
                onOpenWidget: this._handleOpenWidget,
            });
        }
        return (React.createElement(StatusBarContext.Provider, { value: {
                // eslint-disable-next-line deprecation/deprecation
                isInFooterMode: (_b = this.props.isInFooterMode) !== null && _b !== void 0 ? _b : true,
                openWidget: this.state.openWidget,
                toastTargetRef: this._handleToastTargetRef,
                onOpenWidget: this._handleOpenWidget,
            } },
            React.createElement(SafeAreaContext.Consumer, null, (safeAreaInsets) => {
                var _a;
                return (React.createElement(Footer // eslint-disable-line deprecation/deprecation
                , { className: this.props.className, messages: this.getFooterMessages(), 
                    // eslint-disable-next-line deprecation/deprecation
                    isInFooterMode: (_a = this.props.isInFooterMode) !== null && _a !== void 0 ? _a : true, onMouseEnter: UiFramework.visibility.handleWidgetMouseEnter, safeAreaInsets: safeAreaInsets, style: this.props.style }, footerSections));
            })));
    }
    componentDidMount() {
        MessageManager.onMessageAddedEvent.addListener(this._handleMessageAddedEvent);
        MessageManager.onActivityMessageUpdatedEvent.addListener(this._handleActivityMessageUpdatedEvent);
        MessageManager.onActivityMessageCancelledEvent.addListener(this._handleActivityMessageCancelledEvent);
        MessageManager.onMessagesUpdatedEvent.addListener(this._handleMessagesUpdatedEvent);
        MessageManager.updateMessages();
    }
    componentWillUnmount() {
        MessageManager.onMessageAddedEvent.removeListener(this._handleMessageAddedEvent);
        MessageManager.onActivityMessageUpdatedEvent.removeListener(this._handleActivityMessageUpdatedEvent);
        MessageManager.onActivityMessageCancelledEvent.removeListener(this._handleActivityMessageCancelledEvent);
        MessageManager.onMessagesUpdatedEvent.removeListener(this._handleMessagesUpdatedEvent);
    }
    getFooterMessages() {
        return (React.createElement(CustomActivityMessageRenderer, { settings: { placement: "bottom" }, activityMessageInfo: this.state.activityMessageInfo, cancelActivityMessage: this._cancelActivityMessage }));
    }
}
/** StatusBar With Space Between Items React functional component
 * @public
 */
export function StatusBarSpaceBetween(props) {
    const { className, ...divProps } = props;
    return React.createElement(Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-space-between" });
}
/** StatusBar Left Section React functional component
 * @public
 */
export function StatusBarLeftSection(props) {
    const { className, ...divProps } = props;
    return React.createElement(Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-left" });
}
/** StatusBar Center Section React functional component
 * @public
 */
export function StatusBarCenterSection(props) {
    const { className, ...divProps } = props;
    return React.createElement(Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-center" });
}
/** StatusBar Right Section React functional component
 * @public
 */
export function StatusBarRightSection(props) {
    const { className, ...divProps } = props;
    return React.createElement(Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-right" });
}
/** Context providing values for StatusFieldProps and MessageCenterFieldProps
 *  @internal
 */
export const StatusBarContext = React.createContext({
    isInFooterMode: true,
    onOpenWidget: /* istanbul ignore next */ () => { },
    openWidget: "",
    toastTargetRef: { current: null },
});
//# sourceMappingURL=StatusBar.js.map