"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.NestiaEditorFileUploader = NestiaEditorFileUploader;
const jsx_runtime_1 = require("react/jsx-runtime");
const js_yaml_1 = require("js-yaml");
const react_1 = __importDefault(require("react"));
// @ts-ignore
const react_mui_fileuploader_1 = __importDefault(require("react-mui-fileuploader"));
function NestiaEditorFileUploader(props) {
    const [elements, setElements] = react_1.default.useState([]);
    const onChange = (array) => __awaiter(this, void 0, void 0, function* () {
        if (array.length === 0) {
            props.onChange(null, null);
            return;
        }
        const file = array[array.length - 1];
        const buffer = yield file.arrayBuffer();
        const content = new TextDecoder().decode(buffer);
        const extension = file.name.split(".").pop();
        try {
            const json = extension === "json" ? JSON.parse(content) : (0, js_yaml_1.load)(content);
            props.onChange(json, null);
        }
        catch (_a) {
            props.onChange(null, extension === "json" ? "Invalid JSON file" : "Invalid YAML file");
            return;
        }
        if (array.length > 1)
            setElements([file]);
    });
    return ((0, jsx_runtime_1.jsx)(react_mui_fileuploader_1.default, { defaultFiles: elements, onFilesChange: onChange, acceptedType: ".json, .yaml", getBase64: false, multiFile: false, maxUploadFiles: 1, title: "Swagger file uploader", header: "Drag and drop a Swagger file here", buttonLabel: "Click Here", rightLabel: "to select swagger.json/yaml file", buttonRemoveLabel: "Clear" }));
}
//# sourceMappingURL=NestiaEditorFileUploader.js.map