Source: engine/ur/WTA.js

(function() {
    /** Update texts handling for UR harmonization */
    Help4.engine.ur.WTA = class {
        /**
         * @param {Help4.engine.ur.UrHarmonizationEngine} engine
         * @param {Object} data
         */
        static handle(engine, data) {
            // data should contain url (WDA) or helpId (GUI API v1), or backendHelpKey and backendHelpCDSQuery (GUI API v2)
            // (UI5) expecting {backendHelpCDSQuery: 'SDOC_HLP_CE_UI5_AH', backendHelpKey: {tcode: string}}
            if (!data.url) {
                const {JSON} = Help4;
                const {QUERIES: {GUI_AH}} = engine.constructor;

                // convert query param to string if needed
                const {backendHelpKey, helpId} = data;
                if (typeof backendHelpKey === 'object') {
                    data.backendHelpKey = JSON.stringify(backendHelpKey);
                } else if (typeof helpId === 'object') {
                    data.backendHelpKey = JSON.stringify(helpId);
                }
                data.backendHelpCDSQuery ||= GUI_AH;
            }

            _getHelpUrl(engine, data)
            .then(() => {
                // wta tile only updates on navigation, so existing tile can be discarded
                engine._wtaTile = _assembleWtaTile(engine);
                engine._sendUpdateNotification({structural: true, moved: false});
            });
        }

        /**
         * Wrapper for handle() in UI5 case, extracting transaction code first
         * @param {Help4.engine.ur.UrHarmonizationEngine} engine
         */
        static handleUI5(engine) {
            const shell = Help4.getShell();
            const service = shell.getUI5Service('AppLifeCycle');
            const app = service?.getCurrentApplication() || {};

            const {applicationType, homePage} = app;
            const {UI5} = Help4.engine.ur.Connection.APP_TYPE;
            if (homePage || applicationType !== UI5) return;

            app.getInfo?.(['appFrameworkId', 'appId'])
            .then(({appFrameworkId, appId: tcode}) => {
                if (appFrameworkId !== UI5 || !tcode) return;

                this.handle(engine, {
                    backendHelpCDSQuery: engine.constructor.QUERIES.UI5_AH,
                    backendHelpKey: {tcode}
                });
            });
        }
    }

    /**
     * Get url directly (WDA) or from the backend (GUI)
     * @private
     * @param {Help4.engine.ur.UrHarmonizationEngine} engine
     * @param {Object} hotspot
     * @param {string} [hotspot.url] - direct url to use, no query (WDA)
     * @param {string} [hotspot.backendHelpCDSQuery] - query to use (GUI | UI5)
     * @param {Object} [hotspot.backendHelpKey] - item to query (GUI | UI5)
     * @returns {Promise<boolean>}
     */
    async function _getHelpUrl(engine, {url, backendHelpCDSQuery, backendHelpKey}) {
        // we don't need to wrap in ctx.cfg_show here, this is handled in the link tile
        // no url sanity test, we trust source (iframe)
        const {_mock} = engine;
        if (_mock) url = 'https://www.sap.com';

        // WDA
        if (url) {
            engine._wtaUrl = url;
            return true;
        }

        // GUI | UI5
        if (backendHelpCDSQuery && backendHelpKey) {
            /** @type {?Help4.engine.ur.UrHarmonizationEngine.UrLinkResponse[] | ?Help4.engine.ur.UrHarmonizationEngine.ErrorResponse[]} */
            const response = await Help4.ajax.UrHarmonization.requestMultipartContent([{_type: backendHelpCDSQuery, _urId: backendHelpKey}])

            engine._log?.('CMP: received help url response', undefined, ()=>response);
            if (!Help4.isArray(response)) return false;

            const {error, help_link} = response[0] || {};
            if (error) return false;

            engine._wtaUrl = help_link || null;
            engine._log?.('CMP: help_link from response', engine._wtaUrl);
            return true;
        }

        engine._wtaUrl = null;
        return false;
    }

    /**
     * @private
     * @param {Help4.engine.ur.UrHarmonizationEngine} engine
     * @returns {?Help4.widget.help.ProjectTile}
     */
    function _assembleWtaTile(engine) {
        const {_wtaUrl: linkTo} = engine;
        if (!linkTo) return null;

        const id = Help4.createId();
        const wta = Help4.cloneObject(engine._getDefaults());
        let title = Help4.Localization.getText('label.tile.icon.type.whatsthisapp');
        if (Help4.Feature.UrDebugging) title = '🚧' + title;

        return Help4.extendObject(wta, {
            _dataId: id,
            _special: 'wta',
            _standalone: true,
            hotspotAnchor: null,
            id,
            linkTo,
            loio: id,
            showAsButton: true,
            title,
            type: 'link'
        });
    }
})();