"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const IField_1 = require("../dataform/IField");
const Utilities_1 = require("../core/Utilities");
const FieldUtilities_1 = require("../dataform/FieldUtilities");
class JsonSchemaGenerator {
    static async convertFormDefinitionToJsonSchema(formDef, parentId) {
        let id = "";
        if (parentId) {
            id = parentId + "/";
        }
        if (formDef.id) {
            id += formDef.id;
        }
        if (id.length === 0) {
            if (formDef.title) {
                id = formDef.title;
            }
        }
        const schema = {
            $schema: "http://json-schema.org/draft-07/schema#",
            $id: id,
            title: formDef.title ? formDef.title : formDef.id,
            description: formDef.description,
            type: "object",
            properties: {},
            required: [],
        };
        for (const field of formDef.fields) {
            schema.properties = schema.properties || {};
            if (Utilities_1.default.isUsableAsObjectKey(field.id)) {
                schema.properties[field.id] = await JsonSchemaGenerator.convertFieldToJsonSchema(field);
                if (field.isRequired) {
                    schema.required = schema.required || [];
                    schema.required.push(field.id);
                }
            }
        }
        return schema;
    }
    static async convertFieldToJsonSchema(field) {
        let schema = {
            title: field.title,
            description: field.description,
        };
        let subForm = await FieldUtilities_1.default.getSubForm(field);
        switch (field.dataType) {
            case IField_1.FieldDataType.int:
                schema.type = "integer";
                break;
            case IField_1.FieldDataType.boolean:
                schema.type = "boolean";
                break;
            case IField_1.FieldDataType.string:
            case IField_1.FieldDataType.localizableString:
            case IField_1.FieldDataType.molang:
            case IField_1.FieldDataType.minecraftEventReference:
                schema.type = "string";
                break;
            case IField_1.FieldDataType.stringArray:
                schema.type = "array";
                schema.items = { type: "string" };
                break;
            case IField_1.FieldDataType.minecraftEventTrigger:
            case IField_1.FieldDataType.object:
                schema.type = "object";
                schema.properties = {};
                if (field.subForm) {
                    for (const subField of field.subForm?.fields || []) {
                        schema.properties = schema.properties || {};
                        if (Utilities_1.default.isUsableAsObjectKey(subField.id)) {
                            schema.properties[subField.id] = await JsonSchemaGenerator.convertFieldToJsonSchema(subField);
                        }
                    }
                }
                break;
            case IField_1.FieldDataType.objectArray:
                schema.type = "array";
                schema.items = {
                    type: "object",
                    properties: {},
                };
                for (const subField of field.subForm?.fields || []) {
                    schema.items.properties = schema.items.properties || {};
                    if (Utilities_1.default.isUsableAsObjectKey(subField.id)) {
                        schema.items.properties[subField.id] = await JsonSchemaGenerator.convertFieldToJsonSchema(subField);
                    }
                }
                break;
            case IField_1.FieldDataType.intRange:
            case IField_1.FieldDataType.point3:
                schema.type = "array";
                schema.items = { type: "number" };
                break;
            case IField_1.FieldDataType.keyedStringCollection:
                schema.type = "object";
                schema.additionalProperties = { type: "string" };
                break;
            case IField_1.FieldDataType.keyedNumberCollection:
                schema.type = "object";
                schema.additionalProperties = { type: "number" };
                break;
            case IField_1.FieldDataType.keyedObjectCollection:
                schema.type = "object";
                schema.additionalProperties = { type: "object" };
                break;
            case IField_1.FieldDataType.float:
            case IField_1.FieldDataType.number:
                schema.type = "number";
                break;
            default:
                schema.type = "string";
                break;
        }
        if (field.description) {
            schema.description = field.description;
        }
        if (field.choices && field.choices.length > 0) {
            schema.enum = field.choices.map((choice) => choice.id);
        }
        return schema;
    }
}
exports.default = JsonSchemaGenerator;

//# sourceMappingURL=../maps/docgen/JsonSchemaGenerator.js.map
