"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.InternalContentDialogManager = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const UiFramework_1 = require("../UiFramework");
const DialogManagerBase_1 = require("./DialogManagerBase");
const core_frontend_1 = require("@itwin/core-frontend");
const FrameworkContent_1 = require("../framework/FrameworkContent");
// cSpell:ignore ZINDEX modeless
/** Used if the 'dialog' z-index CSS variable cannot be read */
const CONTENT_DIALOG_ZINDEX_DEFAULT = 2000;
/** Content Dialog Manager class displays and manages multiple modeless dialogs
 * @internal
 */
class InternalContentDialogManager {
    /** Get the array of modeless dialogs */
    static get dialogs() {
        return InternalContentDialogManager.dialogManager.dialogs;
    }
    /** Initialize the modeless dialog manager.
     * @internal
     */
    static initialize() {
        InternalContentDialogManager._topZIndex =
            InternalContentDialogManager.getDialogZIndexDefault();
    }
    static getDialogZIndexDefault() {
        const variable = "--uicore-z-index-view-content-dialog";
        const value = (0, core_react_1.getCssVariableAsNumber)(variable);
        // istanbul ignore next
        if (!isNaN(value))
            return value;
        core_bentley_1.Logger.logError(UiFramework_1.UiFramework.loggerCategory(this), `'${variable}' CSS variable not found`);
        return CONTENT_DIALOG_ZINDEX_DEFAULT;
    }
    /** 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 = InternalContentDialogManager._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;
        }
        InternalContentDialogManager._dialogMap.set(id, {
            reactNode: dialog,
            zIndex: ++InternalContentDialogManager._topZIndex,
            parentDocument,
        });
        InternalContentDialogManager._idArray.push(id);
        InternalContentDialogManager.dialogManager.openDialog(dialog, id, parentDocument);
    }
    /** Close a modeless dialog
     * @param id The id of the Dialog to close.
     */
    static close(id) {
        const dialogInfo = InternalContentDialogManager._dialogMap.get(id);
        if (dialogInfo) {
            InternalContentDialogManager.dialogManager.removeDialog(dialogInfo.reactNode);
            InternalContentDialogManager._dialogMap.delete(id);
            const index = InternalContentDialogManager._idArray.indexOf(id);
            // istanbul ignore else
            if (index >= 0)
                InternalContentDialogManager._idArray.splice(index, 1);
            if (InternalContentDialogManager.active === undefined)
                InternalContentDialogManager._topZIndex =
                    InternalContentDialogManager.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() {
        InternalContentDialogManager.dialogManager.closeAll();
        InternalContentDialogManager._dialogMap.clear();
        InternalContentDialogManager._idArray = [];
        InternalContentDialogManager._topZIndex =
            InternalContentDialogManager.getDialogZIndexDefault();
    }
    /** Update the dialogs */
    static update() {
        InternalContentDialogManager.dialogManager.update();
    }
    /** Get the active modeless dialog */
    static get active() {
        if (InternalContentDialogManager._idArray.length > 0) {
            const id = InternalContentDialogManager._idArray[InternalContentDialogManager._idArray.length - 1];
            const dialogInfo = InternalContentDialogManager._dialogMap.get(id);
            // istanbul ignore else
            if (dialogInfo)
                return dialogInfo.reactNode;
        }
        return undefined;
    }
    /** Get the count of modeless dialogs */
    static get count() {
        return InternalContentDialogManager.dialogManager.dialogCount;
    }
    /** Handle a pointer down event on a modeless dialog */
    static handlePointerDownEvent(_event, id, updateFunc) {
        const dialogInfo = InternalContentDialogManager._dialogMap.get(id);
        if (dialogInfo &&
            dialogInfo.reactNode !== InternalContentDialogManager.active) {
            dialogInfo.zIndex = ++InternalContentDialogManager._topZIndex;
            InternalContentDialogManager._idArray.splice(InternalContentDialogManager._idArray.indexOf(id), 1);
            InternalContentDialogManager._idArray.push(id);
            updateFunc();
            this.update();
        }
    }
    /** Get the z-index for a modeless dialog */
    static getZIndex(id) {
        let zIndex = InternalContentDialogManager.getDialogZIndexDefault();
        const dialogInfo = InternalContentDialogManager._dialogMap.get(id);
        // istanbul ignore else
        if (dialogInfo)
            zIndex = dialogInfo.zIndex;
        return zIndex;
    }
    static getInfo(id) {
        return InternalContentDialogManager._dialogMap.get(id);
    }
}
/** Content Dialog Changed Event */
InternalContentDialogManager.onContentDialogChangedEvent = new FrameworkContent_1.ContentDialogChangedEvent();
/** @internal */
InternalContentDialogManager.dialogManager = new DialogManagerBase_1.DialogManagerBase(InternalContentDialogManager.onContentDialogChangedEvent);
InternalContentDialogManager._dialogMap = new Map();
InternalContentDialogManager._idArray = new Array();
InternalContentDialogManager._topZIndex = CONTENT_DIALOG_ZINDEX_DEFAULT;
exports.InternalContentDialogManager = InternalContentDialogManager;
//# sourceMappingURL=InternalContentDialogManager.js.map