"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ArticleCategories_1 = require("../ArticleCategories");
var Cloning_1 = require("./Cloning");
var Configuration_1 = require("./Configuration");
var Deletion_1 = require("./Deletion");
var Creation_1 = require("./Creation");
var Restoring_1 = require("./Restoring");
var Util_1 = require("./Util");
exports.createPDMOperator = function (sourceId, productDataManager, articleQuery, scheduler) {
    var sessionOperator = productDataManager.is("SessionProvider")
        ? createSessionOperator(sourceId, productDataManager, articleQuery)
        : undefined;
    var is = function (name) {
        if (name === "sessionOperator") {
            return sessionOperator !== undefined;
        }
        return false;
    };
    var addArticle = function (params) {
        return Creation_1.executeCreateArticleOperation(params, articleQuery, productDataManager).then(buildOperationResults);
    };
    var configureArticles = function (comIds, changes) {
        return Configuration_1.executeConfigureArticlesOperation(comIds, changes, articleQuery, scheduler, productDataManager).then(buildOperationResults);
    };
    var deleteArticle = function (comId) {
        return Deletion_1.executeDeleteOperation(comId, articleQuery, scheduler, productDataManager).then(buildOperationResults);
    };
    var cloneArticle = function (params) {
        return Cloning_1.executeCloneArticleOperation(params, articleQuery, scheduler, productDataManager).then(buildOperationResults);
    };
    var restoreArticle = function (article) {
        return Restoring_1.executeRestoreOperation(article, articleQuery, productDataManager).then(buildOperationResults);
    };
    var buildOperationResults = function (results) {
        return exports.createPDMOperationResults(articleQuery, productDataManager, sourceId, results);
    };
    var executeArticlesOperation = function (operation) {
        return operation().then(function (operationResults) {
            return exports.createPDMOperationResults(articleQuery, productDataManager, sourceId, operationResults);
        });
    };
    var getGfxRepresentation = function (comId, options) { return productDataManager.getGfxRepresentation(comId, options); };
    return tslib_1.__assign({ is: is,
        addArticle: addArticle,
        configureArticles: configureArticles,
        deleteArticle: deleteArticle,
        cloneArticle: cloneArticle,
        restoreArticle: restoreArticle,
        getGfxRepresentation: getGfxRepresentation,
        executeArticlesOperation: executeArticlesOperation }, sessionOperator);
};
var createSessionOperator = function (sourceId, productDataManager, articleQuery) {
    var save = function () { return productDataManager.save(); };
    var restore = function (data) {
        return Restoring_1.executeRestoreSessionProviderOperation(data, productDataManager).then(function (results) {
            return exports.createPDMOperationResults(articleQuery, productDataManager, sourceId, results);
        });
    };
    return {
        save: save,
        restore: restore,
    };
};
exports.createPDMOperationResults = function (articleQuery, productDataManager, sourceId, results) {
    return Promise.all(results.map(function (result) {
        return createOperationResult(articleQuery, productDataManager, sourceId, result);
    })).then(function (results) {
        return results.filter(function (result) { return result !== false; });
    });
};
var createOperationResult = function (articleQuery, productDataManager, sourceId, operationResult) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    return tslib_1.__generator(this, function (_a) {
        if (operationResult.type === "delete") {
            return [2 /*return*/, {
                    comId: operationResult.comId,
                    type: "delete",
                }];
        }
        if (operationResult.type === "configure") {
            return [2 /*return*/, createConfigureOperationResult(articleQuery, productDataManager, operationResult)];
        }
        return [2 /*return*/, createCreateOperationResult(articleQuery, productDataManager, sourceId, operationResult)];
    });
}); };
var createCreateOperationResult = function (articleQuery, productDataManager, sourceId, result) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var _a, configuration, articleId, dataCategories, properties, articleType, sourceData, info, children, sourcePlacement;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0: return [4 /*yield*/, Promise.all([
                    productDataManager.getArticleConfiguration(result.comId),
                    productDataManager.getArticleID(result.comId),
                    result.categories !== undefined
                        ? Promise.resolve(result.categories)
                        : productDataManager.getArticleCategories(result.comId),
                    productDataManager.getArticlePropertyList(result.comId),
                    result.articleType !== undefined
                        ? Promise.resolve(result.articleType)
                        : productDataManager.getArticleType(result.comId),
                    productDataManager.getArticleCustomData === undefined
                        ? Promise.resolve(undefined)
                        : productDataManager.getArticleCustomData(result.comId),
                    Util_1.fetchArticleInfo(productDataManager, result.comId, undefined),
                    result.children !== undefined
                        ? Promise.resolve(result.children)
                        : productDataManager.getSubArticleIDs(result.comId),
                    productDataManager.getArticlePlacement(result.comId),
                ])];
            case 1:
                _a = tslib_1.__read.apply(void 0, [_b.sent(), 9]), configuration = _a[0], articleId = _a[1], dataCategories = _a[2], properties = _a[3], articleType = _a[4], sourceData = _a[5], info = _a[6], children = _a[7], sourcePlacement = _a[8];
                return [2 /*return*/, {
                        type: "create",
                        articleType: articleType,
                        comId: result.comId,
                        source: sourceId,
                        categories: tslib_1.__assign(tslib_1.__assign({}, ArticleCategories_1.createCategories()), dataCategories),
                        properties: properties,
                        sourceData: sourceData || {},
                        articleNumber: result.articleNumber !== undefined
                            ? result.articleNumber
                            : {
                                baseArticleNumber: articleId,
                            },
                        placement: result.placement,
                        sourcePlacement: result.placement === undefined ? sourcePlacement : undefined,
                        parent: result.parent,
                        children: children,
                        info: info,
                        configuration: configuration,
                        articleId: articleId,
                        data: result.data,
                        gfxId: result.gfxId,
                    }];
        }
    });
}); };
var createConfigureOperationResult = function (articleQuery, productDataManager, operation) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var article, configuration, changes, _a, articleData, info;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                article = articleQuery.getArticleByComId(operation.comId);
                return [4 /*yield*/, productDataManager.getArticleConfiguration(operation.comId)];
            case 1:
                configuration = _b.sent();
                changes = Object.getOwnPropertyNames(configuration).filter(function (prop) { return article.configuration[prop] !== configuration[prop]; });
                if (changes.length === 0) {
                    return [2 /*return*/, false];
                }
                return [4 /*yield*/, Promise.all([
                        Util_1.fetchArticleData(productDataManager, operation.comId),
                        Util_1.fetchArticleInfo(productDataManager, operation.comId, article.info),
                    ])];
            case 2:
                _a = tslib_1.__read.apply(void 0, [_b.sent(), 2]), articleData = _a[0], info = _a[1];
                return [2 /*return*/, {
                        type: "change",
                        comId: operation.comId,
                        configuration: configuration,
                        categories: tslib_1.__assign(tslib_1.__assign({}, ArticleCategories_1.createCategories()), articleData.categories),
                        sourceData: articleData.sourceData,
                        properties: articleData.properties,
                        info: info,
                        articleId: articleData.articleId,
                        children: articleData.subArticles,
                    }];
        }
    });
}); };
