import { __assign, __awaiter, __generator, __read, __rest, __spread } from "tslib";
import { Limiter } from "@intelligentgraphics/ig.utilities";
import { createArticleManager as createBaseArticleManager } from "../ArticleManager";
export var createArticleManager = function (serviceManager, articleState, articleQuery, operationResultHandler, applicationState) {
    var baseArticleManager = createBaseArticleManager(serviceManager, articleState, articleQuery, operationResultHandler, applicationState);
    var articleCreationLimiter = new Limiter(1);
    var createArticleFromCatalogItem = function (_a) { return __awaiter(void 0, void 0, void 0, function () {
        var backend, articleNumber;
        var catalogItem = _a.catalogItem, sourceId = _a.source, passThrough = __rest(_a, ["catalogItem", "source"]);
        return __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(__assign(__assign({}, passThrough), { source: sourceId, articleNumber: articleNumber }))];
            }
        });
    }); };
    var enqueueCreateArticleFromArticleNumber = function (params) {
        articleCreationLimiter.clearQueue();
        return articleCreationLimiter.queue(function () {
            return createArticleFromArticleNumber(params);
        });
    };
    var createArticleFromArticleNumber = function (params) { return __awaiter(void 0, void 0, void 0, function () {
        var results, operator, currentMainArticle, _a, _b, _c, _d, _e, _f;
        return __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([__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([__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 __assign(__assign({}, baseArticleManager), { createArticleFromArticleNumber: enqueueCreateArticleFromArticleNumber, createArticleFromCatalogItem: createArticleFromCatalogItem,
        changeMainArticleProperties: changeMainArticleProperties,
        changeMainArticleProperty: changeMainArticleProperty,
        deleteMainArticle: deleteMainArticle });
};
