import { __awaiter, __extends, __generator } from "tslib";
import { Backend, } from "../Types";
import { IGXACatalogManager } from "./CatalogManager";
import { IGXELoader } from "./Loader";
import { IGXEProductDataManager } from "./ProductDataManager";
var IGXEBackend = /** @class */ (function (_super) {
    __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 __awaiter(this, void 0, void 0, function () {
            var settings, igxa, catalogManager, files, igxeFiles, nativeData, additionalData, productManager;
            return __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*/, 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 = IGXELoader.readProductDataNative(igxeFiles.pop());
                        additionalData = igxeFiles.map(function (file) {
                            return IGXELoader.readProductDataNative(file, true);
                        });
                        productManager = new IGXEProductDataManager({
                            nativeData: nativeData,
                            additionalData: additionalData,
                            pushInterProductPropertysToTop: true,
                            debugInfos: undefined,
                        });
                        return [2 /*return*/, {
                                catalogManager: catalogManager,
                                productManager: productManager,
                            }];
                }
            });
        });
    };
    return IGXEBackend;
}(Backend));
export { IGXEBackend };
