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

/**
 * @file This module provides an API for configuring the search of the Launchpad shell bar.
 * It allows adding a SearchProvider to the search via for example a Plugin.
 */

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

    /**
     * @alias sap.ushell.modules.Search
     * @namespace
     * @description The Search module API to set a SearchProvider and Config.
     *
     * @since 1.142.0
     * @private
     *
     * @ui5-restricted HAN-AS-INA-UI
     */
    class Search {
        /**
         * Sets the search provider for the search module.
         * @param {string} sModulePath The module path of the search provider.
         * @param {string} sSearchResultAppIntent The intent to be used for the search result app incl. #.
         * @param {object} oConfiguration  The configuration object for the search provider.
         *
         * @returns {Promise} A promise that resolves when the configuration is set.
         *
         * @since 1.142.0
         * @private
         * @ui5-restricted HAN-AS-INA-UI
         */
        async setSearchProvider (sModulePath, sSearchResultAppIntent, oConfiguration) {
            Config.emit("/core/search", {
                enabled: true,
                modulePath: sModulePath,
                useWebComponents: true,
                initiallyCollapsed: Config.last("/core/search/initiallyCollapsed"),
                configuration: JSON.stringify(oConfiguration || {}),
                searchResultAppIntent: sSearchResultAppIntent
            });
            // ensure the SearchLoader is loaded so it can react to the config change.
            // This may happen asynchronously if the FLP was not configured to use search at startup.
            const [oSearchLoader] = await ushellUtils.requireAsync(["sap/ushell/modules/loader/SearchLoader"]);
            await oSearchLoader.initialized();
        }
    }
    return new Search();
});
