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

sap.ui.define([
    "sap/base/Log",
    "sap/ui/core/mvc/View",
    "sap/ushell/resources",
    "sap/ushell/Config",
    "sap/ushell/Container",
    "sap/ushell/utils/workpage/UserHomeService"
], (
    Log,
    View,
    resources,
    Config,
    Container,
    UserHomeService
) => {
    "use strict";

    return {
        isRelevant: function () {
            return Config.last("/core/workPages/enabled") && Config.last("/core/workPages/userHome/configurable");
        },
        getEntry: function () {
            let oViewInstance;
            return {
                id: "mySpaceEntry",
                entryHelpID: "mySpace",
                title: resources.i18n.getText("mySpaceTitle"),
                valueResult: null,
                contentResult: null,
                icon: "sap-icon://key-user-settings",
                valueArgument: async function () {
                    const bShow = await UserHomeService.isEnabled();
                    return resources.i18n.getText(bShow ? "settingsMySpaceShown" : "settingsMySpaceHidden");
                },
                contentFunc: async function () {
                    const oUserInfoService = await Container.getServiceAsync("UserInfo");
                    oViewInstance = await View.create({
                        id: "UserSettingsMySpaceSettingsView",
                        viewName: "sap.ushell.components.shell.Settings.mySpace.MySpaceSetting",
                        type: "XML",
                        viewData: {
                            UserInfo: oUserInfoService
                        }
                    });
                    return oViewInstance;
                },
                onSave: function (fnUpdateUserPreferences) {
                    if (oViewInstance) {
                        return oViewInstance.getController().onSave(fnUpdateUserPreferences);
                    }
                    Log.warning("Save operation for my space settings was not executed, because the my space view was not initialized");
                    return Promise.resolve();
                },
                onCancel: function () {
                    if (oViewInstance) {
                        oViewInstance.getController().onCancel();
                        return;
                    }
                    Log.warning("Cancel operation for my space settings was not executed, because the my space view was not initialized");
                },
                provideEmptyWrapper: false,
                // for testing purpose
                _setViewInstance: function (oNewViewInstance) {
                    oViewInstance = oNewViewInstance;
                },
                _getViewInstance: function () {
                    return oViewInstance;
                }
            };
        }
    };
});
