import { BackendRegistry } from "../Session";
import { session } from "./State";
import { articleState } from "./ArticleState";
import { wrapSessionMethod } from "./Util";
const normalizeInput = (input) => {
    if (Array.isArray(input)) {
        return input.map(entry => normalizeInput(entry));
    }
    return typeof input === "object" ? input["comId"] : input;
};
const withNormalizedInput = (func) => (input, ...args) => {
    const target = normalizeInput(input);
    return func(target, ...args);
};
const addArticleFromCatalogItem = (managerId, catalogItem, initialConfiguration, initialPlacement, data) => session
    .createArticleFromCatalogItem({
    catalogItem,
    source: managerId,
    initialConfiguration,
    initialPlacement,
    data,
})
    .then(comId => ({
    comId,
}));
const addArticle = (managerId, articleNumber, initialConfiguration, initialPlacement, data) => session
    .createArticleFromArticleNumber({
    articleNumber,
    source: managerId,
    initialConfiguration,
    initialPlacement,
    data,
})
    .then(comId => ({
    comId,
}));
const changeProperty = withNormalizedInput(wrapSessionMethod("changeProperty"));
const changeProperties = withNormalizedInput(wrapSessionMethod("changeProperties"));
const removeArticles = withNormalizedInput(wrapSessionMethod("deleteArticles"));
const registerCatalogManager = (sourceId, catalogManager, serverType) => session.registerCatalogManager(sourceId, catalogManager);
const registerProductDataManager = (sourceId, productDataManager, serverType) => {
    session.registerProductDataManager(sourceId, productDataManager);
};
const getArticlePlacement = withNormalizedInput((comId) => session.getArticlePlacement(comId));
export const Dispatcher = {
    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,
    getCatalogManagerByRegId: wrapSessionMethod("getCatalogManagerForSourceId"),
    getCatalogManager: wrapSessionMethod("getCatalogManagerForComId"),
    registerProductDataManager,
    getProductDataManagerByRegId: wrapSessionMethod("getProductDataManagerForSourceId"),
    getProductDataManager: wrapSessionMethod("getProductDataManagerForComId"),
    registerMultiArticles: wrapSessionMethod("registerMultiArticles"),
    getMultiArticleByRegId: wrapSessionMethod("getMultiArticlesForSourceId"),
    getMultiArticles: wrapSessionMethod("getMultiArticlesForComId"),
    setGfxId: wrapSessionMethod("setArticleGfxId"),
    getGfxId: wrapSessionMethod("getArticleGfxId"),
    addArticleFromCatalogItem,
    addArticle,
    getArticlePlacement,
    changeProperty,
    changeProperties,
    removeArticles,
    setArticlePlacement: wrapSessionMethod("setArticlePlacement"),
};
