"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.NestiaEditorUploader = NestiaEditorUploader;
const jsx_runtime_1 = require("react/jsx-runtime");
const material_1 = require("@mui/material");
const sdk_1 = __importDefault(require("@stackblitz/sdk"));
const react_1 = __importDefault(require("react"));
const NestiaEditorComposer_1 = require("./internal/NestiaEditorComposer");
const NestiaEditorFileUploader_1 = require("./internal/NestiaEditorFileUploader");
function NestiaEditorUploader(props) {
    // PARAMETERS
    const [mode, setMode] = react_1.default.useState("sdk");
    const [keyword, setKeyword] = react_1.default.useState(true);
    const [simulate, setSimulate] = react_1.default.useState(true);
    const [e2e, setE2e] = react_1.default.useState(true);
    const [name, setName] = react_1.default.useState("@ORGINIZATION/PROJECT");
    // RESULT
    const [document, setDocument] = react_1.default.useState(null);
    const [progress, setProgress] = react_1.default.useState(false);
    const handleError = (error) => {
        if (props.onError)
            props.onError(error);
        else
            alert(error);
    };
    const handleSwagger = (document, error) => {
        setDocument(document);
        if (error !== null)
            handleError(error);
    };
    const generate = () => __awaiter(this, void 0, void 0, function* () {
        var _a, _b;
        if (document === null)
            return;
        setProgress(true);
        try {
            const result = yield NestiaEditorComposer_1.NestiaEditorComposer[mode]({
                document,
                keyword,
                e2e,
                simulate,
                package: name,
            });
            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));
            }
        }
        catch (exp) {
            handleError(exp instanceof Error ? exp.message : "unknown error");
        }
        setProgress(false);
    });
    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: (e) => 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: (_e, value) => 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: () => 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: () => 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: () => 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: () => generate(), children: progress ? "Generating..." : "Generate Editor" })] }));
}
//# sourceMappingURL=NestiaEditorUploader.js.map