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

sap.ui.define([
    "sap/ushell/Container"
], (
    Container
) => {
    "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 {
        #pPersonalizer;
        #pEnabled;

        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 = this.getPersData()
                    .then((oPersData) => 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;
        }
    }

    return new UserHomeService();
});
