"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable deprecation/deprecation */
/** @packageDocumentation
 * @module Widget
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WidgetHost = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const UiFramework_1 = require("../UiFramework");
const Zone_1 = require("../zones/Zone");
/**
 * A WidgetHost represents a definition that hosts one or most Widgets in a Frontstage.
 * @public
 */
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, frontstageApplicationData) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
        // 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 (UiFramework_1.UiFramework.uiVersion === "1") {
            // istanbul ignore next
            if (location in Zone_1.ZoneLocation) {
                switch (location) {
                    case Zone_1.ZoneLocation.CenterLeft:
                        {
                            const middleDynamicWidgetDefs = (_a = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.CenterLeft, section, frontstageApplicationData)) !== null && _a !== void 0 ? _a : [];
                            dynamicWidgetDefs = [...middleDynamicWidgetDefs];
                        }
                        break;
                    case Zone_1.ZoneLocation.BottomLeft:
                        {
                            const bottomDynamicWidgetDefs = (_b = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.BottomLeft, section, frontstageApplicationData)) !== null && _b !== void 0 ? _b : [];
                            dynamicWidgetDefs = [...bottomDynamicWidgetDefs];
                        }
                        break;
                    case Zone_1.ZoneLocation.CenterRight:
                        {
                            const middleDynamicWidgetDefs = (_c = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.CenterRight, section, frontstageApplicationData)) !== null && _c !== void 0 ? _c : [];
                            dynamicWidgetDefs = [...middleDynamicWidgetDefs];
                        }
                        break; // added to BottomRight
                    case Zone_1.ZoneLocation.BottomRight:
                        {
                            const bottomDynamicWidgetDefs = (_d = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.BottomRight, section, frontstageApplicationData)) !== null && _d !== void 0 ? _d : [];
                            dynamicWidgetDefs = [...bottomDynamicWidgetDefs];
                        }
                        break;
                }
            }
            else if ((location in appui_abstract_1.StagePanelLocation) && undefined !== section) {
                switch (section) {
                    case appui_abstract_1.StagePanelSection.Start: {
                        dynamicWidgetDefs = (_e = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.Start, frontstageApplicationData)) !== null && _e !== void 0 ? _e : [];
                        break;
                    }
                    case appui_abstract_1.StagePanelSection.Middle:
                        const middleDynamicWidgetDefs = (_f = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.Middle, frontstageApplicationData)) !== null && _f !== void 0 ? _f : [];
                        dynamicWidgetDefs = [...middleDynamicWidgetDefs];
                        break; // added to BottomLeft
                    case appui_abstract_1.StagePanelSection.End: {
                        const bottomDynamicWidgetDefs = (_g = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.End, frontstageApplicationData)) !== null && _g !== void 0 ? _g : [];
                        dynamicWidgetDefs = [...bottomDynamicWidgetDefs];
                        break;
                    }
                }
            }
        }
        else {
            if (location in Zone_1.ZoneLocation) {
                switch (location) {
                    case Zone_1.ZoneLocation.CenterLeft:
                        break; // added to BottomLeft
                    case Zone_1.ZoneLocation.BottomLeft:
                        {
                            const middleDynamicWidgetDefs = (_h = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.CenterLeft, section, frontstageApplicationData)) !== null && _h !== void 0 ? _h : [];
                            const bottomDynamicWidgetDefs = (_j = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.BottomLeft, section, frontstageApplicationData)) !== null && _j !== void 0 ? _j : [];
                            dynamicWidgetDefs = [...middleDynamicWidgetDefs, ...bottomDynamicWidgetDefs];
                        }
                        break;
                    case Zone_1.ZoneLocation.CenterRight:
                        break; // added to BottomRight
                    case Zone_1.ZoneLocation.BottomRight:
                        {
                            const middleDynamicWidgetDefs = (_k = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.CenterRight, section, frontstageApplicationData)) !== null && _k !== void 0 ? _k : [];
                            const bottomDynamicWidgetDefs = (_l = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, Zone_1.ZoneLocation.BottomRight, section, frontstageApplicationData)) !== null && _l !== void 0 ? _l : [];
                            dynamicWidgetDefs = [...middleDynamicWidgetDefs, ...bottomDynamicWidgetDefs];
                        }
                        break;
                }
            }
            else 
            // istanbul ignore else
            if ((location in appui_abstract_1.StagePanelLocation) && undefined !== section) {
                switch (section) {
                    case appui_abstract_1.StagePanelSection.Start: {
                        dynamicWidgetDefs = (_m = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.Start, frontstageApplicationData)) !== null && _m !== void 0 ? _m : [];
                        break;
                    }
                    case appui_abstract_1.StagePanelSection.Middle:
                        break; // added to BottomLeft
                    case appui_abstract_1.StagePanelSection.End: {
                        const middleDynamicWidgetDefs = (_o = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.Middle, frontstageApplicationData)) !== null && _o !== void 0 ? _o : [];
                        const bottomDynamicWidgetDefs = (_p = UiFramework_1.UiFramework.widgetManager.getWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.End, frontstageApplicationData)) !== null && _p !== void 0 ? _p : [];
                        dynamicWidgetDefs = [...middleDynamicWidgetDefs, ...bottomDynamicWidgetDefs];
                        break;
                    }
                }
            }
        }
        const uniqueDynamicWidgetDefs = dynamicWidgetDefs === null || dynamicWidgetDefs === void 0 ? void 0 : 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;
    }
}
exports.WidgetHost = WidgetHost;
//# sourceMappingURL=WidgetHost.js.map