/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Widget
 */
import { UiFramework } from "../UiFramework";
/**
 * A WidgetHost represents a definition that hosts one or most Widgets in a Frontstage.
 * @public
 */
export class WidgetHost {
    /** Constructor for WidgetHost.
     */
    constructor() {
        this._widgetDefs = new Array();
        this._sortedWidgetDefs = [];
    }
    /** Adds a WidgetDef to the list of Widgets.
     * @param widgetDef  Definition of the Widget to add
     */
    addWidgetDef(widgetDef) {
        this._widgetDefs.push(widgetDef);
        this.sortWidgetDefs();
    }
    /** Gets the list of Widgets. */
    get widgetDefs() {
        return this._sortedWidgetDefs;
    }
    /** Gets the number of Widgets. */
    get widgetCount() {
        return this.widgetDefs.length;
    }
    /** If there is only one Widget in the Panel, gets the single WidgetDef.
     * @returns The single WidgetDef if there is only one Widget; otherwise, undefined is returned.
     */
    getSingleWidgetDef() {
        if (this.widgetCount === 1) {
            return this.widgetDefs[0];
        }
        return undefined;
    }
    /** Finds a WidgetDef with a given Id.
     * @param id  Id of the WidgetDef to find
     * @returns The WidgetDef if found; otherwise, undefined is returned.
     */
    findWidgetDef(id) {
        return this.widgetDefs.find((widgetDef) => widgetDef.id === id);
    }
    /** Updates the WidgetHost with dynamic widgets
     * @internal
     */
    updateDynamicWidgetDefs(stageId, stageUsage, location, section, allStageWidgetDefs) {
        // get widgetDefs not already in allStageWidgetDefs and add them
        const uniqueWidgets = this._widgetDefs.filter((widgetDef) => {
            return !allStageWidgetDefs.find((wDef) => wDef.id === widgetDef.id);
        });
        allStageWidgetDefs.push(...uniqueWidgets);
        let dynamicWidgetDefs;
        if (section !== undefined) {
            dynamicWidgetDefs =
                UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, section) ?? [];
        }
        const uniqueDynamicWidgetDefs = dynamicWidgetDefs?.filter((widgetDef) => {
            return !allStageWidgetDefs.find((wDef) => wDef.id === widgetDef.id);
        });
        // Now that we no longer support a middle panel section, yet we have existing API that allows a middle section to be
        // defined, the following is needed to combining middle and end panel section widgets into a single set of widgets
        if (uniqueDynamicWidgetDefs) {
            allStageWidgetDefs.push(...uniqueDynamicWidgetDefs);
            this._dynamicWidgetDefs = [...uniqueDynamicWidgetDefs];
        }
        this.sortWidgetDefs();
    }
    /** Sorts all widgets */
    sortWidgetDefs() {
        let sortedWidgetDefs = [];
        if (this._dynamicWidgetDefs)
            sortedWidgetDefs = this._widgetDefs.concat(this._dynamicWidgetDefs);
        else
            sortedWidgetDefs = this._widgetDefs.slice();
        sortedWidgetDefs.sort((a, b) => a.priority - b.priority);
        this._sortedWidgetDefs = sortedWidgetDefs;
    }
}
//# sourceMappingURL=WidgetHost.js.map