import { __read, __values } from "tslib";
export var createServiceManager = function (backendManager) {
    var sources = new Map();
    var comIdMapping = new Map();
    var createSourceItemGetter = function (name) { return function (sourceId) { return getSourceItem(sourceId, name); }; };
    var getSourceItem = function (sourceId, name) {
        if (!sources.has(sourceId)) {
            return undefined;
        }
        var sourceData = sources.get(sourceId);
        return sourceData[name];
    };
    var createSourceItemByComIdGetter = function (name) { return function (comId) {
        if (!comIdMapping.has(comId)) {
            return undefined;
        }
        var sourceId = comIdMapping.get(comId);
        return getSourceItem(sourceId, name);
    }; };
    var createSourceItemSetter = function (name) { return function (sourceId, item) {
        var _a;
        if (getSourceItem(sourceId, name) !== undefined) {
            return;
        }
        var sourceData = sources.get(sourceId);
        if (sourceData !== undefined) {
            sourceData[name] = item;
            return;
        }
        sourceData = (_a = {
                productDataManager: undefined,
                catalogManager: undefined,
                multiArticles: undefined,
                serverType: undefined,
                operator: undefined,
                configurationService: undefined
            },
            _a[name] = item,
            _a);
        sources.set(sourceId, sourceData);
    }; };
    var setArticleSource = function (comId, sourceId) {
        return comIdMapping.set(comId, sourceId);
    };
    var deleteArticleSource = function (comId) { return comIdMapping.delete(comId); };
    var getSourceBackend = function (sourceId) {
        var serverType = getSourceItem(sourceId, "serverType");
        if (serverType === undefined) {
            return undefined;
        }
        return backendManager.getBackendForServerType(serverType);
    };
    var getOperatorForComId = function (comId) {
        var sourceId = comIdMapping.get(comId);
        return getOperatorForSourceId(sourceId);
    };
    var getOperatorForSourceId = function (sourceId) {
        var operator = getSourceItem(sourceId, "operator");
        if (operator === undefined) {
            throw new Error("No operator has been registered for sourceId \"" + sourceId + "\"");
        }
        return operator;
    };
    var getRegisteredSourceIds = function () { return Array.from(sources.keys()); };
    var getRegisteredSourceIdsForServerType = function (serverType) {
        var e_1, _a;
        var sourceIds = [];
        try {
            for (var sources_1 = __values(sources), sources_1_1 = sources_1.next(); !sources_1_1.done; sources_1_1 = sources_1.next()) {
                var _b = __read(sources_1_1.value, 2), sourceId = _b[0], sourceData = _b[1];
                if (sourceData.serverType === serverType) {
                    sourceIds.push(sourceId);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (sources_1_1 && !sources_1_1.done && (_a = sources_1.return)) _a.call(sources_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return sourceIds;
    };
    return {
        getRegisteredSourceIds: getRegisteredSourceIds,
        getRegisteredSourceIdsForServerType: getRegisteredSourceIdsForServerType,
        registerBackend: backendManager.registerBackend,
        loadBackend: backendManager.loadBackend,
        getBackendForServerType: backendManager.getBackendForServerType,
        deleteArticleSource: deleteArticleSource,
        setArticleSource: setArticleSource,
        setSourceServerType: createSourceItemSetter("serverType"),
        getSourceServerType: createSourceItemGetter("serverType"),
        getSourceBackend: getSourceBackend,
        registerCatalogManager: createSourceItemSetter("catalogManager"),
        getCatalogManagerForSourceId: createSourceItemGetter("catalogManager"),
        getCatalogManagerForComId: createSourceItemByComIdGetter("catalogManager"),
        registerProductDataManager: createSourceItemSetter("productDataManager"),
        getProductDataManagerForSourceId: createSourceItemGetter("productDataManager"),
        getProductDataManagerForComId: createSourceItemByComIdGetter("productDataManager"),
        registerMultiArticles: createSourceItemSetter("multiArticles"),
        getMultiArticlesForSourceId: createSourceItemGetter("multiArticles"),
        getMultiArticlesForComId: createSourceItemByComIdGetter("multiArticles"),
        registerOperator: createSourceItemSetter("operator"),
        getOperatorForComId: getOperatorForComId,
        getOperatorForSourceId: getOperatorForSourceId,
        getConfigurationServiceForComId: createSourceItemByComIdGetter("configurationService"),
        getConfigurationServiceForSourceId: createSourceItemGetter("configurationService"),
        registerConfigurationService: createSourceItemSetter("configurationService"),
    };
};
