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

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

sap.ui.define([
    "sap/ushell/navigation/RootIntent"
], (RootIntent) => {
    "use strict";

    /**
     * @alias sap.ushell.navigation.HomeIntent
     * @namespace
     * @description Module that determines whether a given intent is an FLP home intent.
     * Covers all known home intent patterns: classic homepage, spaces/pages navigation,
     * work pages, and Space Overview.
     *
     * @since 1.148.0
     * @private
     */
    class HomeIntent {
        /**
         * Checks whether the given intent is an FLP home intent
         * (classical or spaces/pages, including Space Overview).
         *
         * @param {string} [sIntent] The intent to check, e.g. <code>#Shell-home</code>.
         *   If omitted, <code>window.location.hash</code> is used. Falls back to the
         *   configured root intent if the hash is also empty.
         * @returns {boolean} Whether the given intent is an FLP home intent.
         * @throws {Error} When a non-string value is passed as <code>sIntent</code>.
         *
         * @private
         */
        isHomeIntent (sIntent) {
            if (!sIntent) {
                sIntent = window.location.hash || RootIntent.get();
            } else if (typeof sIntent !== "string") {
                throw new Error("The given intent must be a string");
            }

            const sIntentNoHash = sIntent.replace("#", "");
            if (sIntentNoHash.startsWith("Shell-home") ||
                sIntentNoHash.startsWith("Launchpad-openFLPPage") ||
                sIntentNoHash.startsWith("Launchpad-openWorkPage") ||
                sIntentNoHash.startsWith("Launchpad-openSpaceOverview")) {
                return true;
            }

            // Check if the current application is marked as an FLP home view (e.g. Space Overview).
            // Falls back to false during start when no application has been resolved yet.
            // Note: sap.ui.require is used to avoid circular dependency.
            const AppConfiguration = sap.ui.require("sap/ushell/services/AppConfiguration");
            const oCurrentApp = AppConfiguration && AppConfiguration.getCurrentApplication();
            return !!(oCurrentApp && oCurrentApp.isFLPHome === true);
        }
    }

    return new HomeIntent();
});
