"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 ContentView
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FloatingViewportContentControl = exports.ViewportContentControl = void 0;
const core_frontend_1 = require("@itwin/core-frontend");
const ConfigurableUiControl_1 = require("../configurableui/ConfigurableUiControl");
const CubeNavigationAidControl_1 = require("../navigationaids/CubeNavigationAidControl");
const DrawingNavigationAidControl_1 = require("../navigationaids/DrawingNavigationAidControl");
const SheetNavigationAid_1 = require("../navigationaids/SheetNavigationAid");
const UiFramework_1 = require("../UiFramework");
const ViewUtilities_1 = require("../utils/ViewUtilities");
const ContentControl_1 = require("./ContentControl");
/**
 * The base class for frontstage Viewport content controls that connects to a `ScreenViewport`
 * that is managed by the `ViewManager`.
 * @public
 */
class ViewportContentControl extends ContentControl_1.ContentControl {
    /** Creates an instance of ViewportContentControl.
     * @param info         An object that the subclass must pass to this base class.
     * @param options      Options provided via the applicationData in a [[ContentProps]].
     */
    constructor(info, options) {
        super(info, options);
        /**
         * Fetches appropriate NavigationAid based on the class of the current viewport.
         * @param classFullName The full name of the current viewport class.
         * @returns The ID of the navigation aid to be displayed.
         */
        this._getNavigationAid = (classFullName) => {
            const className = ViewUtilities_1.ViewUtilities.getBisBaseClass(classFullName);
            let navigationAidId = "";
            switch (className) {
                case core_frontend_1.SheetViewState.className:
                    navigationAidId = SheetNavigationAid_1.SheetNavigationAidControl.navigationAidId;
                    break;
                case core_frontend_1.DrawingViewState.className:
                    navigationAidId = DrawingNavigationAidControl_1.DrawingNavigationAidControl.navigationAidId;
                    break;
                case core_frontend_1.SpatialViewState.className:
                case core_frontend_1.OrthographicViewState.className:
                    navigationAidId = CubeNavigationAidControl_1.CubeNavigationAidControl.navigationAidId;
                    break;
            }
            return navigationAidId;
        };
        this._isReady = new Promise((onSuccess, _onFailure) => {
            this._viewportReadyCallback = onSuccess;
        });
    }
    /** Gets the type of ConfigurableUiControl, which is 'Viewport' in this case */
    getType() {
        return ConfigurableUiControl_1.ConfigurableUiControlType.Viewport;
    }
    /** Returns true if this control is a Viewport control. */
    get isViewport() {
        return true;
    }
    /** The underlying ScreenViewport */
    get viewport() {
        return this._viewport;
    }
    set viewport(v) {
        this._viewport = v;
        this.setIsReady();
    }
    /** Returns a promise that resolves when the control is ready for usage.
     */
    setIsReady() {
        if (this._viewportReadyCallback) {
            this._viewportReadyCallback();
        }
    }
    /** Returns a promise that resolves when the control is ready for usage.
     */
    get isReady() {
        return this._isReady;
    }
    /** Called when this ContentControl is activated */
    onActivated() {
        super.onActivated();
    }
    /** Get the NavigationAidControl associated with this ContentControl */
    get navigationAidControl() {
        let navigationAidId = "";
        if (this.viewport) {
            navigationAidId = this._getNavigationAid(this.viewport.view.classFullName);
        }
        return navigationAidId;
    }
    /** Returns true if this control supports processing ViewSelector changes. */
    get supportsViewSelectorChange() {
        return true;
    }
    /** Process a ViewSelector change. */
    async processViewSelectorChange(iModel, viewDefinitionId, viewState, name) {
        if (this.viewport) {
            if (core_frontend_1.IModelApp.viewManager &&
                this.viewport === core_frontend_1.IModelApp.viewManager.selectedView)
                this.viewport.changeView(viewState);
        }
        else {
            this.reactNode = this.getReactElementForViewSelectorChange(iModel, viewDefinitionId, viewState, name);
        }
        UiFramework_1.UiFramework.content.refreshActive(this.reactNode);
    }
    /** Get the React.Element for a ViewSelector change. */
    getReactElementForViewSelectorChange(_iModel, _viewDefinitionId, _viewState, _name) {
        return null;
    }
}
exports.ViewportContentControl = ViewportContentControl;
/**
 * @beta
 */
class FloatingViewportContentControl extends ViewportContentControl {
    constructor(uniqueId, name, node) {
        super(new ConfigurableUiControl_1.ConfigurableCreateInfo(name, uniqueId, name), undefined);
        this._reactNode = node;
        this.setIsReady();
    }
    /** The React node associated with this control. */
    get reactNode() {
        return this._reactNode;
    }
    set reactNode(r) {
        this._reactNode = r;
        const activeFrontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        if (this.viewport && activeFrontstageDef)
            activeFrontstageDef.setActiveViewFromViewport(this.viewport);
    }
}
exports.FloatingViewportContentControl = FloatingViewportContentControl;
//# sourceMappingURL=ViewportContentControl.js.map