import { Package } from "@intelligentgraphics/ig.gfx.multiarticle";
import { createConfigurationServiceOperator } from "./ConfigurationServiceOperator";
import { createPDMOperator } from "./PDMOperator";
export var createSourceRegistry = function (serviceManager, articleQuery, scheduler) {
    var hasSource = function (sourceId) {
        return serviceManager.getProductDataManagerForSourceId(sourceId) !==
            undefined ||
            serviceManager.getConfigurationServiceForSourceId(sourceId) !==
                undefined;
    };
    var registerSource = function (params) {
        if (hasSource(params.sourceId)) {
            throw new Error("Can not overwrite already registered services for sourceId \"" + params.sourceId + "\"");
        }
        if (params.serverType !== undefined) {
            serviceManager.setSourceServerType(params.sourceId, params.serverType);
        }
        if (params.configurationService !== undefined) {
            registerConfigurationService(params.sourceId, params.configurationService);
        }
        else if (params.productDataManager !== undefined) {
            registerProductDataManager(params.sourceId, params.productDataManager);
        }
        else {
            throw new Error("Refusing to register a new source without an ProductDataManager or ConfigurationService");
        }
        if (params.catalogManager !== undefined) {
            serviceManager.registerCatalogManager(params.sourceId, params.catalogManager);
        }
        if (params.multiArticles !== undefined) {
            var multiArticles = void 0;
            if (params.multiArticles instanceof Package) {
                multiArticles = params.multiArticles;
            }
            else {
                multiArticles = new Package(params.multiArticles);
            }
            serviceManager.registerMultiArticles(params.sourceId, multiArticles);
        }
    };
    var registerProductDataManager = function (sourceId, productDataManager) {
        serviceManager.registerProductDataManager(sourceId, productDataManager);
        serviceManager.registerOperator(sourceId, createPDMOperator(sourceId, productDataManager, articleQuery, scheduler));
    };
    var registerConfigurationService = function (sourceId, configurationService) {
        serviceManager.registerConfigurationService(sourceId, configurationService);
        serviceManager.registerOperator(sourceId, createConfigurationServiceOperator(sourceId, configurationService, articleQuery, scheduler));
    };
    return {
        getBackendForServerType: serviceManager.getBackendForServerType,
        loadBackend: serviceManager.loadBackend,
        registerBackend: serviceManager.registerBackend,
        hasSource: hasSource,
        registerSource: registerSource,
        registerConfigurationService: registerConfigurationService,
        registerProductDataManager: registerProductDataManager,
    };
};
