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

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

    /**
     * @alias sap.ushell.utils.workpage.UserHomeService
     * @class
     * @classdesc Utility for managing User Home (My Space) personalization settings.
     *
     * @since 1.147.0
     * @private
     */
    class UserHomeService {
        constructor () {
            this._pPersonalizer = null;
            this._pEnabled = null;
        }

        /**
         * Get and cache the personalizer for My Space settings.
         *
         * @returns {Promise<object>} The personalizer instance
         * @since 1.147.0
         * @private
         */
        async _getPersonalizer () {
            if (this._pPersonalizer) {
                return this._pPersonalizer;
            }
            const oPersonalizationService = await Container.getServiceAsync("PersonalizationV2");
            const oScope = {
                keyCategory: oPersonalizationService.KeyCategory.FIXED_KEY,
                writeFrequency: oPersonalizationService.WriteFrequency.LOW,
                clientStorageAllowed: true
            };
            const oPersId = {
                container: "sap.ushell.mySpace",
                item: "settings"
            };

            this._pPersonalizer = await oPersonalizationService.getPersonalizer(oPersId, oScope);
            return this._pPersonalizer;
        }

        /**
         * Read the persisted My Space settings.
         *
         * @returns {Promise<object>} The personalization data
         * @since 1.147.0
         * @private
         */
        async getPersData () {
            const oPersonalizer = await this._getPersonalizer();
            return await oPersonalizer.getPersData() || {};
        }

        /**
         * Write the My Space settings to the personalization service.
         *
         * @param {object} oData The data to persist
         * @returns {Promise<undefined>} Resolves when data is saved
         * @since 1.147.0
         * @private
         */
        async setPersData (oData) {
            const oPersonalizer = await this._getPersonalizer();
            await oPersonalizer.setPersData(oData);
        }

        /**
         * Check whether My Space is enabled in the user's personalization settings.
         *
         * @returns {Promise<boolean>} Whether My Space is enabled
         * @since 1.147.0
         * @private
         */
        isEnabled () {
            if (!this._pEnabled) {
                this._pEnabled = (async () => {
                    const oPersData = await this.getPersData();
                    return oPersData?.enabled ?? false;
                })();
            }
            return this._pEnabled;
        }

        /**
         * Resets the cached state so that the next call to {@link #isEnabled}
         * re-reads the value from the personalization service.
         *
         * @since 1.147.0
         * @private
         */
        resetCache () {
            this._pPersonalizer = null;
            this._pEnabled = null;
        }

        /**
         * Deletes the user's My Space workpage from the backend.
         *
         * @returns {Promise} Resolves when the workpage has been deleted
         * @since 1.147.0
         * @private
         */
        deleteWorkPage () {
            const sSiteId = Config.last("/core/site/siteId");
            const oWorkPageService = new WorkPageService();
            return oWorkPageService.deleteWorkPage(sSiteId);
        }
    }

    return new UserHomeService();
});
