/*---------------------------------------------------------------------------------------------
 * 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 { DrawingViewState, IModelApp, OrthographicViewState, SheetViewState, SpatialViewState, } from "@itwin/core-frontend";
import { ConfigurableCreateInfo, ConfigurableUiControlType, } from "../configurableui/ConfigurableUiControl";
import { CubeNavigationAidControl } from "../navigationaids/CubeNavigationAidControl";
import { DrawingNavigationAidControl } from "../navigationaids/DrawingNavigationAidControl";
import { SheetNavigationAidControl } from "../navigationaids/SheetNavigationAid";
import { UiFramework } from "../UiFramework";
import { ViewUtilities } from "../utils/ViewUtilities";
import { ContentControl } from "./ContentControl";
/**
 * The base class for frontstage Viewport content controls that connects to a `ScreenViewport`
 * that is managed by the `ViewManager`.
 * @public
 */
export class ViewportContentControl extends 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.getBisBaseClass(classFullName);
            let navigationAidId = "";
            switch (className) {
                case SheetViewState.className:
                    navigationAidId = SheetNavigationAidControl.navigationAidId;
                    break;
                case DrawingViewState.className:
                    navigationAidId = DrawingNavigationAidControl.navigationAidId;
                    break;
                case SpatialViewState.className:
                case OrthographicViewState.className:
                    navigationAidId = 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 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() {
        // istanbul ignore else
        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 = "";
        // istanbul ignore else
        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. */
    // istanbul ignore next
    async processViewSelectorChange(iModel, viewDefinitionId, viewState, name) {
        if (this.viewport) {
            if (IModelApp.viewManager &&
                this.viewport === IModelApp.viewManager.selectedView)
                this.viewport.changeView(viewState);
        }
        else {
            this.reactNode = this.getReactElementForViewSelectorChange(iModel, viewDefinitionId, viewState, name);
        }
        UiFramework.content.refreshActive(this.reactNode);
    }
    /** Get the React.Element for a ViewSelector change. */
    // istanbul ignore next
    getReactElementForViewSelectorChange(_iModel, _viewDefinitionId, _viewState, _name) {
        return null;
    }
}
/**
 * @beta
 */
export class FloatingViewportContentControl extends ViewportContentControl {
    constructor(uniqueId, name, node) {
        super(new 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.frontstages.activeFrontstageDef;
        // istanbul ignore else
        if (this.viewport && activeFrontstageDef)
            activeFrontstageDef.setActiveViewFromViewport(this.viewport);
    }
}
//# sourceMappingURL=ViewportContentControl.js.map