"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.StatusMessageManager = void 0;
const MessageManager_1 = require("./MessageManager");
const core_frontend_1 = require("@itwin/core-frontend");
/** Manager for Status messages
 * @internal
 */
class StatusMessageManager {
    constructor() {
        this._messages = [];
        this._messageId = 0;
    }
    initialize() {
        this._messages = [];
        this._messageId = 0;
    }
    get messages() {
        return this._messages;
    }
    add(messageDetails) {
        const id = this._messageId.toString();
        const severity = MessageManager_1.MessageManager.getSeverity(messageDetails);
        const messages = this._messages.slice();
        messages.splice(0, 0, { id, messageDetails, severity }); // Insert at beginning
        this._messages = messages;
        this._messageId++;
        /** Remove Sticky messages beyond the max displayed */
        const stickyMessages = this._messages.filter((message) => message.messageDetails.msgType === core_frontend_1.OutputMessageType.Sticky);
        if (stickyMessages.length > MessageManager_1.MessageManager.maxDisplayedStickyMessages) {
            const removeMessages = stickyMessages.slice(MessageManager_1.MessageManager.maxDisplayedStickyMessages);
            for (const removeMessage of removeMessages) {
                this.remove(removeMessage.id);
            }
        }
    }
    remove(id) {
        let result = false;
        const foundIndex = this._messages.findIndex((message) => message.id === id);
        if (foundIndex >= 0) {
            const messages = this._messages.slice();
            messages.splice(foundIndex, 1);
            this._messages = messages;
            result = true;
        }
        return result;
    }
}
exports.StatusMessageManager = StatusMessageManager;
//# sourceMappingURL=StatusMessageManager.js.map