import { __assign, __awaiter, __generator, __read } from "tslib";
import { Obj } from "@intelligentgraphics/ig.utilities";
export var fetchArticleData = function (productDataManager, comId) { return __awaiter(void 0, void 0, void 0, function () {
    var _a, articleId, categories, properties, sourceData, subArticles;
    return __generator(this, function (_b) {
        switch (_b.label) {
            case 0: return [4 /*yield*/, Promise.all([
                    productDataManager.getArticleID(comId),
                    productDataManager.getArticleCategories(comId),
                    productDataManager.getArticlePropertyList(comId),
                    productDataManager.getArticleCustomData !== undefined
                        ? productDataManager.getArticleCustomData(comId)
                        : undefined,
                    productDataManager.getSubArticleIDs(comId),
                ])];
            case 1:
                _a = __read.apply(void 0, [_b.sent(), 5]), articleId = _a[0], categories = _a[1], properties = _a[2], sourceData = _a[3], subArticles = _a[4];
                return [2 /*return*/, {
                        articleId: articleId,
                        categories: categories,
                        properties: properties,
                        sourceData: sourceData || {},
                        subArticles: subArticles,
                    }];
        }
    });
}); };
export var fetchArticleInfo = function (productDataManager, comId, info) { return __awaiter(void 0, void 0, void 0, function () {
    var loadDynamicInfo, updatedInfo;
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                loadDynamicInfo = function () { return ({
                    varCode: productDataManager.getArticleVariantCode(comId),
                    shortText: productDataManager.getArticleShortText(comId),
                    longText: productDataManager.getArticleLongText(comId),
                    varText: productDataManager.getArticleVariantText(comId),
                    image: productDataManager.getArticleCatalogImage(comId),
                    prices: productDataManager.getArticlePrices(comId),
                    name: productDataManager.getArticleName(comId),
                }); };
                if (!(info === undefined)) return [3 /*break*/, 2];
                return [4 /*yield*/, Obj.loadObjectData(__assign(__assign({}, loadDynamicInfo()), { programName: productDataManager.getArticleProgramName(comId), programId: productDataManager.getArticleProgramID(comId), manufacturerName: productDataManager.getArticleManufacturerName(comId), manufacturerId: productDataManager.getArticleManufacturerID(comId) }))];
            case 1:
                info = _a.sent();
                return [3 /*break*/, 4];
            case 2: return [4 /*yield*/, Obj.loadObjectData(loadDynamicInfo()).catch(function () { return undefined; })];
            case 3:
                updatedInfo = _a.sent();
                info = __assign(__assign({}, info), updatedInfo);
                _a.label = 4;
            case 4: return [2 /*return*/, info];
        }
    });
}); };
