"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.NestiaEditorUploader = NestiaEditorUploader;
var jsx_runtime_1 = require("react/jsx-runtime");
var material_1 = require("@mui/material");
var sdk_1 = __importDefault(require("@stackblitz/sdk"));
var react_1 = __importDefault(require("react"));
var NestiaEditorComposer_1 = require("./internal/NestiaEditorComposer");
var NestiaEditorFileUploader_1 = require("./internal/NestiaEditorFileUploader");
function NestiaEditorUploader(props) {
    var _this = this;
    // PARAMETERS
    var _a = __read(react_1.default.useState("sdk"), 2), mode = _a[0], setMode = _a[1];
    var _b = __read(react_1.default.useState(true), 2), keyword = _b[0], setKeyword = _b[1];
    var _c = __read(react_1.default.useState(true), 2), simulate = _c[0], setSimulate = _c[1];
    var _d = __read(react_1.default.useState(true), 2), e2e = _d[0], setE2e = _d[1];
    var _f = __read(react_1.default.useState("@ORGINIZATION/PROJECT"), 2), name = _f[0], setName = _f[1];
    // RESULT
    var _g = __read(react_1.default.useState(null), 2), document = _g[0], setDocument = _g[1];
    var _h = __read(react_1.default.useState(false), 2), progress = _h[0], setProgress = _h[1];
    var handleError = function (error) {
        if (props.onError)
            props.onError(error);
        else
            alert(error);
    };
    var handleSwagger = function (document, error) {
        setDocument(document);
        if (error !== null)
            handleError(error);
    };
    var generate = function () { return __awaiter(_this, void 0, void 0, function () {
        var result, exp_1;
        var _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    if (document === null)
                        return [2 /*return*/];
                    setProgress(true);
                    _c.label = 1;
                case 1:
                    _c.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, NestiaEditorComposer_1.NestiaEditorComposer[mode]({
                            document: document,
                            keyword: keyword,
                            e2e: e2e,
                            simulate: simulate,
                            package: name,
                        })];
                case 2:
                    result = _c.sent();
                    if (result.success === true) {
                        sdk_1.default.openProject({
                            title: (_b = (_a = document.info) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : "Nestia Editor",
                            template: "node",
                            files: result.data.files,
                        }, {
                            newWindow: true,
                            openFile: result.data.openFile,
                            startScript: result.data.startScript,
                        });
                    }
                    else {
                        handleError(JSON.stringify(result.errors, null, 2));
                    }
                    return [3 /*break*/, 4];
                case 3:
                    exp_1 = _c.sent();
                    handleError(exp_1 instanceof Error ? exp_1.message : "unknown error");
                    return [3 /*break*/, 4];
                case 4:
                    setProgress(false);
                    return [2 /*return*/];
            }
        });
    }); };
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(NestiaEditorFileUploader_1.NestiaEditorFileUploader, { onChange: handleSwagger }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsxs)(material_1.FormControl, { fullWidth: true, style: { paddingLeft: 15 }, children: [(0, jsx_runtime_1.jsx)(material_1.TextField, { onChange: function (e) { return setName(e.target.value); }, defaultValue: name, label: "Package Name", variant: "outlined" }), (0, jsx_runtime_1.jsx)(material_1.FormLabel, { style: { paddingTop: 20 }, children: " Mode " }), (0, jsx_runtime_1.jsxs)(material_1.RadioGroup, { defaultValue: mode, onChange: function (_e, value) { return setMode(value); }, style: { paddingLeft: 15 }, children: [(0, jsx_runtime_1.jsx)(material_1.FormControlLabel, { value: "sdk", control: (0, jsx_runtime_1.jsx)(material_1.Radio, {}), label: "Software Development Kit" }), (0, jsx_runtime_1.jsx)(material_1.FormControlLabel, { value: "nest", control: (0, jsx_runtime_1.jsx)(material_1.Radio, {}), label: "NestJS Project" })] }), (0, jsx_runtime_1.jsx)(material_1.FormLabel, { style: { paddingTop: 20 }, children: " Options " }), (0, jsx_runtime_1.jsx)(material_1.FormControlLabel, { label: "Keyword Parameter", style: { paddingTop: 5, paddingLeft: 15 }, control: (0, jsx_runtime_1.jsx)(material_1.Switch, { checked: keyword, onChange: function () { return setKeyword(!keyword); } }) }), (0, jsx_runtime_1.jsx)(material_1.FormControlLabel, { label: "Mockup Simulator", style: { paddingTop: 5, paddingLeft: 15 }, control: (0, jsx_runtime_1.jsx)(material_1.Switch, { checked: simulate, onChange: function () { return setSimulate(!simulate); } }) }), (0, jsx_runtime_1.jsx)(material_1.FormControlLabel, { label: "E2E Test Functions", style: { paddingLeft: 15 }, control: (0, jsx_runtime_1.jsx)(material_1.Switch, { checked: e2e, onChange: function () { return setE2e(!e2e); } }) })] }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(material_1.Button, { component: "a", fullWidth: true, variant: "contained", color: "info", size: "large", disabled: progress === true || document === null, onClick: function () { return generate(); }, children: progress ? "Generating..." : "Generate Editor" })] }));
}
//# sourceMappingURL=NestiaEditorUploader.js.map