"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ig_gfx_multiarticle_1 = require("@intelligentgraphics/ig.gfx.multiarticle");
var ConfigurationServiceOperator_1 = require("./ConfigurationServiceOperator");
var PDMOperator_1 = require("./PDMOperator");
exports.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 ig_gfx_multiarticle_1.Package) {
                multiArticles = params.multiArticles;
            }
            else {
                multiArticles = new ig_gfx_multiarticle_1.Package(params.multiArticles);
            }
            serviceManager.registerMultiArticles(params.sourceId, multiArticles);
        }
    };
    var registerProductDataManager = function (sourceId, productDataManager) {
        serviceManager.registerProductDataManager(sourceId, productDataManager);
        serviceManager.registerOperator(sourceId, PDMOperator_1.createPDMOperator(sourceId, productDataManager, articleQuery, scheduler));
    };
    var registerConfigurationService = function (sourceId, configurationService) {
        serviceManager.registerConfigurationService(sourceId, configurationService);
        serviceManager.registerOperator(sourceId, ConfigurationServiceOperator_1.createConfigurationServiceOperator(sourceId, configurationService, articleQuery, scheduler));
    };
    return {
        getBackendForServerType: serviceManager.getBackendForServerType,
        loadBackend: serviceManager.loadBackend,
        registerBackend: serviceManager.registerBackend,
        hasSource: hasSource,
        registerSource: registerSource,
        registerConfigurationService: registerConfigurationService,
        registerProductDataManager: registerProductDataManager,
    };
};
