"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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NestiaEditorApplication = NestiaEditorApplication;
var jsx_runtime_1 = require("react/jsx-runtime");
var material_1 = require("@mui/material");
var react_1 = __importDefault(require("react"));
var NestiaEditorIframe_1 = require("./NestiaEditorIframe");
var NestiaEditorUploader_1 = require("./NestiaEditorUploader");
function NestiaEditorApplication() {
    var _this = this;
    var _a = __read(react_1.default.useState(false), 2), ready = _a[0], setReady = _a[1];
    var _b = __read(react_1.default.useState(null), 2), asset = _b[0], setAsset = _b[1];
    react_1.default.useEffect(function () {
        (function () { return __awaiter(_this, void 0, void 0, function () {
            var _a, _b;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        _c.trys.push([0, 2, , 3]);
                        _a = setAsset;
                        return [4 /*yield*/, getAsset()];
                    case 1:
                        _a.apply(void 0, [_c.sent()]);
                        return [3 /*break*/, 3];
                    case 2:
                        _b = _c.sent();
                        setAsset(null);
                        return [3 /*break*/, 3];
                    case 3:
                        setReady(true);
                        return [2 /*return*/];
                }
            });
        }); })().catch(function () { });
    }, []);
    if (ready === false)
        return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
    return asset !== null ? ((0, jsx_runtime_1.jsx)(NestiaEditorIframe_1.NestiaEditorIframe, { swagger: asset.url, package: asset.package, simulate: asset.simulate, e2e: asset.e2e, mode: asset.mode })) : ((0, jsx_runtime_1.jsxs)("div", { style: {
            padding: 25,
        }, children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "h4", children: "Nestia Editor" }), (0, jsx_runtime_1.jsx)("hr", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(NestiaEditorUploader_1.NestiaEditorUploader, {})] }));
}
function getAsset() {
    return __awaiter(this, void 0, void 0, function () {
        var index, query, url, _a, _b, mode, packageName, keyword, simulate, e2e;
        var _c, _d, _e, _f, _g, _h;
        return __generator(this, function (_j) {
            switch (_j.label) {
                case 0:
                    index = window.location.href.indexOf("?");
                    query = new URLSearchParams(index === -1 ? "" : window.location.href.substring(index + 1));
                    if (query.has("uploader"))
                        return [2 /*return*/, null];
                    if (!((_c = query.get("url")) !== null && _c !== void 0)) return [3 /*break*/, 1];
                    _b = _c;
                    return [3 /*break*/, 3];
                case 1: return [4 /*yield*/, findSwagger("./swagger.json")];
                case 2:
                    _b = (_j.sent());
                    _j.label = 3;
                case 3:
                    if (!((_d = _b) !== null && _d !== void 0)) return [3 /*break*/, 4];
                    _a = _d;
                    return [3 /*break*/, 6];
                case 4: return [4 /*yield*/, findSwagger("./swagger.yaml")];
                case 5:
                    _a = (_j.sent());
                    _j.label = 6;
                case 6:
                    url = _a;
                    if (url === null)
                        return [2 /*return*/, null];
                    mode = query.get("mode");
                    packageName = (_e = query.get("package")) !== null && _e !== void 0 ? _e : window.package;
                    keyword = (_f = query.get("keyword")) !== null && _f !== void 0 ? _f : window.keyword;
                    simulate = (_g = query.get("simulate")) !== null && _g !== void 0 ? _g : window.simulate;
                    e2e = (_h = query.get("e2e")) !== null && _h !== void 0 ? _h : window.e2e;
                    return [2 /*return*/, {
                            mode: mode === "nest" ? "nest" : "sdk",
                            package: packageName !== null && packageName !== void 0 ? packageName : "@ORGANIZATION/PROJECT",
                            url: url,
                            keyword: keyword !== null
                                ? keyword === true || keyword === "true" || keyword === "1"
                                : false,
                            simulate: simulate !== null
                                ? simulate === true || simulate === "true" || simulate === "1"
                                : false,
                            e2e: e2e !== null ? e2e === true || e2e === "true" || e2e === "1" : false,
                        }];
            }
        });
    });
}
function findSwagger(file) {
    return __awaiter(this, void 0, void 0, function () {
        var response;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, fetch(file)];
                case 1:
                    response = _a.sent();
                    return [2 /*return*/, response.status === 200 ? file : null];
            }
        });
    });
}
//# sourceMappingURL=NestiaEditorApplication.js.map