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

/**
 * @fileOverview Fiori Launchpad Sandbox Startup.
 *
 * This module contains the core startup logic for the Fiori Launchpad Sandbox environment.
 * It initializes the sandbox by:
 * 1. Executing an optional pre-FLP-start hook module
 * 2. Applying the ushell configuration
 * 3. Initializing the ushell Container with CDM adapter
 * 4. Creating and rendering the Fiori 2.0 shell renderer
 * 5. Displaying a system info bar indicating sandbox mode
 *
 * The sandbox is designed for local application development and testing,
 * providing a fully functional FLP environment without requiring a backend system.
 *
 * @version 1.147.1
 * @since 1.147
 */
sap.ui.define([
    "sap/base/config",
    "sap/ushell/iconfonts",
    "sap/ushell/sandbox/ConfigurationProvider",
    "sap/ushell/ui/shell/SysInfoBar"
], (
    BaseConfig,
    Iconfonts,
    SandboxConfigurationProvider,
    SysInfoBar
) => {
    "use strict";

    /**
     * Runs the Fiori Launchpad Sandbox.
     *
     * Orchestrates the complete sandbox startup sequence including configuration,
     * container initialization, and renderer creation.
     *
     * @returns {Promise<undefined>} A promise resolving when the sandbox is fully started
     * @since 1.147
     * @private
     */
    async function run () {
        // Check for optional pre-FLP-start hook module
        const sBeforeFlpStartModule = BaseConfig.get({
            name: "sapUshellSandboxBeforeFlpStart",
            type: BaseConfig.Type.String,
            freeze: true
        });

        // Execute the beforeFlpStart hook if configured
        if (sBeforeFlpStartModule) {
            const sModule = /^module:((?:[_$.\-a-zA-Z0-9]+\/)*[_$.\-a-zA-Z0-9]+)$/.exec(sBeforeFlpStartModule)[1];
            await new Promise((res, rej) => {
                sap.ui.require([sModule], (beforeFlpStart) => {
                    res(beforeFlpStart?.execute?.());
                }, rej);
            });
        }

        // Apply ushell config after beforeFlpStart hook has been executed
        // to ensure no direct ushell config has been set before sandbox start
        await SandboxConfigurationProvider.applyUshellConfig();

        // Require ushell dependencies lazily since they have transitive dependencies to ushell-config
        // which must be modified before loading
        await new Promise((res, rej) => {
            sap.ui.require(["sap/ushell/renderer/ShellLayout", "sap/ushell/Container"], async (ShellLayout, Container) => {
                // Initialize the ushell Container with CDM (Common Data Model) adapter
                await Container.init("cdm");

                // Register Fiori 2.0 icon fonts
                Iconfonts.registerFiori2IconFont();

                // Create the Fiori 2.0 renderer
                const oContent = await Container.createRendererInternal("fiori2");

                // Ensure 'canvas' div exists as child of body, create if not present
                if (!document.querySelector("body > div#canvas")) {
                    const oCanvasDiv = document.createElement("div");
                    oCanvasDiv.id = "canvas";
                    document.body.appendChild(oCanvasDiv);
                }

                // Place the renderer content in the shell's root view area
                oContent.placeAt(ShellLayout.ShellArea.RendererRootView);

                // Create and place the system info bar to indicate sandbox mode
                const sSytemInfoArea = "systemInfo-shellArea";
                const sSystemInfoHtml = `<div id='${sSytemInfoArea}'></div>`;
                const oShellHeaderShellArea = document.getElementById(ShellLayout.ShellArea.ShellHeader);

                oShellHeaderShellArea.insertAdjacentHTML("beforebegin", sSystemInfoHtml);

                new SysInfoBar({
                    icon: "sap-icon://technical-object",
                    text: "SAP Fiori Launchpad Sandbox",
                    subText: "For Testing Purposes Only!",
                    color: "orange"
                }).placeAt(sSytemInfoArea);

                res(ShellLayout, Container);
            }, rej);
        });
    }

    /**
     * Fiori Launchpad Sandbox startup module.
     *
     * @namespace sap.ushell.sandbox.StartSandbox
     * @alias module:sap/ushell/sandbox/StartSandbox
     * @since 1.147
     * @private
     */
    return {
        run
    };
});
