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

/**
 * @fileOverview Navigation Sample Application Component.
 *
 * This demo application showcases FLP navigation capabilities including:
 * - Cross-application navigation with semantic objects and actions
 * - AppState handling and visualization
 * - Startup parameter processing
 *
 * @version 1.148.1
 * @since 1.147
 */
sap.ui.define([
    "sap/base/Log",
    "sap/ui/core/UIComponent",
    "sap/ui/model/json/JSONModel"
], (
    Log,
    UIComponent,
    JSONModel
) => {
    "use strict";

    const sComponent = "sap.ushell.sandbox.NavigationSample";

    /**
     * UI Component for the Navigation Sample application.
     *
     * Bootstraps the sample app by:
     * - Initializing the router for inner-app navigation
     * - Exposing startup parameters to the view
     * - Fetching and normalizing the FLP AppState
     *
     * @class
     * @extends sap.ui.core.UIComponent
     * @alias sap.ushell.sandbox.NavigationSample.Component
     * @since 1.147
     * @private
     */
    return UIComponent.extend("sap.ushell.sandbox.NavigationSample.Component", {
        metadata: {
            manifest: "json",
            interfaces: ["sap.ui.core.IAsyncContentCreation"]
        },

        /**
         * Initializes routing and models and triggers optional deep-link navigation.
         *
         * @private
         */
        init: function () {
            UIComponent.prototype.init.apply(this, arguments);
            this.getRouter().initialize();

            const oComponentData = this.getComponentData() || {};
            const oStartupParameters = oComponentData.startupParameters || {};
            const sNavTo = oStartupParameters.navTo && oStartupParameters.navTo[0];
            if (sNavTo) {
                this.getRouter().navTo(sNavTo, null, true);
            }

            // Container probing: sap.ui.require returns undefined if the Container
            // module was never loaded (i.e. no FLP bootstrap happened).
            // This is the recommended pattern per Container.js JSDoc.
            const bStandalone = !sap.ui.require("sap/ushell/Container");
            this.setModel(new JSONModel({ isStandalone: bStandalone }), "app");

            if (bStandalone) {
                Log.warning(
                    "Running in standalone mode. Navigation features are not available."
                    + " Use sandbox.html for full FLP functionality.",
                    null, sComponent
                );
            }

            this.rootControlLoaded().then((oView) => {
                const oStartupData = this._extractStartupParameters(oStartupParameters);
                oView.setModel(new JSONModel(oStartupData), "startupParameters");
            });
        },

        /**
         * Flattens startup parameters into an array suitable for list binding.
         *
         * @param {object} oStartupParameters - Startup parameters from getComponentData().startupParameters
         * @returns {{parameters: Array<{key: string, value: string}>}} Model object with key/value pairs
         * @private
         */
        _extractStartupParameters: function (oStartupParameters) {
            return {
                parameters: Object.keys(oStartupParameters).map((sKey) => ({
                    key: sKey,
                    value: oStartupParameters[sKey].toString()
                }))
            };
        }
    });
});
