/*---------------------------------------------------------------------------------------------
 * 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
 */
import { BeUiEvent, Logger } from "@itwin/core-bentley";
import { UiFramework } from "../UiFramework.js";
import { DialogManagerBase } from "./DialogManagerBase.js";
import { IModelApp, NotifyMessageDetails, OutputMessagePriority, OutputMessageType, } from "@itwin/core-frontend";
/** Modeless Dialog Manager class displays and manages multiple modeless dialogs
 * @internal
 */
export class InternalModelessDialogManager {
    /** Get the array of modeless dialogs */
    static get dialogs() {
        return InternalModelessDialogManager.dialogManager.dialogs;
    }
    /** Initialize the modeless dialog manager
     * @internal
     */
    static initialize() {
        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`;
            Logger.logInfo(UiFramework.loggerCategory("InternalModelessDialogManager"), `openDialog: ${message}`);
            IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, message, undefined, OutputMessageType.Toast));
            return;
        }
        InternalModelessDialogManager._dialogMap.set(id, {
            reactNode: dialog,
            zIndex: 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.topZIndex =
                    DialogManagerBase.getDialogZIndexDefault();
            this.update();
        }
        else {
            Logger.logError(UiFramework.loggerCategory("InternalModelessDialogManager"), `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.topZIndex += 1;
            dialogInfo.zIndex = 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.getDialogZIndexDefault();
        const dialogInfo = InternalModelessDialogManager._dialogMap.get(id);
        if (dialogInfo)
            zIndex = dialogInfo.zIndex;
        return zIndex;
    }
    static getInfo(id) {
        return InternalModelessDialogManager._dialogMap.get(id);
    }
}
/** Modeless Dialog Changed Event */
InternalModelessDialogManager.onModelessDialogChangedEvent = new BeUiEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** @internal */
InternalModelessDialogManager.dialogManager = new DialogManagerBase(InternalModelessDialogManager.onModelessDialogChangedEvent);
InternalModelessDialogManager._dialogMap = new Map();
InternalModelessDialogManager._idArray = new Array();
//# sourceMappingURL=InternalModelessDialogManager.js.map