import { __read, __spread } from "tslib";
import { BackendRegistry } from "../Session";
import { session } from "./State";
import { articleState } from "./ArticleState";
import { wrapSessionMethod } from "./Util";
var normalizeInput = function (input) {
    if (Array.isArray(input)) {
        return input.map(function (entry) { return normalizeInput(entry); });
    }
    return typeof input === "object" ? input["comId"] : input;
};
var withNormalizedInput = function (func) { return function (input) {
    var args = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
    }
    var target = normalizeInput(input);
    return func.apply(void 0, __spread([target], args));
}; };
var addArticleFromCatalogItem = function (managerId, catalogItem, initialConfiguration, initialPlacement, data) {
    return session
        .createArticleFromCatalogItem({
        catalogItem: catalogItem,
        source: managerId,
        initialConfiguration: initialConfiguration,
        initialPlacement: initialPlacement,
        data: data,
    })
        .then(function (comId) { return ({
        comId: comId,
    }); });
};
var addArticle = function (managerId, articleNumber, initialConfiguration, initialPlacement, data) {
    return session
        .createArticleFromArticleNumber({
        articleNumber: articleNumber,
        source: managerId,
        initialConfiguration: initialConfiguration,
        initialPlacement: initialPlacement,
        data: data,
    })
        .then(function (comId) { return ({
        comId: comId,
    }); });
};
var changeProperty = withNormalizedInput(wrapSessionMethod("changeProperty"));
var changeProperties = withNormalizedInput(wrapSessionMethod("changeProperties"));
var removeArticles = withNormalizedInput(wrapSessionMethod("deleteArticles"));
var registerCatalogManager = function (sourceId, catalogManager, serverType) { return session.registerCatalogManager(sourceId, catalogManager); };
var registerProductDataManager = function (sourceId, productDataManager, serverType) {
    session.registerProductDataManager(sourceId, productDataManager);
};
var getArticlePlacement = withNormalizedInput(function (comId) {
    return session.getArticlePlacement(comId);
});
export var Dispatcher = {
    articleState: articleState,
    backendManager: {
        registerBackend: BackendRegistry.registerBackend,
        getBackendForServerType: BackendRegistry.getBackendForServerType,
        loadBackend: BackendRegistry.loadBackend,
    },
    notificationID: "IGD.Dispatcher",
    connectionManager: {
        registerConnection: wrapSessionMethod("registerConnection"),
        getConnection: wrapSessionMethod("getConnection"),
        getConnectionIds: wrapSessionMethod("getConnectionIds"),
        ensureConnection: wrapSessionMethod("ensureConnection"),
    },
    registerConnection: wrapSessionMethod("registerConnection"),
    getConnection: wrapSessionMethod("getConnection"),
    getConnectionIds: wrapSessionMethod("getConnectionIds"),
    ensureConnection: wrapSessionMethod("ensureConnection"),
    registerCatalogManager: registerCatalogManager,
    getCatalogManagerByRegId: wrapSessionMethod("getCatalogManagerForSourceId"),
    getCatalogManager: wrapSessionMethod("getCatalogManagerForComId"),
    registerProductDataManager: registerProductDataManager,
    getProductDataManagerByRegId: wrapSessionMethod("getProductDataManagerForSourceId"),
    getProductDataManager: wrapSessionMethod("getProductDataManagerForComId"),
    registerMultiArticles: wrapSessionMethod("registerMultiArticles"),
    getMultiArticleByRegId: wrapSessionMethod("getMultiArticlesForSourceId"),
    getMultiArticles: wrapSessionMethod("getMultiArticlesForComId"),
    setGfxId: wrapSessionMethod("setArticleGfxId"),
    getGfxId: wrapSessionMethod("getArticleGfxId"),
    addArticleFromCatalogItem: addArticleFromCatalogItem,
    addArticle: addArticle,
    getArticlePlacement: getArticlePlacement,
    changeProperty: changeProperty,
    changeProperties: changeProperties,
    removeArticles: removeArticles,
    setArticlePlacement: wrapSessionMethod("setArticlePlacement"),
};
