import { __assign, __read, __spread } from "tslib";
import { generateId } from "@intelligentgraphics/ig.utilities";
export var createSessionWithLoadingReasons = function (session, reasonCollection) {
    var withLoadingReason = function (execute, generateReason) {
        if (generateReason === void 0) { generateReason = generateId; }
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            var reason = generateReason.apply(void 0, __spread(args));
            reasonCollection.addLoadingReason(reason);
            try {
                var callResult = execute.apply(void 0, __spread(args));
                if (typeof callResult === "object" &&
                    typeof callResult["then"] === "function") {
                    var res = callResult;
                    res.finally(function () { return reasonCollection.removeLoadingReason(reason); });
                    return callResult;
                }
                reasonCollection.removeLoadingReason(reason);
                return callResult;
            }
            catch (err) {
                reasonCollection.removeLoadingReason(reason);
                throw err;
            }
        };
    };
    return __assign(__assign({}, session), { createArticleFromArticleNumber: withLoadingReason(session.createArticleFromArticleNumber, function () { return "article_creation_" + generateId(); }), createArticleFromCatalogItem: withLoadingReason(session.createArticleFromCatalogItem, function () { return "article_creation_" + generateId(); }), changeArticleProperties: withLoadingReason(session.changeArticleProperties, function (comId) { return "article_configuration_" + comId; }), changeArticleProperty: withLoadingReason(session.changeArticleProperty, function (comId) { return "article_configuration_" + comId; }), changeProperties: withLoadingReason(session.changeProperties, function (comIds) { return "article_configuration_" + comIds.join("&"); }), changeProperty: withLoadingReason(session.changeProperty, function (comIds) { return "article_configuration_" + comIds.join("&"); }), cloneArticle: withLoadingReason(session.cloneArticle, function () { return "article_creation_" + generateId(); }), restoreFromIGXB: withLoadingReason(session.restoreFromIGXB, function () { return "restoring_igxb"; }), restoreFromArticleList: withLoadingReason(session.restoreFromArticleList, function () { return "restoring_articlelist"; }), restoreFromServices: withLoadingReason(session.restoreFromServices, function () { return "restoring_from_services"; }), saveAsIGXB: withLoadingReason(session.saveAsIGXB, function () { return "saving_igxb"; }), saveAsArticleList: withLoadingReason(session.saveAsArticleList, function () { return "saving_articelist"; }), saveFromServices: withLoadingReason(session.saveFromServices, function () { return "saving_to_services"; }), withLoadingReason: withLoadingReason });
};
