/*---------------------------------------------------------------------------------------------
 * 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 { InternalFrontstageManager } from "../frontstage/InternalFrontstageManager";
import { UiFramework } from "../UiFramework";
import { ContentLayoutDef } from "./ContentLayout";
/** ContentLayout Manager class.
 * @internal
 */
export class InternalContentLayoutManager {
    /** build a layout key that is unique for group layout combination */
    static getKey(props) {
        return `${props.contentGroupId}-${props.layoutId}`;
    }
    /** Return a LayoutDef that is specific to a content group.
     * @returns the [[ContentLayoutDef]] if found, or undefined otherwise
     */
    static getForGroup(contentGroupProps, overrideContentLayout) {
        const layoutId = overrideContentLayout?.id ?? contentGroupProps.layout.id;
        const layoutKey = this.getKey({
            contentGroupId: contentGroupProps.id,
            layoutId,
        });
        if (!overrideContentLayout &&
            InternalContentLayoutManager._layoutDefs.has(layoutKey)) {
            return InternalContentLayoutManager._layoutDefs.get(layoutKey);
        }
        const newContentLayoutProps = {
            ...contentGroupProps.layout,
            ...overrideContentLayout,
        };
        const newLayoutDef = new ContentLayoutDef(newContentLayoutProps);
        this.add(layoutKey, newLayoutDef);
        return newLayoutDef;
    }
    /** Finds a Content Layout with a given id.
     * @param layoutKey  group specific layout id, see `getLayoutKey`
     * @returns the [[ContentLayoutDef]] if found, or undefined otherwise
     */
    static find(layoutKey) {
        return InternalContentLayoutManager._layoutDefs.get(layoutKey);
    }
    /** Adds a Content Layout.
     * @param layoutId  the id of the Content Layout to add
     * @param layoutDef  the Content Layout definition to add
     */
    static add(layoutId, layoutDef) {
        InternalContentLayoutManager._layoutDefs.set(layoutId, layoutDef);
    }
    /** Gets the active Content Layout */
    static get activeLayout() {
        let layoutDef;
        const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (activeFrontstageDef)
            layoutDef = activeFrontstageDef.contentLayoutDef;
        return layoutDef;
    }
    /** Gets the active Content Group */
    static get activeContentGroup() {
        let contentGroup;
        const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (activeFrontstageDef)
            contentGroup = activeFrontstageDef.contentGroup;
        return contentGroup;
    }
    /** Sets the active Content Layout, Content Group and Content Control.
     * @param contentLayoutDef  Content layout to make active
     * @param contentGroup  Content Group to make active
     */
    static async setActive(contentLayoutDef, contentGroup) {
        await InternalFrontstageManager.setActiveLayout(contentLayoutDef, contentGroup);
    }
    /** Sets the active Content Group.
     * @param contentGroup  Content Group to make active
     */
    static async setActiveContentGroup(contentGroup) {
        await InternalFrontstageManager.setActiveContentGroup(contentGroup);
    }
    /** Refreshes the active layout and content group.
     */
    static refreshActive() {
        const activeFrontstageDef = UiFramework.frontstages.activeFrontstageDef;
        if (activeFrontstageDef &&
            activeFrontstageDef.contentLayoutDef &&
            activeFrontstageDef.contentGroup) {
            UiFramework.frontstages.onContentLayoutActivatedEvent.emit({
                contentLayout: activeFrontstageDef.contentLayoutDef,
                contentGroup: activeFrontstageDef.contentGroup,
            });
            activeFrontstageDef.contentGroup.refreshContentNodes();
        }
    }
}
InternalContentLayoutManager._layoutDefs = new Map();
//# sourceMappingURL=InternalContentLayoutManager.js.map