"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Types_1 = require("../Types");
var CatalogManager_1 = require("./CatalogManager");
var Loader_1 = require("./Loader");
var ProductDataManager_1 = require("./ProductDataManager");
var IGXEBackend = /** @class */ (function (_super) {
    tslib_1.__extends(IGXEBackend, _super);
    function IGXEBackend() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    IGXEBackend.prototype.initializeManagers = function (_a) {
        var catalog = _a.catalog, fetchData = _a.fetchData, dispatcherId = _a.dispatcherId;
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var settings, igxa, catalogManager, files, igxeFiles, nativeData, additionalData, productManager;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        settings = catalog.Settings;
                        if (settings.IGXA === undefined) {
                            throw new Error("Can not initialize IGXACatalogManager without an igxa");
                        }
                        return [4 /*yield*/, fetchData(settings.IGXA)];
                    case 1:
                        igxa = _b.sent();
                        return [4 /*yield*/, CatalogManager_1.IGXACatalogManager.Create({
                                data: igxa,
                                catalogLocale: "any",
                                sourceId: dispatcherId,
                            })];
                    case 2:
                        catalogManager = _b.sent();
                        files = settings.IGXEAdditional || [];
                        if (settings.IGXEPrimary) {
                            files.push(settings.IGXEPrimary);
                        }
                        return [4 /*yield*/, Promise.all(files.map(fetchData))];
                    case 3:
                        igxeFiles = (_b.sent());
                        nativeData = Loader_1.IGXELoader.readProductDataNative(igxeFiles.pop());
                        additionalData = igxeFiles.map(function (file) {
                            return Loader_1.IGXELoader.readProductDataNative(file, true);
                        });
                        productManager = new ProductDataManager_1.IGXEProductDataManager({
                            nativeData: nativeData,
                            additionalData: additionalData,
                            pushInterProductPropertysToTop: true,
                            debugInfos: undefined,
                        });
                        return [2 /*return*/, {
                                catalogManager: catalogManager,
                                productManager: productManager,
                            }];
                }
            });
        });
    };
    return IGXEBackend;
}(Types_1.Backend));
exports.IGXEBackend = IGXEBackend;
