"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.MessageCenterField = void 0;
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const MessageManager_1 = require("../messages/MessageManager");
const MessageSpan_1 = require("../messages/MessageSpan");
const UiFramework_1 = require("../UiFramework");
/** Enum for the [[MessageCenterField]] active tab
 * @internal
 */
var MessageCenterActiveTab;
(function (MessageCenterActiveTab) {
    MessageCenterActiveTab[MessageCenterActiveTab["AllMessages"] = 0] = "AllMessages";
    MessageCenterActiveTab[MessageCenterActiveTab["Problems"] = 1] = "Problems";
})(MessageCenterActiveTab || (MessageCenterActiveTab = {}));
/** Message Center Field React component.
 * @public
 */
class MessageCenterField extends React.Component {
    constructor(p) {
        var _a;
        super(p);
        this._indicator = React.createRef();
        this._title = UiFramework_1.UiFramework.translate("messageCenter.messages");
        this._handleMessagesUpdatedEvent = () => {
            // istanbul ignore else
            if (this._unloadMessagesUpdatedHandler)
                this.setState({ messageCount: MessageManager_1.MessageManager.messages.length });
        };
        this._handleOpenMessageCenterEvent = () => {
            this.setOpenWidget(this._className);
        };
        this._handleTargetRef = (target) => {
            // eslint-disable-next-line deprecation/deprecation
            const ref = this.props.targetRef;
            if (typeof ref === "function")
                ref(target);
            else if (ref)
                ref.current = target;
            this.setState({ target });
        };
        this._handleClose = () => {
            this.setOpenWidget(null);
        };
        this._handleOutsideClick = (e) => {
            if (!this._indicator.current ||
                !(e.target instanceof Node) ||
                this._indicator.current.contains(e.target))
                return;
            this._handleClose();
        };
        this._handleMessageIndicatorClick = () => {
            var _a;
            // eslint-disable-next-line deprecation/deprecation
            const isOpen = ((_a = this.props.openWidget) !== null && _a !== void 0 ? _a : this.state.openWidget) === this._className;
            if (isOpen)
                this.setOpenWidget(null);
            else
                this.setOpenWidget(this._className);
        };
        this._changeActiveTab = (tab) => {
            this.setState({ activeTab: tab });
        };
        const instance = this.constructor;
        this._className = instance.name;
        this.state = {
            activeTab: MessageCenterActiveTab.AllMessages,
            target: null,
            messageCount: MessageManager_1.MessageManager.messages.length,
            // eslint-disable-next-line deprecation/deprecation
            openWidget: (_a = p.openWidget) !== null && _a !== void 0 ? _a : null,
        };
    }
    /** @internal */
    componentDidMount() {
        this._unloadMessagesUpdatedHandler = MessageManager_1.MessageManager.onMessagesUpdatedEvent.addListener(this._handleMessagesUpdatedEvent, this);
        this._removeOpenMessagesCenterHandler = MessageManager_1.MessageManager.onOpenMessageCenterEvent.addListener(this._handleOpenMessageCenterEvent, this);
        MessageManager_1.MessageManager.registerAnimateOutToElement(this._indicator.current);
    }
    /** @internal */
    componentWillUnmount() {
        // istanbul ignore else
        if (this._unloadMessagesUpdatedHandler) {
            this._unloadMessagesUpdatedHandler();
            this._unloadMessagesUpdatedHandler = undefined;
        }
        // istanbul ignore else
        if (this._removeOpenMessagesCenterHandler) {
            this._removeOpenMessagesCenterHandler();
            this._removeOpenMessagesCenterHandler = undefined;
        }
    }
    render() {
        var _a, _b, _c;
        const tooltip = `${this.state.messageCount} ${this._title}`;
        const divStyle = { ...this.props.style, height: "100%" };
        const footerMessages = (React.createElement(React.Fragment, null,
            React.createElement("div", { className: this.props.className, style: divStyle, title: tooltip, ref: this._handleTargetRef },
                React.createElement(appui_layout_react_1.MessageCenter, { indicatorRef: this._indicator, 
                    // eslint-disable-next-line deprecation/deprecation
                    isInFooterMode: (_a = this.props.isInFooterMode) !== null && _a !== void 0 ? _a : true, 
                    // eslint-disable-next-line deprecation/deprecation
                    label: ((_b = this.props.isInFooterMode) !== null && _b !== void 0 ? _b : true) ? this._title : undefined, onClick: this._handleMessageIndicatorClick }, this.state.messageCount.toString())),
            React.createElement(appui_layout_react_1.FooterPopup // eslint-disable-line deprecation/deprecation
            , { isOpen: ((_c = this.props.openWidget) !== null && _c !== void 0 ? _c : this.state.openWidget) === this._className, onClose: this._handleClose, onOutsideClick: this._handleOutsideClick, target: this.state.target },
                React.createElement(appui_layout_react_1.MessageCenterDialog, { prompt: UiFramework_1.UiFramework.translate("messageCenter.prompt"), tabs: React.createElement(React.Fragment, null,
                        React.createElement(appui_layout_react_1.MessageCenterTab, { isActive: this.state.activeTab === MessageCenterActiveTab.AllMessages, onClick: () => this._changeActiveTab(MessageCenterActiveTab.AllMessages) }, UiFramework_1.UiFramework.translate("messageCenter.all")),
                        React.createElement(appui_layout_react_1.MessageCenterTab, { isActive: this.state.activeTab === MessageCenterActiveTab.Problems, onClick: () => this._changeActiveTab(MessageCenterActiveTab.Problems) }, UiFramework_1.UiFramework.translate("messageCenter.errors"))), title: this._title }, this.getMessages()))));
        return footerMessages;
    }
    getMessages() {
        const messages = MessageManager_1.MessageManager.messages.slice(0).reverse();
        const tabRows = new Array();
        messages.forEach((details, index) => {
            /* istanbul ignore else */
            if (this.state.activeTab === MessageCenterActiveTab.AllMessages || this.isProblemStatus(details.priority)) {
                const iconClassName = MessageManager_1.MessageManager.getIconClassName(details);
                const message = details.briefMessage;
                tabRows.push(React.createElement(appui_layout_react_1.MessageCenterMessage, { key: index.toString(), icon: React.createElement("i", { className: iconClassName }) },
                    React.createElement(MessageSpan_1.MessageSpan, { message: message }),
                    details.detailedMessage &&
                        React.createElement(React.Fragment, null,
                            React.createElement("br", null),
                            React.createElement(MessageSpan_1.MessageSpan, { className: "uicore-text-small", message: details.detailedMessage }))));
            }
        });
        return tabRows;
    }
    isProblemStatus(priority) {
        // See priority values in DgnPlatform defined in NotificationManager
        if (priority === core_frontend_1.OutputMessagePriority.Error || priority === core_frontend_1.OutputMessagePriority.Fatal)
            return true;
        return false;
    }
    setOpenWidget(openWidget) {
        var _a, _b;
        // eslint-disable-next-line deprecation/deprecation
        (_b = (_a = this.props).onOpenWidget) === null || _b === void 0 ? void 0 : _b.call(_a, openWidget);
        this.setState({ openWidget });
    }
}
exports.MessageCenterField = MessageCenterField;
//# sourceMappingURL=MessageCenter.js.map