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

/**
 * @fileOverview Module that provides the definition of the RootIntent.
 */

sap.ui.define([
    "sap/base/util/ObjectPath",
    "sap/ushell/base/UrlParsing"
], (
    ObjectPath,
    UrlParsing
) => {
    "use strict";

    /**
     * @alias sap.ushell.navigation.RootIntent
     * @namespace
     * @description Module that provides the the definition of the RootIntent.
     *
     * @since 1.147.0
     * @private
     */
    class RootIntent {
        get () {
            const Container = sap.ui.require("sap/ushell/Container"); // Avoid circular dependency RootIntent -> Container -> ushellUtils -> RootIntent
            const oRenderer = Container.getRendererInternal("fiori2");

            let sConfiguredIntent;
            if (oRenderer) {
                try {
                    sConfiguredIntent = oRenderer.getShellConfig().rootIntent;
                    // Might fail when accessing Renderer's config before the view is created
                } catch { /* fail silently */ }
            }

            if (!sConfiguredIntent) { // fallback to config path if there is no FLP (yet)
                const sRootIntentConfigPath = "renderers.fiori2.componentData.config.rootIntent";
                sConfiguredIntent = ObjectPath.get(sRootIntentConfigPath, window["sap-ushell-config"]);
            }

            if (!sConfiguredIntent) {
                sConfiguredIntent = "#Shell-home";
            }

            if (!sConfiguredIntent.startsWith("#")) {
                sConfiguredIntent = `#${sConfiguredIntent}`;
            }

            return sConfiguredIntent;
        }

        /**
         * Checks whether the given parsed hash represents the root intent.
         * @param {sap.ushell.services.URLParsing.DecomposedHash} oParsedHash The parsed hash to check
         * @returns {boolean} Whether the parsed hash represents the root intent
         *
         * @since 1.142.0
         * @private
         */
        isEqual (oParsedHash) {
            const sRootIntent = this.get();
            const oParsedRootIntent = UrlParsing.parseShellHash(sRootIntent, true);
            const oHashComparison = UrlParsing.compareParsedHashes(oParsedHash, oParsedRootIntent);

            return oHashComparison.sameIntent && oHashComparison.sameParameters;
        }
    }

    return new RootIntent();
});
