"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var ArticleManager_1 = require("../ArticleManager");
exports.createArticleManager = function (serviceManager, articleState, articleQuery, operationResultHandler, applicationState) {
    var baseArticleManager = ArticleManager_1.createArticleManager(serviceManager, articleState, articleQuery, operationResultHandler, applicationState);
    var articleCreationLimiter = new ig_utilities_1.Limiter(1);
    var createArticleFromCatalogItem = function (_a) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var backend, articleNumber;
        var catalogItem = _a.catalogItem, sourceId = _a.source, passThrough = tslib_1.__rest(_a, ["catalogItem", "source"]);
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, serviceManager.getSourceBackend(sourceId)];
                case 1:
                    backend = _b.sent();
                    if (backend === undefined) {
                        throw new Error("No backend has been registered for \"" + sourceId + "\", no article number can be created.");
                    }
                    articleNumber = backend.createArticleNumber(catalogItem);
                    return [2 /*return*/, enqueueCreateArticleFromArticleNumber(tslib_1.__assign(tslib_1.__assign({}, passThrough), { source: sourceId, articleNumber: articleNumber }))];
            }
        });
    }); };
    var enqueueCreateArticleFromArticleNumber = function (params) {
        articleCreationLimiter.clearQueue();
        return articleCreationLimiter.queue(function () {
            return createArticleFromArticleNumber(params);
        });
    };
    var createArticleFromArticleNumber = function (params) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var results, operator, currentMainArticle, _a, _b, _c, _d, _e, _f;
        return tslib_1.__generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    results = [];
                    operator = serviceManager.getOperatorForSourceId(params.source);
                    currentMainArticle = articleQuery.getMainArticle();
                    if (!(currentMainArticle !== undefined)) return [3 /*break*/, 2];
                    _b = (_a = results.push).apply;
                    _c = [results];
                    return [4 /*yield*/, operator.deleteArticle(currentMainArticle.comId)];
                case 1:
                    _b.apply(_a, _c.concat([tslib_1.__spread.apply(void 0, [(_g.sent())])]));
                    _g.label = 2;
                case 2:
                    _e = (_d = results.push).apply;
                    _f = [results];
                    return [4 /*yield*/, operator.addArticle(params)];
                case 3:
                    _e.apply(_d, _f.concat([tslib_1.__spread.apply(void 0, [(_g.sent())])]));
                    return [4 /*yield*/, operationResultHandler.processOperationResults(results)];
                case 4:
                    _g.sent();
                    return [2 /*return*/, articleQuery.getMainArticleComId()];
            }
        });
    }); };
    var changeMainArticleProperties = function (changes) {
        return baseArticleManager.changeArticleProperties(articleQuery.getMainArticleComId(), changes);
    };
    var changeMainArticleProperty = function (propertyKey, newValue) {
        return baseArticleManager.changeArticleProperty(articleQuery.getMainArticleComId(), propertyKey, newValue);
    };
    var deleteMainArticle = function () {
        var mainComId = articleQuery.getMainArticleComId();
        if (mainComId === undefined) {
            return Promise.resolve();
        }
        return baseArticleManager.deleteArticle(mainComId);
    };
    return tslib_1.__assign(tslib_1.__assign({}, baseArticleManager), { createArticleFromArticleNumber: enqueueCreateArticleFromArticleNumber, createArticleFromCatalogItem: createArticleFromCatalogItem,
        changeMainArticleProperties: changeMainArticleProperties,
        changeMainArticleProperty: changeMainArticleProperty,
        deleteMainArticle: deleteMainArticle });
};
