"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 Widget
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WidgetManager = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const UiFramework_1 = require("../UiFramework");
const WidgetDef_1 = require("./WidgetDef");
const StableWidgetDef_1 = require("./StableWidgetDef");
const StagePanelLocation_1 = require("../stagepanels/StagePanelLocation");
const StagePanelSection_1 = require("../stagepanels/StagePanelSection");
const UiItemsManager_1 = require("../ui-items-provider/UiItemsManager");
function getWidgetManagerStableWidgetId(stageUsage, location, section, index) {
    return `uifw-wm-${stageUsage || ""}-${StagePanelLocation_1.StagePanelLocation[location]}-${StagePanelSection_1.StagePanelSection[section]}-${index}`;
}
/** Widget Manager class.
 * @beta
 */
class WidgetManager {
    constructor() {
        this._widgets = [];
        /** Event raised when Widgets are changed.
         * @internal
         */
        this.onWidgetsChanged = new core_bentley_1.BeUiEvent();
    }
    /** @internal */
    get widgetCount() {
        return this._widgets.length;
    }
    /** @internal */
    get widgets() {
        return this._widgets;
    }
    set widgets(w) {
        this._widgets = w;
        this.onWidgetsChanged.emit({ items: w });
    }
    /** Adds a WidgetDef for use in a Frontstage.
     * @note Added `widgetDef` must return unique id to correctly save/restore App layout.
     * Semi-stable id is generated when auto-generated `widgetDef` id is detected,
     * but correctness of such id depends on `addWidgetDef` call order and widget location.
     */
    addWidgetDef(widgetDef, stageId, stageUsage, location, section) {
        if (stageId === undefined && stageUsage === undefined) {
            core_bentley_1.Logger.logError(UiFramework_1.UiFramework.loggerCategory(this), `addWidgetDef: stageId or stageUsage param must be specified`);
            return false;
        }
        section = section !== undefined ? section : StagePanelSection_1.StagePanelSection.Start;
        const index = this._widgets.reduce((acc, info) => {
            if (info.stageId === stageId &&
                info.stageUsage === stageUsage &&
                info.location === location &&
                info.section === section)
                return acc + 1;
            return acc;
        }, 0);
        const stableId = getWidgetManagerStableWidgetId(stageUsage, location, section, index);
        const stableWidget = (0, StableWidgetDef_1.createStableWidgetDef)(widgetDef, stableId);
        const newWidget = {
            widgetDef: stableWidget,
            stageId,
            stageUsage,
            location,
            section,
        };
        const oldWidgets = this._widgets.filter((info) => info.widgetDef.id !== newWidget.widgetDef.id);
        const updatedWidgets = [...oldWidgets, newWidget];
        this.widgets = updatedWidgets;
        return true;
    }
    /** Removes a WidgetDef.
     */
    removeWidgetDef(widgetId) {
        let result = false;
        const updatedWidgets = this._widgets.filter((info) => info.widgetDef.id !== widgetId);
        if (updatedWidgets.length !== this._widgets.length) {
            this.widgets = updatedWidgets;
            result = true;
        }
        return result;
    }
    /** Gets WidgetDefs for a Frontstage location.
     */
    getWidgetDefs(stageId, stageUsage, location, section) {
        const definedSection = section === undefined ? StagePanelSection_1.StagePanelSection.Start : section;
        const widgetInfos = this._widgets.filter((info) => {
            return ((!info.stageId || info.stageId === stageId) &&
                (!info.stageUsage || info.stageUsage === stageUsage) &&
                info.location === location &&
                info.section === definedSection);
        });
        const widgetDefs = widgetInfos.map((info) => info.widgetDef);
        // Consult the UiItemsManager to get any Abstract widgets
        const widgets = UiItemsManager_1.UiItemsManager.getWidgets(stageId, stageUsage, location, definedSection);
        widgets.forEach((abstractProps) => {
            const widgetDef = WidgetDef_1.WidgetDef.create(abstractProps);
            widgetDefs.push(widgetDef);
        });
        return widgetDefs.length > 0 ? widgetDefs : undefined;
    }
}
exports.WidgetManager = WidgetManager;
//# sourceMappingURL=WidgetManager.js.map