"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 Dialog
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalModelessDialogManager = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const UiFramework_1 = require("../UiFramework");
const DialogManagerBase_1 = require("./DialogManagerBase");
const core_frontend_1 = require("@itwin/core-frontend");
// cSpell:ignore ZINDEX modeless
/** Modeless Dialog Manager class displays and manages multiple modeless dialogs
 * @internal
 */
class InternalModelessDialogManager {
    /** Get the array of modeless dialogs */
    static get dialogs() {
        return InternalModelessDialogManager.dialogManager.dialogs;
    }
    /** Initialize the modeless dialog manager
     * @internal
     */
    static initialize() {
        DialogManagerBase_1.DialogManagerBase.initialize();
    }
    /** Open a modeless dialog
     * @param dialog The Dialog to open
     * @param id The id of the Dialog to open
     */
    static open(dialog, id, parentDocument = document) {
        const dialogInfo = InternalModelessDialogManager._dialogMap.get(id);
        if (dialogInfo) {
            const message = `Dialog with id of '${id}' already opened`;
            core_bentley_1.Logger.logInfo(UiFramework_1.UiFramework.loggerCategory(this), `openDialog: ${message}`);
            core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, message, undefined, core_frontend_1.OutputMessageType.Toast));
            return;
        }
        InternalModelessDialogManager._dialogMap.set(id, {
            reactNode: dialog,
            zIndex: DialogManagerBase_1.DialogManagerBase.topZIndex,
            parentDocument,
        });
        InternalModelessDialogManager._idArray.push(id);
        InternalModelessDialogManager.dialogManager.openDialog(dialog, id, parentDocument);
    }
    /** Close a modeless dialog
     * @param id The id of the Dialog to close.
     */
    static close(id) {
        const dialogInfo = InternalModelessDialogManager._dialogMap.get(id);
        if (dialogInfo) {
            InternalModelessDialogManager.dialogManager.removeDialog(dialogInfo.reactNode);
            InternalModelessDialogManager._dialogMap.delete(id);
            const index = InternalModelessDialogManager._idArray.indexOf(id);
            if (index >= 0)
                InternalModelessDialogManager._idArray.splice(index, 1);
            if (InternalModelessDialogManager.active === undefined)
                DialogManagerBase_1.DialogManagerBase.topZIndex =
                    DialogManagerBase_1.DialogManagerBase.getDialogZIndexDefault();
            this.update();
        }
        else {
            core_bentley_1.Logger.logError(UiFramework_1.UiFramework.loggerCategory(this), `closeDialog: Could not find dialog with id of '${id}'`);
        }
    }
    /** @internal */
    static closeAll() {
        InternalModelessDialogManager.dialogManager.closeAll();
        InternalModelessDialogManager._dialogMap.clear();
        InternalModelessDialogManager._idArray = [];
    }
    /** Update the dialogs */
    static update() {
        InternalModelessDialogManager.dialogManager.update();
    }
    /** Get the active modeless dialog */
    static get active() {
        if (InternalModelessDialogManager._idArray.length > 0) {
            const id = InternalModelessDialogManager._idArray[InternalModelessDialogManager._idArray.length - 1];
            const dialogInfo = InternalModelessDialogManager._dialogMap.get(id);
            if (dialogInfo)
                return dialogInfo.reactNode;
        }
        return undefined;
    }
    /** Get the count of modeless dialogs */
    static get count() {
        return InternalModelessDialogManager.dialogManager.dialogCount;
    }
    /** Handle a pointer down event on a modeless dialog */
    static handlePointerDownEvent(_event, id, updateFunc) {
        const dialogInfo = InternalModelessDialogManager._dialogMap.get(id);
        if (dialogInfo &&
            dialogInfo.reactNode !== InternalModelessDialogManager.active) {
            DialogManagerBase_1.DialogManagerBase.topZIndex += 1;
            dialogInfo.zIndex = DialogManagerBase_1.DialogManagerBase.topZIndex;
            InternalModelessDialogManager._idArray.splice(InternalModelessDialogManager._idArray.indexOf(id), 1);
            InternalModelessDialogManager._idArray.push(id);
            updateFunc();
            this.update();
        }
    }
    /** Get the z-index for a modeless dialog */
    static getZIndex(id) {
        let zIndex = DialogManagerBase_1.DialogManagerBase.getDialogZIndexDefault();
        const dialogInfo = InternalModelessDialogManager._dialogMap.get(id);
        if (dialogInfo)
            zIndex = dialogInfo.zIndex;
        return zIndex;
    }
    static getInfo(id) {
        return InternalModelessDialogManager._dialogMap.get(id);
    }
}
exports.InternalModelessDialogManager = InternalModelessDialogManager;
/** Modeless Dialog Changed Event */
InternalModelessDialogManager.onModelessDialogChangedEvent = new core_bentley_1.BeUiEvent(); // eslint-disable-line deprecation/deprecation
/** @internal */
InternalModelessDialogManager.dialogManager = new DialogManagerBase_1.DialogManagerBase(InternalModelessDialogManager.onModelessDialogChangedEvent);
InternalModelessDialogManager._dialogMap = new Map();
InternalModelessDialogManager._idArray = new Array();
//# sourceMappingURL=InternalModelessDialogManager.js.map