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

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

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

    // load the default visual configuration from config just when the module is loaded to ensure
    // that the defaults where not changed by an API call before. The assumption is that the config
    // is either set initially by the bootsrap config or was changed by this API.
    const oDefaultVisualConfig = Config.last("/core/assistant/visualConfiguration");

    /**
     * @alias sap.ushell.modules.Assistant
     * @namespace
     * @description The Assistant module API to set an AssistantProvider and Config.
     *
     * @since 1.143.0
     * @private
     *
     * @ui5-restricted Joule
     */
    class Assistant {
        /**
         * Sets the assistant provider for the assistant module.
         * @param {string} sModulePath The module path of the assistant provider.
         * @param {sap.ushell.modules.Assistant.VisualConfiguration} oVisualConfiguration The visual configuration object for the assistant button.
         * @param {object} oConfiguration  The configuration object for the assistant provider.
         *
         * @returns {Promise} A promise that resolves when the configuration is set.
         *
         * @since 1.143.0
         * @private
         * @ui5-restricted Joule
         */
        async setAssistantProvider (sModulePath, oVisualConfiguration, oConfiguration) {
            Config.emit("/core/assistant/enabled", true);
            Config.emit("/core/assistant/modulePath", sModulePath);
            Config.emit("/core/assistant/configuration", JSON.stringify(oConfiguration || {}));
            Config.emit("/core/assistant/visualConfiguration", Object.assign(oDefaultVisualConfig, oVisualConfiguration || {}));

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