Source: widget/help/project/SEN2.js

(function() {
    /**
     * this backend connector is able to handle EXT project content for an EXT model configuration
     *
     * for TOURS
     * - downloads RW || RO
     *
     * for HELP
     * - downloads RO && RW
     * - merges both projects
     *
     * @augments Help4.widget.help.project.SEN
     */
    Help4.widget.help.project.SEN2 = class extends Help4.widget.help.project.SEN {
        /**
         * all pure RO projects will have catalogueType 'SEN' or 'UACP' and will never come here
         * this function will only be called for pure RW projects or in EXT case
         *
         * tours: just take the RW project
         * help: merge RO + RW
         *
         * @override
         * @param {string} projectId - project ID
         * @param {Help4.typedef.SystemConfiguration} config - the system configuration
         * @param {Help4.widget.help.Data} data
         * @returns {Promise<Help4.widget.help.Projects|null>}
         */
        static async load(projectId, config, data) {
            const {wpb, sen, ext} = Help4.SERVICE_LAYER;
            const {serviceLayer, rwModel, serviceUrl2} = config.help;
            if (!serviceUrl2 || serviceLayer !== ext || rwModel !== wpb && rwModel !== sen) return null;

            const {core: {editor}} = config;
            /** @type {Help4.widget.help.project.SEN.ProjectUrlConfig} */
            const {serverBaseUrl, pubUrl, headUrl} = this._getUrls(projectId, config, serviceUrl2);

            /** @type {Help4.widget.help.Projects} */
            const projects = {pub: null};
            const {SEN} = Help4.widget.companionCore;
            if (editor) {
                // editor: load PUB and HEAD
                /** @type {?Help4.widget.companionCore.SEN.ProjectResponse} */
                const response = await SEN.doProjectRequest(config, serviceUrl2, {serverBaseUrl, pubUrl, headUrl});
                const {pub, head, tagInfo} = response || {};
                if (!tagInfo) return null;

                if (pub) {
                    /** @type {?Help4.widget.companionCore.SEN.PubProjectResponse} */
                    const assembled = this._assemblePub(pub, tagInfo, config);
                    const isExtension = _isExtension(projectId, 'pub', assembled.contextType, data);
                    if (assembled) projects.pub = this._parse(assembled, config, 'SEN2', isExtension);
                }
                if (head) {
                    /** @type {?Help4.widget.companionCore.SEN.PubProjectResponse} */
                    const assembled = this._assembleHead(head, tagInfo, config);
                    const isExtension = _isExtension(projectId, 'head', assembled.contextType, data);
                    if (assembled) projects.head = this._parse(assembled, config, 'SEN2', isExtension);
                }
            } else {
                // non-editor: load PUB only
                /** @type {?Help4.widget.companionCore.SEN.PubProjectResponse} */
                const response = await SEN.doProjectRequest(config, serviceUrl2, {serverBaseUrl, pubUrl});
                if (response) {
                    const isExtension = _isExtension(projectId, 'pub', response.contextType, data);
                    projects.pub = this._parse(response, config, 'SEN2', isExtension);
                }
            }

            return projects;
        }
    }

    /**
     * @memberof Help4.widget.help.project.SEN2
     * @private
     * @param {string} projectId
     * @param {Help4.widget.help.CatalogueKeys} catalogueKey
     * @param {Help4.widget.help.ContextTypes} contextType
     * @param {Help4.widget.help.Data} data
     * @returns {boolean}
     */
    function _isExtension(projectId, catalogueKey, contextType, data) {
        if (contextType === 'TOUR') return false;  // XRAY-1550; tours are full override and not extensions

        const {selected = {}} = data.catalogues[catalogueKey] || {};
        for (const screenId of Object.keys(selected)) {
            /** @type {Help4.widget.help.CatalogueSelection[]} */
            const selection = selected[screenId];
            const my = selection.find(({id, id2}) => projectId === id || projectId === id2);
            if (my) return !!my.id2;  // extension exists in case id2 is set
        }
        return false;  // not extended or unable to detect the extension
    }
})();