"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NestiaEditorApplication = NestiaEditorApplication;
const jsx_runtime_1 = require("react/jsx-runtime");
const material_1 = require("@mui/material");
const react_1 = __importDefault(require("react"));
const NestiaEditorIframe_1 = require("./NestiaEditorIframe");
const NestiaEditorUploader_1 = require("./NestiaEditorUploader");
function NestiaEditorApplication() {
    const [ready, setReady] = react_1.default.useState(false);
    const [asset, setAsset] = react_1.default.useState(null);
    react_1.default.useEffect(() => {
        (() => __awaiter(this, void 0, void 0, function* () {
            try {
                setAsset(yield getAsset());
            }
            catch (_a) {
                setAsset(null);
            }
            setReady(true);
        }))().catch(() => { });
    }, []);
    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 _a, _b, _c, _d, _e, _f;
        const index = window.location.href.indexOf("?");
        const query = new URLSearchParams(index === -1 ? "" : window.location.href.substring(index + 1));
        if (query.has("uploader"))
            return null;
        const url = (_b = (_a = query.get("url")) !== null && _a !== void 0 ? _a : (yield findSwagger("./swagger.json"))) !== null && _b !== void 0 ? _b : (yield findSwagger("./swagger.yaml"));
        if (url === null)
            return null;
        const mode = query.get("mode");
        const packageName = (_c = query.get("package")) !== null && _c !== void 0 ? _c : window.package;
        const keyword = (_d = query.get("keyword")) !== null && _d !== void 0 ? _d : window.keyword;
        const simulate = (_e = query.get("simulate")) !== null && _e !== void 0 ? _e : window.simulate;
        const e2e = (_f = query.get("e2e")) !== null && _f !== void 0 ? _f : window.e2e;
        return {
            mode: mode === "nest" ? "nest" : "sdk",
            package: packageName !== null && packageName !== void 0 ? packageName : "@ORGANIZATION/PROJECT",
            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* () {
        const response = yield fetch(file);
        return response.status === 200 ? file : null;
    });
}
//# sourceMappingURL=NestiaEditorApplication.js.map