// 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.147.1
 * @since 1.147
 */
sap.ui.define([
    "sap/ui/core/UIComponent",
    "sap/ui/model/json/JSONModel"
], (
    UIComponent,
    JSONModel
) => {
    "use strict";

    /**
     * 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 oStartupParameters = this.getComponentData().startupParameters;
            const sNavTo = oStartupParameters && oStartupParameters.navTo && oStartupParameters.navTo[0];
            if (sNavTo) {
                this.getRouter().navTo(sNavTo, null, true);
            }

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

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