"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Session_1 = require("../Session");
var State_1 = require("./State");
var ArticleState_1 = require("./ArticleState");
var Util_1 = require("./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, tslib_1.__spread([target], args));
}; };
var addArticleFromCatalogItem = function (managerId, catalogItem, initialConfiguration, initialPlacement, data) {
    return State_1.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 State_1.session
        .createArticleFromArticleNumber({
        articleNumber: articleNumber,
        source: managerId,
        initialConfiguration: initialConfiguration,
        initialPlacement: initialPlacement,
        data: data,
    })
        .then(function (comId) { return ({
        comId: comId,
    }); });
};
var changeProperty = withNormalizedInput(Util_1.wrapSessionMethod("changeProperty"));
var changeProperties = withNormalizedInput(Util_1.wrapSessionMethod("changeProperties"));
var removeArticles = withNormalizedInput(Util_1.wrapSessionMethod("deleteArticles"));
var registerCatalogManager = function (sourceId, catalogManager, serverType) { return State_1.session.registerCatalogManager(sourceId, catalogManager); };
var registerProductDataManager = function (sourceId, productDataManager, serverType) {
    State_1.session.registerProductDataManager(sourceId, productDataManager);
};
var getArticlePlacement = withNormalizedInput(function (comId) {
    return State_1.session.getArticlePlacement(comId);
});
exports.Dispatcher = {
    articleState: ArticleState_1.articleState,
    backendManager: {
        registerBackend: Session_1.BackendRegistry.registerBackend,
        getBackendForServerType: Session_1.BackendRegistry.getBackendForServerType,
        loadBackend: Session_1.BackendRegistry.loadBackend,
    },
    notificationID: "IGD.Dispatcher",
    connectionManager: {
        registerConnection: Util_1.wrapSessionMethod("registerConnection"),
        getConnection: Util_1.wrapSessionMethod("getConnection"),
        getConnectionIds: Util_1.wrapSessionMethod("getConnectionIds"),
        ensureConnection: Util_1.wrapSessionMethod("ensureConnection"),
    },
    registerConnection: Util_1.wrapSessionMethod("registerConnection"),
    getConnection: Util_1.wrapSessionMethod("getConnection"),
    getConnectionIds: Util_1.wrapSessionMethod("getConnectionIds"),
    ensureConnection: Util_1.wrapSessionMethod("ensureConnection"),
    registerCatalogManager: registerCatalogManager,
    getCatalogManagerByRegId: Util_1.wrapSessionMethod("getCatalogManagerForSourceId"),
    getCatalogManager: Util_1.wrapSessionMethod("getCatalogManagerForComId"),
    registerProductDataManager: registerProductDataManager,
    getProductDataManagerByRegId: Util_1.wrapSessionMethod("getProductDataManagerForSourceId"),
    getProductDataManager: Util_1.wrapSessionMethod("getProductDataManagerForComId"),
    registerMultiArticles: Util_1.wrapSessionMethod("registerMultiArticles"),
    getMultiArticleByRegId: Util_1.wrapSessionMethod("getMultiArticlesForSourceId"),
    getMultiArticles: Util_1.wrapSessionMethod("getMultiArticlesForComId"),
    setGfxId: Util_1.wrapSessionMethod("setArticleGfxId"),
    getGfxId: Util_1.wrapSessionMethod("getArticleGfxId"),
    addArticleFromCatalogItem: addArticleFromCatalogItem,
    addArticle: addArticle,
    getArticlePlacement: getArticlePlacement,
    changeProperty: changeProperty,
    changeProperties: changeProperties,
    removeArticles: removeArticles,
    setArticlePlacement: Util_1.wrapSessionMethod("setArticlePlacement"),
};
