"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 StatusBar
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatusBarContext = exports.StatusBarRightSection = exports.StatusBarCenterSection = exports.StatusBarLeftSection = exports.StatusBarSpaceBetween = exports.StatusBar = void 0;
require("./StatusBar.scss");
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const MessageManager_1 = require("../messages/MessageManager");
const SafeAreaContext_1 = require("../safearea/SafeAreaContext");
const ActivityMessage_1 = require("../messages/ActivityMessage");
const UiFramework_1 = require("../UiFramework");
/** Status Bar React component.
 * @public
 */
class StatusBar extends React.Component {
    /** @internal */
    constructor(props) {
        super(props);
        this.messages = [];
        this._handleMessageAddedEvent = (_args) => {
            this._updateMessages();
            const messagesToAdd = MessageManager_1.MessageManager.activeMessageManager.messages.filter((msg) => !this.messages.find((m) => m.id === msg.id));
            messagesToAdd.forEach((msg) => {
                const displayedMessage = MessageManager_1.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_1.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_1.MessageManager.endActivityMessage(false);
        };
        this._handleOpenWidget = (openWidget) => {
            this.setState({
                openWidget,
            });
        };
        this._closeMessage = (id) => {
            MessageManager_1.MessageManager.activeMessageManager.remove(id);
            MessageManager_1.MessageManager.updateMessages();
        };
        this._handleToastTargetRef = (toastTarget) => {
            MessageManager_1.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(exports.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_1.SafeAreaContext.Consumer, null, (safeAreaInsets) => {
                var _a;
                return (React.createElement(appui_layout_react_1.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_1.UiFramework.visibility.handleWidgetMouseEnter, safeAreaInsets: safeAreaInsets, style: this.props.style }, footerSections));
            })));
    }
    componentDidMount() {
        MessageManager_1.MessageManager.onMessageAddedEvent.addListener(this._handleMessageAddedEvent);
        MessageManager_1.MessageManager.onActivityMessageUpdatedEvent.addListener(this._handleActivityMessageUpdatedEvent);
        MessageManager_1.MessageManager.onActivityMessageCancelledEvent.addListener(this._handleActivityMessageCancelledEvent);
        MessageManager_1.MessageManager.onMessagesUpdatedEvent.addListener(this._handleMessagesUpdatedEvent);
        MessageManager_1.MessageManager.updateMessages();
    }
    componentWillUnmount() {
        MessageManager_1.MessageManager.onMessageAddedEvent.removeListener(this._handleMessageAddedEvent);
        MessageManager_1.MessageManager.onActivityMessageUpdatedEvent.removeListener(this._handleActivityMessageUpdatedEvent);
        MessageManager_1.MessageManager.onActivityMessageCancelledEvent.removeListener(this._handleActivityMessageCancelledEvent);
        MessageManager_1.MessageManager.onMessagesUpdatedEvent.removeListener(this._handleMessagesUpdatedEvent);
    }
    getFooterMessages() {
        return (React.createElement(ActivityMessage_1.CustomActivityMessageRenderer, { settings: { placement: "bottom" }, activityMessageInfo: this.state.activityMessageInfo, cancelActivityMessage: this._cancelActivityMessage }));
    }
}
exports.StatusBar = StatusBar;
/** StatusBar With Space Between Items React functional component
 * @public
 */
function StatusBarSpaceBetween(props) {
    const { className, ...divProps } = props;
    return React.createElement(core_react_1.Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-space-between" });
}
exports.StatusBarSpaceBetween = StatusBarSpaceBetween;
/** StatusBar Left Section React functional component
 * @public
 */
function StatusBarLeftSection(props) {
    const { className, ...divProps } = props;
    return React.createElement(core_react_1.Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-left" });
}
exports.StatusBarLeftSection = StatusBarLeftSection;
/** StatusBar Center Section React functional component
 * @public
 */
function StatusBarCenterSection(props) {
    const { className, ...divProps } = props;
    return React.createElement(core_react_1.Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-center" });
}
exports.StatusBarCenterSection = StatusBarCenterSection;
/** StatusBar Right Section React functional component
 * @public
 */
function StatusBarRightSection(props) {
    const { className, ...divProps } = props;
    return React.createElement(core_react_1.Div, { ...divProps, mainClassName: className ? className : "uifw-statusbar-right" });
}
exports.StatusBarRightSection = StatusBarRightSection;
/** Context providing values for StatusFieldProps and MessageCenterFieldProps
 *  @internal
 */
exports.StatusBarContext = React.createContext({
    isInFooterMode: true,
    onOpenWidget: /* istanbul ignore next */ () => { },
    openWidget: "",
    toastTargetRef: { current: null },
});
//# sourceMappingURL=StatusBar.js.map