/* Generated from Java with JSweet 2.3.7 - http://www.jsweet.org */
import { ModelUtils } from '../../cmd/util/ModelUtils';
import { JsonCompat } from '../../compat/JsonCompat';
import { NodeCompat } from '../../compat/NodeCompat';
import { RegexCompat } from '../../compat/RegexCompat';
import { DocumentType } from '../models/DocumentType';
/**
 * @author eric.wittmann@gmail.com
 * @class
 */
var OasSchemaFactory = /** @class */ (function () {
    function OasSchemaFactory() {
    }
    /*private*/ OasSchemaFactory.resolveType = function (thing, schema) {
        if (JsonCompat.isNumber(thing)) {
            var value = JsonCompat.toNumber(thing);
            var isInteger = ("" + value).indexOf(".") === -1;
            if (isInteger) {
                schema.type = "integer";
                if ( /* intValue */(value | 0) >= -2147483647 && /* intValue */ (value | 0) <= 2147483647) {
                    schema.format = "int32";
                }
                else if ( /* longValue */value >= -9223372036854775807 && /* longValue */ value <= 9223372036854775807) {
                    schema.format = "int64";
                }
            }
            else {
                schema.type = "number";
                schema.format = "double";
            }
        }
        else if (JsonCompat.isBoolean(thing)) {
            schema.type = "boolean";
        }
        else if (JsonCompat.isArray(thing)) {
            schema.type = "array";
        }
        else if (JsonCompat.isObject(thing)) {
            schema.type = "object";
        }
        else if (JsonCompat.isString(thing)) {
            var value = JsonCompat.toString(thing);
            schema.type = "string";
            if (RegexCompat.matches(value, "^(\\d{4})\\D?(0[1-9]|1[0-2])\\D?([12]\\d|0[1-9]|3[01])$")) {
                schema.format = "date";
            }
            else if (RegexCompat.matches(value, "^(\\d{4})\\D?(0[1-9]|1[0-2])\\D?([12]\\d|0[1-9]|3[01])(\\D?([01]\\d|2[0-3])\\D?([0-5]\\d)\\D?([0-5]\\d)?\\D?(\\d{3})?([zZ]|([\\+-])([01]\\d|2[0-3])\\D?([0-5]\\d)?)?)?$")) {
                schema.format = "date-time";
            }
        }
        else if (JsonCompat.isNull(thing)) {
            schema.type = "string";
        }
    };
    /*private*/ OasSchemaFactory.resolveAll = function (object, schema) {
        OasSchemaFactory.resolveType(object, schema);
        if (NodeCompat.equals(schema.type, "array")) {
            schema.items = schema.createItemsSchema();
            var list = JsonCompat.toList(object);
            if ( /* size */list.length > 0) {
                OasSchemaFactory.resolveAll(/* get */ list[0], schema.items);
            }
        }
        else if (NodeCompat.equals(schema.type, "object")) {
            schema.type = "object";
            var keys = JsonCompat.keys(object);
            for (var index121 = 0; index121 < keys.length; index121++) {
                var propName = keys[index121];
                {
                    var pschema = schema.createPropertySchema(propName);
                    schema.addProperty(propName, pschema);
                    var propValue = JsonCompat.getProperty(object, propName);
                    OasSchemaFactory.resolveAll(propValue, pschema);
                }
            }
        }
    };
    /**
     * Creates a new definition schema from a given example.  This method will analyze the example
     * object and create a new schema object that represents the example.  Note that this method
     * does not support arbitrarily complicated examples, and should be used as a starting point
     * for a schema, not a canonical one.
     * @param {OasDocument} document
     * @param {string} name
     * @param {*} example
     * @return {*}
     */
    OasSchemaFactory.createSchemaDefinitionFromExample = function (document, name, example) {
        var schema = null;
        if (document.getDocumentType() === DocumentType.openapi2) {
            var doc20 = document;
            if (ModelUtils.isNullOrUndefined(doc20.definitions)) {
                doc20.definitions = doc20.createDefinitions();
            }
            schema = doc20.definitions.createSchemaDefinition(name);
        }
        else if (document.getDocumentType() === DocumentType.openapi3) {
            var doc30 = document;
            if (ModelUtils.isNullOrUndefined(doc30.components)) {
                doc30.components = doc30.createComponents();
            }
            schema = doc30.components.createSchemaDefinition(name);
        }
        else {
            throw Object.defineProperty(new Error("Only OpenAPI 2 and 3 are currently supported."), '__classes', { configurable: true, value: ['java.lang.Throwable', 'java.lang.Object', 'java.lang.RuntimeException', 'java.lang.Exception'] });
        }
        if (JsonCompat.isString(example)) {
            example = JsonCompat.parseJSON(JsonCompat.toString(example));
        }
        OasSchemaFactory.resolveAll(example, schema);
        schema.title = "Root Type for " + name;
        schema.description = "The root of the " + name + " type\'s schema.";
        return schema;
    };
    return OasSchemaFactory;
}());
export { OasSchemaFactory };
OasSchemaFactory["__class"] = "io.apicurio.datamodels.core.factories.OasSchemaFactory";
//# sourceMappingURL=OasSchemaFactory.js.map