"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = exports.getConfigFromNetwork = exports.getConfigFromCache = void 0;
const getConfigFromCache = () => {
    var _a, _b;
    if (typeof window === "undefined") {
        return;
    }
    const configFromCache = JSON.parse((_a = window.sessionStorage.getItem("reactify-search:config")) !== null && _a !== void 0 ? _a : "null");
    if (configFromCache) {
        if (((_b = configFromCache.config.cache) === null || _b === void 0 ? void 0 : _b.enabled) === false) {
            return;
        }
        return configFromCache.config;
    }
    return;
};
exports.getConfigFromCache = getConfigFromCache;
const getConfigFromNetwork = (shopifyPermanentDomain) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    let configFromNetwork;
    try {
        configFromNetwork = (_a = (yield fetch(`https://config.usereactify.com/?shop=${shopifyPermanentDomain}`).then((response) => response.json()))) === null || _a === void 0 ? void 0 : _a.body;
    }
    catch (error) {
        configFromNetwork = (_b = (yield fetch(`https://config.usereactify.com/?shop=${shopifyPermanentDomain}`).then((response) => response.json()))) === null || _b === void 0 ? void 0 : _b.body;
    }
    return configFromNetwork;
});
exports.getConfigFromNetwork = getConfigFromNetwork;
const getConfig = (shopifyPermanentDomain) => __awaiter(void 0, void 0, void 0, function* () {
    const configFromCache = (0, exports.getConfigFromCache)();
    if (configFromCache) {
        return configFromCache;
    }
    const configFromNetwork = yield (0, exports.getConfigFromNetwork)(shopifyPermanentDomain);
    if (!configFromNetwork) {
        throw new Error("Unable to load config");
    }
    if (typeof window !== "undefined") {
        window.sessionStorage.setItem("reactify-search:config", JSON.stringify({
            config: configFromNetwork,
        }));
    }
    return configFromNetwork;
});
exports.getConfig = getConfig;
//# sourceMappingURL=config.js.map