Source: media/data/GalleryConfigLoader.js

import { IndexBinder } from "../../map/importer/binder/IndexBinder.js";
import { ServiceDataRequest } from "./utils.js";
import { MediaGalleryQueryParameter } from "../grid/MediaGalleryQueryParameter.js";
export { GalleryConfigLoader }

/**
 * Class representing the loading of the gallery configuration.
 */
class GalleryConfigLoader {
    /**
     * Create the gallery configuration loading object.
     */
    constructor() {
        this.catalogConfigDict;
        this.galleryConfigDict;
        this.queryConfigDict;
    }

    /**
     * Return the service configuration dictionary, regardless of whether it was loaded via catalogue or somewhere else (future).
     */
    get serviceConfigDict() {
        const serviceConfigDict = {}
        if (this.catalogConfigDict)
            serviceConfigDict['catalogConfigDict'] = this.catalogConfigDict
        return serviceConfigDict
    }

    /**
     * Load the entire gallery configuration based on an identifier.
     * This identifier is known to a catalog response.
     * With it, the gallery service configuration can be loaded from the catalog.
     * Furthermore, it contains the URL to the gallery.config.json file, that is finally loaded too.
     * @param {*} sourceID - Identifier for the gallery source data.
     * @returns Promise with the gallery configuration.
     */
    loadConfigData(sourceID) {
        this._loadQueryConfig();
        sourceID = sourceID || this.queryConfigDict['sourceID'];
        return this._loadConfigFromCatalog(sourceID).then((catalogConfig) => {
            const caseFallback = catalogConfig == null;
            if (caseFallback) {
                return Promise.reject('Info is not in catalog')
            }
            return catalogConfig
        }).catch((e) => {
            const message = 'Catalog does not provide data'
            console.error(`${message}:`, e ? e : 'Error when loading catalog');
            throw new Error(message);
        }).then((config) => {
            const id = Object.keys(config)[0];
            const galleryConfigUrlCatalog = config[id]?.['galleryConfig']?.['url'];
            const galleryConfigUrl = galleryConfigUrlCatalog;
            return this._loadGalleryConfig(galleryConfigUrl)
        }).catch((e) => {
            const message = 'Gallery configuration cannot be loaded'
            console.error(`${message}:`, e ? e : 'Error when loading gallery configuration file');
            throw new Error(message);
        })
    }

    _checkForEmptyResponse(config) {
        if (config.constructor == Object) {
            return Object.keys(config).length === 0
        }
        else if (config.constructor == Array) {
            return config.length === 0
        }
        return null
    }

    _loadConfigFromCatalog(idCatalogLayer) {
        const binder = new IndexBinder();
        return binder.fetchLayers({ query: `(uniqueId:"${idCatalogLayer}")`, hits: 10 }).then((catalogConfig) => {
            this.catalogConfigDict = this._checkForEmptyResponse(catalogConfig) ? null : catalogConfig
            return this.catalogConfigDict
        })
    }

    _loadQueryConfig() {
        const GalleryQueryParameter = new MediaGalleryQueryParameter();
        this.queryConfigDict = GalleryQueryParameter.queryParameterDict;
    }

    _loadGalleryConfig(galleryConfigUrl) {
        return ServiceDataRequest.requestJSON(decodeURIComponent(galleryConfigUrl)).then((data) => {
            this.galleryConfigDict = data;
            return data
        })
    }
}