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

sap.ui.define([
    "sap/ui/core/Element",
    "sap/ushell/modules/BaseAPI"
], (
    Element,
    BaseAPI
) => {
    "use strict";

    /**
     * @alias sap.ushell.modules.Assistant.AssistantAPI
     * @namespace
     * @description The API object for the Assistant module.
     * This module cannot be required it gets injected in the constructor of the Assistant module.
     * @see sap.ushell.modules.Assistant.AssistantProvider
     *
     * @hideconstructor
     *
     * @since 1.143.0
     * @private
     * @ui5-restricted Joule
     */
    class AssistantAPI extends BaseAPI {
        /**
         * The instance of the ShellBar or ShellHeader depending on the ushell Config.
         *
         * @type {sap.f.gen.ui5.webcomponents_fiori.dist.ShellBar}
         * @since 1.143.0
         * @private
         */
        #oShellBar;

        /**
         * Creates a new instance of the AssistantAPI.
         *
         * @param {sap.f.gen.webcomponents.dist.Button} oAssistantButton The assistant button created by the ShellBar.
         * @param {object} oConfig The configuration object for the assistant module.
         * @since 1.143.0
         * @private
         * @ui5-restricted Joule
         */
        constructor (oAssistantButton, oConfig) {
            super(oConfig);
            this.#oShellBar = Element.getElementById("shell-header")?.getComponentInstance?.()?.getRootControl()?.byId("shellBar");
            if (this.#oShellBar) {
                this.#oShellBar.destroyAssistant();
                this.#oShellBar.addAssistant(oAssistantButton);
            }
        }

        /**
         * Checks if the ShellBar instance is available. This is used as long as the ShellHeader may still be used.
         *
         * @returns {boolean} True if the ShellBar instance is available, false otherwise.
         * @since 1.143.0
         * @private
         */
        #isShellBar () {
            return this.#oShellBar?.isA("sap.f.gen.ui5.webcomponents_fiori.dist.ShellBar");
        }

        /**
         * Gets the assistant button. It is pre-created to be rendered as early as possible in the ShellBar. It will be set to disabled until the Assistant module is loaded.
         *
         * @returns {sap.f.gen.webcomponents.dist.Button} The assistant button or undefined if not available.
         * @since 1.143.0
         * @private
         * @ui5-restricted Joule
         */
        getAssistantButton () {
            if (!this.#isShellBar()) {
                // only available in ShellBar
                return;
            }
            return this.#oShellBar.getAssistant()[0];
        }

        /**
         * Gets the DOM reference for the assistant button.
         *
         * @returns {Element} The DOM reference.
         * @since 1.143.0
         * @private
         * @ui5-restricted Joule
         */
        getAssistantDomRef () {
            return this.getAssistantButton()?.getDomRef();
        }
    }

    return AssistantAPI;
});
