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

/**
 * @fileoverview This file contains the StatefulContainerV1Handler class.
 * It provides utility functions for WebGui stateful container V1.
 */
sap.ui.define([
    "sap/ushell/utils",
    "sap/ushell/Container"
], (
    ushellUtils,
    Container
) => {
    "use strict";

    /**
     * @alias sap.ushell.appIntegration.contracts.StatefulContainerV1Handler
     * @class
     * @classdesc Handles stateful container operations for V1 protocol (WebGui).
     * Can only be used for {@link sap.ushell.appIntegration.IframeApplicationContainer} which implement the StatefulContainerV1 contract.
     *
     * @private
     */
    class StatefulContainerV1Handler {
        /**
         * Creates an application in a V1 stateful container.
         * @param {sap.ushell.appIntegration.IframeApplicationContainer} oApplicationContainer The application container.
         * @param {string} sStorageAppId The storage application ID.
         * @param {object} oResolvedHashFragment The resolved hash fragment.
         * @returns {Promise} Resolves when the application is created.
         *
         * @private
         */
        async createApp (oApplicationContainer, sStorageAppId, oResolvedHashFragment) {
            oApplicationContainer.setCurrentAppId(sStorageAppId);
            oApplicationContainer.setCurrentAppUrl(oResolvedHashFragment.url);
            oApplicationContainer.setCurrentAppTargetResolution(oResolvedHashFragment);

            const oPostParams = await this.#createPostParams(oApplicationContainer, oResolvedHashFragment);
            await oApplicationContainer.sendRequest("sap.its.startService", oPostParams, true);
        }

        /**
         * Creates the post message parameters for application creation in a V1 stateful container.
         * @param {sap.ushell.appIntegration.IframeApplicationContainer} oApplicationContainer The application container.
         * @param {object} oResolvedHashFragment The resolved hash fragment.
         * @returns {Promise<object>} The post message parameters.
         *
         * @private
         */
        async #createPostParams (oApplicationContainer, oResolvedHashFragment) {
            let sUrl = oResolvedHashFragment.url;

            sUrl = await ushellUtils.appendSapShellParam(sUrl);
            sUrl = ushellUtils.filterOutParamsFromLegacyAppURL(sUrl);

            const oPostParams = {
                url: sUrl
            };

            if (oApplicationContainer.getIframeWithPost()) {
                const oAppStatesInfo = ushellUtils.getParamKeys(sUrl);
                const oFLPParams = {};

                if (oAppStatesInfo.aAppStateNamesArray.length > 0) {
                    const Navigation = await Container.getServiceAsync("Navigation");
                    try {
                        const aDataArray = await Navigation.getAppStateData(oAppStatesInfo.aAppStateKeysArray);
                        oAppStatesInfo.aAppStateNamesArray.forEach((sParamName, iIndex) => {
                            if (aDataArray[iIndex]) {
                                oFLPParams[sParamName] = aDataArray[iIndex];
                            }
                        });
                    } catch {
                        // fail silently
                    }
                }

                oPostParams["sap-flp-params"] = {
                    ...oFLPParams,
                    "sap-flp-url": Container.getFLPUrl(true),
                    "system-alias": oApplicationContainer.getSystemAlias()
                };
            }

            return oPostParams;
        }

        /**
         * Destroys an application in a V1 stateful container.
         * @param {sap.ushell.appIntegration.IframeApplicationContainer} oApplicationContainer The application container.
         * @returns {Promise} Resolves when the application is destroyed.
         *
         * @private
         */
        async destroyApp (oApplicationContainer) {
            oApplicationContainer.setCurrentAppId("");
            await oApplicationContainer.sendRequest("sap.gui.triggerCloseSession", {}, false);
        }
    }

    return new StatefulContainerV1Handler();
});
