// Copyright (c) 2009-2026 SAP SE, All Rights Reserved

/**
 * @file This module provides an API for configuring the product switch of the Launchpad shell bar.
 */

sap.ui.define([
    "sap/ushell/Config",
    "sap/ushell/utils"
], (
    Config,
    ushellUtils
) => {
    "use strict";

    /**
     * @alias sap.ushell.modules.ProductSwitch
     * @namespace
     * @description The Product Switch module API to set a ProductSwitchProvider and Config.
     *
     * @since 1.144.0
     * @private
     *
     * @ui5-restricted CEP
     */
    class ProductSwitch {
        /**
         * Sets the product switch provider and loads the product switch module.
         * @param {string} sModulePath The module path of the product switch provider.
         * @param {object} oConfiguration  The configuration object for the product switch provider.
         *
         * @since 1.144.0
         * @private
         * @ui5-restricted CEP
         */
        async setProductSwitchProvider (sModulePath, oConfiguration) {
            Config.emit("/core/productSwitch", {
                enabled: true,
                modulePath: sModulePath,
                configuration: JSON.stringify(oConfiguration || {})
            });

            // ensure the ProductSwitchLoader is loaded so it can react to the config change.
            // This may happen asynchronously if the FLP was not configured to use a product switch at startup.
            const [oProductSwitchLoader] = await ushellUtils.requireAsync(["sap/ushell/modules/loader/ProductSwitchLoader"]);
            await oProductSwitchLoader.initialized();
        }
    }
    return new ProductSwitch();
});
