"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.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 SafeAreaHelpers_1 = require("../safearea/SafeAreaHelpers");
const ActivityMessage_1 = require("../messages/ActivityMessage");
const UiFramework_1 = require("../UiFramework");
const Field_1 = require("./Field");
const Popup_1 = require("./Popup");
/** 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,
            });
        };
        this._closeMessage = (id) => {
            MessageManager_1.MessageManager.activeMessageManager.remove(id);
            MessageManager_1.MessageManager.updateMessages();
        };
        this.state = {
            activityMessageInfo: undefined,
        };
    }
    render() {
        return (React.createElement(SafeAreaContext_1.SafeAreaContext.Consumer, null, (safeAreaInsets) => (React.createElement(appui_layout_react_1.Footer, { className: this.props.className, messages: React.createElement(ActivityMessage_1.ActivityMessageRenderer, null), onMouseEnter: UiFramework_1.UiFramework.visibility.handleWidgetMouseEnter, safeAreaInsets: (0, SafeAreaHelpers_1.toLayoutSafeAreaInsets)(safeAreaInsets), style: this.props.style },
            this.props.widgetControl?.getReactNode?.() ?? null,
            this.props.children))));
    }
    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);
    }
}
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;
/** Components used in a [[StatusBar]].
 * @public
 */
(function (StatusBar) {
    /** Field of a [[StatusBar]].
     * @beta
     */
    StatusBar.Field = Field_1.StatusBarField;
    /** Popup of a [[StatusBar]].
     * @beta
     */
    StatusBar.Popup = Popup_1.StatusBarPopup;
})(StatusBar = exports.StatusBar || (exports.StatusBar = {}));
//# sourceMappingURL=StatusBar.js.map