// Copyright (c) 2009-2026 SAP SE, All Rights Reserved
sap.ui.define([
    "sap/base/Log",
    "sap/ui/core/Element",
    "sap/ui/core/UIComponent",
    "sap/ui/Device",
    "sap/ui/model/json/JSONModel",
    "sap/ushell/Config",
    "sap/ushell/resources",
    "sap/ushell/utils/WindowUtils",
    "sap/ushell/ui/shell/ShellHeadItem",
    "sap/ushell/Container",
    "sap/ushell/EventHub"
], (
    Log,
    Element,
    UIComponent,
    Device,
    JSONModel,
    Config,
    resources,
    WindowUtils,
    ShellHeadItem,
    Container,
    EventHub
) => {
    "use strict";

    function getProductSwitchPopover () {
        return Element.getElementById("sapUshellProductSwitchPopover");
    }

    return UIComponent.extend("sap.ushell.components.shell.ProductSwitch.Component", {

        metadata: {
            version: "1.147.1",
            library: "sap.ushell.components.shell.ProductSwitch",
            dependencies: {
                libs: {
                    "sap.m": {},
                    "sap.f": {
                        lazy: true
                    }
                }
            }
        },

        init: function () {
            UIComponent.prototype.init.apply(this, arguments);
            this._oAPI = this.getComponentData().productSwitchAPI;
            this.oModel = this._getModel();
        },

        _getModel: function () {
            const that = this;
            const oModel = new JSONModel();
            oModel.loadData(this._oAPI.getConfigValue("url"))
                .then(() => {
                    const aProducts = oModel.getData();
                    if (aProducts.length === 0) {
                        that._oAPI.setShowProductSwitch(false);
                        Log.debug("There are no other products configured for your user. ProductSwitch button will be hidden.");
                    } else {
                        that._oAPI.setShowProductSwitch(true);
                        that._oAPI.attachProductSwitchPress(this._openProductSwitch.bind(this));
                    }
                })
                .catch((oError) => {
                    Log.debug("get ProductSwitch model failed: ", oError);
                });
            return oModel;
        },

        _openProductSwitch: function () {
            let oPopover = getProductSwitchPopover();
            let oLoadPopover = Promise.resolve();

            if (!oPopover) {
                oLoadPopover = new Promise((resolve, reject) => {
                    sap.ui.require(["sap/ui/core/Fragment"], (Fragment) => {
                        Fragment.load({
                            name: "sap.ushell.components.shell.ProductSwitch.ProductSwitch",
                            type: "XML",
                            controller: this
                        }).then(resolve).catch(reject);
                    }, reject);
                }).then((popover) => {
                    oPopover = popover;
                    oPopover.setModel(this.oModel);
                    oPopover.setModel(resources.i18nModel, "i18n");
                    if (Device.system.phone) {
                        oPopover.setShowHeader(true);
                    }
                });
            }

            oLoadPopover.then(() => {
                oPopover.openBy(this._oAPI.getProductSwitchDomRef());
            });

            return oLoadPopover;
        },

        onProductItemPress: function (oEvent) {
            const sTargetUrl = oEvent.getParameter("itemPressed").getTargetSrc();
            getProductSwitchPopover().close();
            WindowUtils.openURL(sTargetUrl, "_blank");
        },

        exit: function () {
            const oPopover = getProductSwitchPopover();
            if (oPopover) {
                oPopover.destroy();
            }
        }
    });
});
