/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ContentView
 */
import { ViewUtilities } from "../utils/ViewUtilities.js";
import { InternalContentLayoutManager } from "./InternalContentLayoutManager.js";
import { IModelApp } from "@itwin/core-frontend";
import { BeUiEvent, Logger } from "@itwin/core-bentley";
import { UiFramework } from "../UiFramework.js";
import { InternalContentDialogManager } from "../dialog/InternalContentDialogManager.js";
/** Content View Manager class.
 * @internal
 */
export class InternalContentViewManager {
    static _mouseDown = false;
    static _activeContent;
    /** Gets the [[MouseDownChangedEvent]] */
    static onMouseDownChangedEvent = new BeUiEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
    /** Determines if the mouse is down in a content view */
    static get isMouseDown() {
        return this._mouseDown;
    }
    /** Sets the mouse down status for a content view */
    static setMouseDown(mouseDown) {
        this._mouseDown = mouseDown;
        this.onMouseDownChangedEvent.emit({ mouseDown });
    }
    /** Gets the [[ActiveContentChangedEvent]] */
    static onActiveContentChangedEvent = new BeUiEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
    /** Fires when floating contents are added or removed */
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    static onAvailableContentChangedEvent = new BeUiEvent();
    /** Gets the active content as a React.ReactNode. */
    static getActive() {
        return this._activeContent;
    }
    static getControlFromElement(content, activeContentGroup, 
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    floatingControls, logIfNotFound = false) {
        if (floatingControls?.length) {
            // if we find a React node that matches exactly, return its containing control
            let control = floatingControls.find((contentControl) => contentControl.reactNode === content);
            if (control)
                return control;
            // if we don't find a React node that matches exactly, rely on the id specified by the creator
            let controlId;
            if (content && content.key) {
                const key = content.key;
                // key has format `${contentProps.id}::${this.groupId}` which is stored as unique id
                controlId = key.split("::", 1)[0];
            }
            floatingControls.forEach((contentControl) => {
                const node = contentControl.reactNode;
                const key = node?.key;
                const nodeId = key && key.split("::", 1)[0];
                if (nodeId === controlId)
                    control = contentControl;
            });
            if (control)
                return control;
        }
        // if it's not a floating control, look through the content area views
        if (activeContentGroup) {
            const activeContentControl = 
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            activeContentGroup.getControlFromElement(content);
            if (activeContentControl)
                return activeContentControl;
        }
        if (logIfNotFound)
            Logger.logError(UiFramework.loggerCategory("InternalContentViewManager"), `getControlFromElement: no control found for element`);
        return undefined;
    }
    /** Return the active ContentControl. */
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    static getActiveContentControl() {
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        let activeContentControl;
        const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (this._activeContent && activeFrontstageDef) {
            const activeContentGroup = activeFrontstageDef.contentGroup;
            activeContentControl = this.getControlFromElement(this._activeContent, activeContentGroup, 
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            activeFrontstageDef.floatingContentControls);
        }
        return activeContentControl;
    }
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    static addFloatingContentControl(contentControl) {
        const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (activeFrontstageDef && contentControl) {
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            activeFrontstageDef.addFloatingContentControl(contentControl);
        }
    }
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    static dropFloatingContentControl(contentControl) {
        const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (activeFrontstageDef && contentControl)
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            activeFrontstageDef.dropFloatingContentControl(contentControl);
    }
    /** Sets the active [[ContentControl]]. */
    static setActive(activeContent, forceEventProcessing = false) {
        if (this._activeContent !== activeContent || forceEventProcessing) {
            const oldContent = this._activeContent;
            this._activeContent = activeContent;
            const frontstageDef = UiFramework.frontstages.activeFrontstageDef;
            if (!frontstageDef)
                return;
            const contentGroup = frontstageDef.contentGroup;
            if (contentGroup) {
                const reactContent = contentGroup.contentPropsList.find((contentProps) => contentProps.content === activeContent);
                if (reactContent) {
                    this.onActiveContentChangedEvent.emit({
                        id: reactContent.id,
                        activeContent: reactContent.content,
                    });
                    return;
                }
            }
            const oldContentControl = this.getControlFromElement(oldContent, contentGroup, 
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            frontstageDef.floatingContentControls);
            const activeContentControl = this.getControlFromElement(activeContent, contentGroup, 
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            frontstageDef.floatingContentControls, true);
            // Only call setActiveView if going to or coming from a non-viewport ContentControl
            if (!activeContentControl) {
                this.onActiveContentChangedEvent.emit({
                    activeContent,
                    oldContent,
                    id: undefined,
                });
                return;
            }
            const doSetActiveView = forceEventProcessing ||
                !activeContentControl.viewport ||
                (oldContentControl && !oldContentControl.viewport);
            if (doSetActiveView) {
                // eslint-disable-next-line @typescript-eslint/no-deprecated
                frontstageDef.setActiveView(activeContentControl, oldContentControl);
                this.onActiveContentChangedEvent.emit({
                    id: activeContentControl.controlId,
                    activeContent,
                    oldContent,
                });
            }
            else {
                if (activeContentControl.viewport &&
                    activeContentControl.viewport !== IModelApp.viewManager.selectedView) {
                    void IModelApp.viewManager.setSelectedView(activeContentControl.viewport);
                }
            }
        }
    }
    static setActiveId(contentId) {
        if (!contentId) {
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            this.setActive(contentId);
            return;
        }
        const frontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (!frontstageDef)
            return;
        const contentGroup = frontstageDef.contentGroup;
        if (!contentGroup)
            return;
        const contentIndex = contentGroup.contentPropsList.findIndex((content) => content.id === contentId);
        if (contentIndex < 0)
            return;
        const contentProps = contentGroup.contentPropsList[contentIndex];
        if (contentProps.content) {
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            this.setActive(contentProps.content);
            return;
        }
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        const control = contentGroup.getContentControl(contentProps, contentIndex);
        if (!control)
            return;
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        this.setActive(control.reactNode);
    }
    static getActiveId() {
        const activeContentControl = this.getActiveContentControl();
        if (activeContentControl) {
            return activeContentControl.controlId;
        }
        const frontstageDef = UiFramework.frontstages.activeFrontstageDef;
        const contentGroup = frontstageDef?.contentGroup;
        const activeContent = contentGroup?.contentPropsList.find((contentProps) => {
            return contentProps.content === this.getActive();
        });
        return activeContent?.id;
    }
    /** Refreshes the active [[ContentControl]]. */
    static refreshActive(activeContent) {
        this.layouts.refreshActive();
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        this.setActive(activeContent, true);
    }
    /**
     * Determines if content displays a Sheet view.
     * @param content ContentControl to check
     */
    static isContentSheetView(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    content) {
        if (!content || !content.viewport)
            return false;
        return ViewUtilities.isSheetView(content.viewport);
    }
    /**
     * Determines if content displays a Drawing view.
     * @param content ContentControl to check
     */
    static isContentDrawingView(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    content) {
        if (!content || !content.viewport)
            return false;
        return ViewUtilities.isDrawingView(content.viewport);
    }
    /**
     * Determines if content displays a Spatial view.
     * @param content ContentControl to check
     */
    static isContentSpatialView(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    content) {
        if (!content || !content.viewport)
            return false;
        return ViewUtilities.isSpatialView(content.viewport);
    }
    /**
     * Determines if content displays a Orthographic view.
     * @param content ContentControl to check
     */
    static isContentOrthographicView(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    content) {
        if (!content || !content.viewport)
            return false;
        return ViewUtilities.isOrthographicView(content.viewport);
    }
    /**
     * Determines if content displays a 3d view.
     * @param content ContentControl to check
     */
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    static isContent3dView(content) {
        if (!content || !content.viewport)
            return false;
        return ViewUtilities.is3dView(content.viewport);
    }
    /**
     * Determines if viewport supports use of a camera.
     * @param content ContentControl to check
     */
    static contentSupportsCamera(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    content) {
        if (!content || !content.viewport)
            return false;
        return ViewUtilities.viewSupportsCamera(content.viewport);
    }
    /** Manage content layouts. */
    static get layouts() {
        return InternalContentLayoutManager;
    }
    /** Manage dialogs displaying managed content. */
    static get dialogs() {
        return InternalContentDialogManager;
    }
}
//# sourceMappingURL=InternalContentViewManager.js.map