/* Generated from Java with JSweet 2.3.7 - http://www.jsweet.org */
import { Library } from '../../../Library';
import { JsonCompat } from '../../../compat/JsonCompat';
import { NodeCompat } from '../../../compat/NodeCompat';
import { Constants } from '../../../core/Constants';
import { DocumentType } from '../../../core/models/DocumentType';
import { ReferenceUtil } from '../../../core/util/ReferenceUtil';
import { ConsumesProducesFinder } from '../../../core/visitors/ConsumesProducesFinder';
import { OperationFinder } from '../../../core/visitors/OperationFinder';
import { TraverserDirection } from '../../../core/visitors/TraverserDirection';
/**
 * A visitor used to transform an OpenAPI 2.0 document into an OpenAPI 3.0.x document.
 * @author eric.wittmann@gmail.com
 * @class
 */
var Oas20to30TransformationVisitor = /** @class */ (function () {
    function Oas20to30TransformationVisitor() {
        if (this.doc30 === undefined)
            this.doc30 = null;
        this._nodeMap = ({});
        this._postProcessResponses = ([]);
        this._postProcessingComplete = false;
    }
    /**
     * Retrieves the final result.
     * @return {Oas30Document}
     */
    Oas20to30TransformationVisitor.prototype.getResult = function () {
        if (!this._postProcessingComplete) {
            this.postProcess();
        }
        return this.doc30;
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitDocument(io.apicurio.datamodels.core.models.Document)
     * @param {Document} node
     */
    Oas20to30TransformationVisitor.prototype.visitDocument = function (node) {
        this.doc30 = Library.createDocument(DocumentType.openapi3);
        var doc20 = node;
        if (!NodeCompat.isNullOrUndefined(doc20.host)) {
            var basePath = doc20.basePath;
            if (NodeCompat.isNullOrUndefined(basePath)) {
                basePath = "";
            }
            var schemes = doc20.schemes;
            if (NodeCompat.isNullOrUndefined(schemes) || /* size */ schemes.length === 0) {
                schemes = NodeCompat.asList("http");
            }
            var server30 = this.doc30.createServer();
            this.doc30.servers = ([]);
            /* add */ (this.doc30.servers.push(server30) > 0);
            if ( /* size */schemes.length === 1) {
                server30.url = /* get */ schemes[0] + "://" + doc20.host + basePath;
            }
            else {
                server30.url = "{scheme}://" + doc20.host + basePath;
                var var30 = server30.createServerVariable("scheme");
                server30.addServerVariable("scheme", var30);
                var30.default_ = /* get */ schemes[0];
                var30.enum_ = NodeCompat.copyList(schemes);
                var30.description = "The supported protocol schemes.";
            }
        }
        this.mapNode(doc20, this.doc30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitInfo(io.apicurio.datamodels.core.models.common.Info)
     * @param {Info} node
     */
    Oas20to30TransformationVisitor.prototype.visitInfo = function (node) {
        this.doc30.info = this.doc30.createInfo();
        this.doc30.info.title = node.title;
        this.doc30.info.description = node.description;
        this.doc30.info.termsOfService = node.termsOfService;
        this.doc30.info.version = node.version;
        this.mapNode(node, this.doc30.info);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitContact(io.apicurio.datamodels.core.models.common.Contact)
     * @param {Contact} node
     */
    Oas20to30TransformationVisitor.prototype.visitContact = function (node) {
        var info30 = this.lookup(node.parent());
        var contact30 = info30.createContact();
        info30.contact = contact30;
        contact30.name = node.name;
        contact30.url = node.url;
        contact30.email = node.email;
        this.mapNode(node, contact30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitLicense(io.apicurio.datamodels.core.models.common.License)
     * @param {License} node
     */
    Oas20to30TransformationVisitor.prototype.visitLicense = function (node) {
        var info30 = this.lookup(node.parent());
        var license30 = info30.createLicense();
        info30.license = license30;
        license30.name = node.name;
        license30.url = node.url;
        this.mapNode(node, license30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitPaths(io.apicurio.datamodels.openapi.models.OasPaths)
     * @param {OasPaths} node
     */
    Oas20to30TransformationVisitor.prototype.visitPaths = function (node) {
        this.doc30.paths = this.doc30.createPaths();
        this.mapNode(node, this.doc30.paths);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitPathItem(io.apicurio.datamodels.openapi.models.OasPathItem)
     * @param {OasPathItem} node
     */
    Oas20to30TransformationVisitor.prototype.visitPathItem = function (node) {
        var paths30 = this.lookup(node.parent());
        var pathItem30 = paths30.createPathItem(node.getPath());
        paths30.addPathItem(node.getPath(), pathItem30);
        pathItem30.$ref = this.updateRef(node.$ref);
        this.mapNode(node, pathItem30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitOperation(io.apicurio.datamodels.core.models.common.Operation)
     * @param {Operation} node
     */
    Oas20to30TransformationVisitor.prototype.visitOperation = function (node) {
        var op = node;
        var pathItem30 = this.lookup(node.parent());
        var operation30 = pathItem30.createOperation(node.getType());
        pathItem30.setOperation(operation30);
        operation30.tags = op.tags;
        operation30.summary = op.summary;
        operation30.description = op.description;
        operation30.operationId = op.operationId;
        operation30.deprecated = op.deprecated;
        if (!NodeCompat.isNullOrUndefined(op.schemes) && /* size */ op.schemes.length > 0 && !NodeCompat.isNullOrUndefined(this.doc30.servers) && /* size */ this.doc30.servers.length > 0) {
            var server30 = operation30.createServer();
            operation30.servers = ([]);
            /* add */ (operation30.servers.push(server30) > 0);
            server30.url = /* get */ this.doc30.servers[0].url;
            if ( /* size */op.schemes.length === 1) {
                server30.url = /* replace */ server30.url.split("{scheme}").join(/* get */ op.schemes[0]);
                server30.removeServerVariable("scheme");
            }
            else {
                server30.url = "{scheme}" + server30.url.substring(server30.url.indexOf("://"));
                var var30 = server30.createServerVariable("scheme");
                server30.addServerVariable("scheme", var30);
                var30.description = "The supported protocol schemes.";
                var30.default_ = /* get */ op.schemes[0];
                var30.enum_ = NodeCompat.copyList(op.schemes);
            }
        }
        this.mapNode(op, operation30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitParameter(io.apicurio.datamodels.core.models.common.Parameter)
     * @param {Parameter} node
     */
    Oas20to30TransformationVisitor.prototype.visitParameter = function (node) {
        var _this = this;
        var param20 = node;
        if (NodeCompat.equals(param20.in, "body")) {
            var operation30 = this.lookup(this.findParentOperation(param20));
            if (!NodeCompat.isNullOrUndefined(operation30)) {
                var body30 = operation30.createRequestBody();
                operation30.requestBody = body30;
                body30.description = param20.description;
                body30.required = param20.required;
                if (!NodeCompat.isNullOrUndefined(param20.schema)) {
                    var consumes = this.findConsumes(param20);
                    var schema = param20.schema;
                    consumes.forEach((function (schema, body30) {
                        return function (ct) {
                            var mediaType30 = body30.createMediaType(ct);
                            body30.addMediaType(ct, mediaType30);
                            var schema30 = mediaType30.createSchema();
                            mediaType30.schema = _this.toSchema(schema, schema30, true);
                            _this.mapNode(schema, schema30);
                        };
                    })(schema, body30));
                }
            }
        }
        else if (NodeCompat.equals(param20.in, "formData")) {
            var operation30 = this.lookup(this.findParentOperation(param20));
            if (!NodeCompat.isNullOrUndefined(operation30)) {
                var consumes = this.findConsumes(param20);
                if (!this.hasFormDataMimeType(consumes)) {
                    consumes = NodeCompat.asList("application/x-www-form-urlencoded");
                }
                consumes.forEach((function (operation30, param20) {
                    return function (ct) {
                        if (_this.isFormDataMimeType(ct)) {
                            var body30 = operation30.requestBody;
                            if (NodeCompat.isNullOrUndefined(body30)) {
                                body30 = operation30.createRequestBody();
                                operation30.requestBody = body30;
                                body30.required = true;
                            }
                            var mediaType30 = body30.getMediaType(ct);
                            if (NodeCompat.isNullOrUndefined(mediaType30)) {
                                mediaType30 = body30.createMediaType(ct);
                                body30.addMediaType(ct, mediaType30);
                            }
                            var schema30 = mediaType30.schema;
                            if (NodeCompat.isNullOrUndefined(schema30)) {
                                schema30 = mediaType30.createSchema();
                                mediaType30.schema = schema30;
                                schema30.type = "object";
                            }
                            var property30 = schema30.createPropertySchema(param20.name);
                            schema30.addProperty(param20.name, property30);
                            property30.description = param20.description;
                            _this.toSchema(param20, property30, false);
                            _this.mapNode(param20, schema30);
                        }
                    };
                })(operation30, param20));
            }
        }
        else {
            if (this.isRef(param20)) {
                var paramDef = ReferenceUtil.resolveRef$java_lang_String$io_apicurio_datamodels_core_models_Node(param20.$ref, param20);
                if (!NodeCompat.isNullOrUndefined(paramDef) && NodeCompat.equals(paramDef.in, "body")) {
                    var parent30_1 = this.lookup(this.findParentOperation(param20));
                    if (!NodeCompat.isNullOrUndefined(parent30_1)) {
                        var body30 = parent30_1.createRequestBody();
                        parent30_1.requestBody = body30;
                        body30.$ref = "#/components/requestBodies/" + paramDef.getName();
                        this.mapNode(param20, body30);
                        return;
                    }
                }
                if (!NodeCompat.isNullOrUndefined(paramDef) && NodeCompat.equals(paramDef.in, "formData")) {
                    Library.readNode(Library.writeNode(paramDef), param20);
                    param20.$ref = null;
                    this.visitParameter(param20);
                    return;
                }
            }
            var parent30 = this.lookup(param20.parent());
            var param30 = parent30.createParameter();
            parent30.addParameter(param30);
            this.transformParam(param20, param30);
            this.mapNode(param20, param30);
        }
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.transformParam = function (node, param30) {
        param30.$ref = this.updateRef(node.$ref);
        if (!NodeCompat.isNullOrUndefined(param30.$ref)) {
            return param30;
        }
        param30.name = node.name;
        param30.in = node.in;
        param30.description = node.description;
        param30.required = node.required;
        param30.allowEmptyValue = node.allowEmptyValue;
        param30.schema = this.toSchema(node, param30.createSchema(), false);
        this.collectionFormatToStyleAndExplode(node, param30);
        return param30;
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitParameterDefinition(io.apicurio.datamodels.core.models.common.IDefinition)
     * @param {*} node
     */
    Oas20to30TransformationVisitor.prototype.visitParameterDefinition = function (node) {
        var _this = this;
        var pd20 = node;
        if (NodeCompat.equals(pd20.in, "body")) {
            var parent30 = this.getOrCreateComponents();
            var bodyDef30 = parent30.createRequestBodyDefinition(pd20.getName());
            parent30.addRequestBodyDefinition(pd20.getName(), bodyDef30);
            bodyDef30.description = pd20.description;
            bodyDef30.required = pd20.required;
            if (!NodeCompat.isNullOrUndefined(pd20.schema)) {
                var consumes = this.findConsumes(pd20);
                var schema = pd20.schema;
                consumes.forEach((function (schema, bodyDef30) {
                    return function (ct) {
                        var mediaType30 = bodyDef30.createMediaType(ct);
                        bodyDef30.addMediaType(ct, mediaType30);
                        var schema30 = mediaType30.createSchema();
                        mediaType30.schema = _this.toSchema(schema, schema30, true);
                        _this.mapNode(schema, schema30);
                    };
                })(schema, bodyDef30));
            }
        }
        else if (NodeCompat.equals(pd20.in, "formData")) {
        }
        else {
            var components30 = this.getOrCreateComponents();
            var paramDef30 = components30.createParameterDefinition(pd20.getName());
            components30.addParameterDefinition(pd20.getName(), paramDef30);
            this.transformParam(pd20, paramDef30);
            this.mapNode(pd20, paramDef30);
        }
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitExternalDocumentation(io.apicurio.datamodels.core.models.common.ExternalDocumentation)
     * @param {ExternalDocumentation} node
     */
    Oas20to30TransformationVisitor.prototype.visitExternalDocumentation = function (node) {
        var parent30 = this.lookup(node.parent());
        var externalDocs30 = parent30.createExternalDocumentation();
        parent30.setExternalDocumentation(externalDocs30);
        externalDocs30.description = node.description;
        externalDocs30.url = node.url;
        this.mapNode(node, externalDocs30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitSecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)
     * @param {SecurityRequirement} node
     */
    Oas20to30TransformationVisitor.prototype.visitSecurityRequirement = function (node) {
        var req = node;
        var parent30 = this.lookup(req.parent());
        var securityRequirement30 = parent30.createSecurityRequirement();
        parent30.addSecurityRequirement(securityRequirement30);
        req.getSecurityRequirementNames().forEach((function (securityRequirement30, req) {
            return function (name) {
                securityRequirement30.addSecurityRequirementItem(name, req.getScopes(name));
            };
        })(securityRequirement30, req));
        this.mapNode(req, securityRequirement30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitResponses(io.apicurio.datamodels.openapi.models.OasResponses)
     * @param {OasResponses} node
     */
    Oas20to30TransformationVisitor.prototype.visitResponses = function (node) {
        var parent30 = this.lookup(node.parent());
        var responses30 = parent30.createResponses();
        parent30.responses = responses30;
        this.mapNode(node, responses30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitResponse(io.apicurio.datamodels.openapi.models.OasResponse)
     * @param {OasResponse} node
     */
    Oas20to30TransformationVisitor.prototype.visitResponse = function (node) {
        var parent30 = this.lookup(node.parent());
        var response30 = parent30.createResponse(node.getStatusCode());
        parent30.addResponse(node.getStatusCode(), response30);
        response30.$ref = this.updateRef(node.$ref);
        this.transformResponse(node, response30);
        this.mapNode(node, response30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitResponseDefinition(io.apicurio.datamodels.core.models.common.IDefinition)
     * @param {*} node
     */
    Oas20to30TransformationVisitor.prototype.visitResponseDefinition = function (node) {
        var parent30 = this.getOrCreateComponents();
        var responseDef30 = parent30.createResponseDefinition(node.getName());
        parent30.addResponseDefinition(node.getName(), responseDef30);
        this.transformResponse(node, responseDef30);
        this.mapNode(node, responseDef30);
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.transformResponse = function (node, response30) {
        var _this = this;
        response30.description = node.description;
        if (!NodeCompat.isNullOrUndefined(node.schema)) {
            var produces = this.findProduces(node);
            var schema = node.schema;
            produces.forEach((function (schema) {
                return function (ct) {
                    var mediaType30 = response30.createMediaType(ct);
                    response30.addMediaType(ct, mediaType30);
                    var schema30 = mediaType30.createSchema();
                    mediaType30.schema = _this.toSchema(schema, schema30, true);
                    if (!NodeCompat.isNullOrUndefined(node.examples)) {
                        var ctexample = node.examples.getExample(ct);
                        if (!NodeCompat.isNullOrUndefined(ctexample)) {
                            mediaType30.example = ctexample;
                        }
                    }
                    _this.mapNode(schema, schema30);
                };
            })(schema));
            if ( /* size */produces.length > 1) {
                /* add */ (this._postProcessResponses.push(response30) > 0);
            }
        }
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitSchema(io.apicurio.datamodels.core.models.common.Schema)
     * @param {Schema} node
     */
    Oas20to30TransformationVisitor.prototype.visitSchema = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitHeaders(io.apicurio.datamodels.openapi.v2.models.Oas20Headers)
     * @param {Oas20Headers} node
     */
    Oas20to30TransformationVisitor.prototype.visitHeaders = function (node) {
        var parent30 = this.lookup(node.parent());
        this.mapNode(node, parent30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitHeader(io.apicurio.datamodels.openapi.models.OasHeader)
     * @param {OasHeader} node
     */
    Oas20to30TransformationVisitor.prototype.visitHeader = function (node) {
        var parent30 = this.lookup(node.parent());
        var header30 = parent30.createHeader(node.getName());
        parent30.addHeader(node.getName(), header30);
        header30.description = node.description;
        header30.schema = this.toSchema(node, header30.createSchema(), false);
        this.mapNode(node, header30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitExample(io.apicurio.datamodels.core.models.common.IExample)
     * @param {*} node
     */
    Oas20to30TransformationVisitor.prototype.visitExample = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitItems(io.apicurio.datamodels.openapi.v2.models.Oas20Items)
     * @param {Oas20Items} node
     */
    Oas20to30TransformationVisitor.prototype.visitItems = function (node) {
        var parent30 = this.findItemsParent(node);
        var items30 = parent30.createItemsSchema();
        parent30.items = items30;
        this.toSchema(node, items30, false);
        this.mapNode(node, items30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitTag(io.apicurio.datamodels.core.models.common.Tag)
     * @param {Tag} node
     */
    Oas20to30TransformationVisitor.prototype.visitTag = function (node) {
        var parent30 = this.doc30;
        var tag30 = parent30.addTag(node.name, node.description);
        this.mapNode(node, tag30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitSecurityDefinitions(io.apicurio.datamodels.openapi.v2.models.Oas20SecurityDefinitions)
     * @param {Oas20SecurityDefinitions} node
     */
    Oas20to30TransformationVisitor.prototype.visitSecurityDefinitions = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitSecurityScheme(io.apicurio.datamodels.core.models.common.SecurityScheme)
     * @param {SecurityScheme} node
     */
    Oas20to30TransformationVisitor.prototype.visitSecurityScheme = function (node) {
        var scheme = node;
        var parent30 = this.getOrCreateComponents();
        var scheme30 = parent30.createSecurityScheme(scheme.getName());
        parent30.addSecurityScheme(scheme.getName(), scheme30);
        scheme30.type = scheme.type;
        scheme30.description = scheme.description;
        scheme30.name = scheme.name;
        scheme30.in = scheme.in;
        if (NodeCompat.equals(scheme.type, "oauth2")) {
            if (NodeCompat.equals(scheme.flow, "implicit")) {
                scheme30.flows = scheme30.createOAuthFlows();
                scheme30.flows.implicit = scheme30.flows.createImplicitOAuthFlow();
                scheme30.flows.implicit.authorizationUrl = scheme.authorizationUrl;
                if (!NodeCompat.isNullOrUndefined(scheme.scopes)) {
                    scheme.scopes.getScopeNames().forEach((function (scheme, scheme30) {
                        return function (scopeName) {
                            scheme30.flows.implicit.addScope(scopeName, scheme.scopes.getScopeDescription(scopeName));
                        };
                    })(scheme, scheme30));
                }
            }
            if (NodeCompat.equals(scheme.flow, "accessCode")) {
                scheme30.flows = scheme30.createOAuthFlows();
                scheme30.flows.authorizationCode = scheme30.flows.createAuthorizationCodeOAuthFlow();
                scheme30.flows.authorizationCode.authorizationUrl = scheme.authorizationUrl;
                scheme30.flows.authorizationCode.tokenUrl = scheme.tokenUrl;
                if (!NodeCompat.isNullOrUndefined(scheme.scopes)) {
                    scheme.scopes.getScopeNames().forEach((function (scheme, scheme30) {
                        return function (scopeName) {
                            scheme30.flows.authorizationCode.addScope(scopeName, scheme.scopes.getScopeDescription(scopeName));
                        };
                    })(scheme, scheme30));
                }
            }
            if (NodeCompat.equals(scheme.flow, "password")) {
                scheme30.flows = scheme30.createOAuthFlows();
                scheme30.flows.password = scheme30.flows.createPasswordOAuthFlow();
                scheme30.flows.password.tokenUrl = scheme.tokenUrl;
                if (!NodeCompat.isNullOrUndefined(scheme.scopes)) {
                    scheme.scopes.getScopeNames().forEach((function (scheme, scheme30) {
                        return function (scopeName) {
                            scheme30.flows.password.addScope(scopeName, scheme.scopes.getScopeDescription(scopeName));
                        };
                    })(scheme, scheme30));
                }
            }
            if (NodeCompat.equals(scheme.flow, "application")) {
                scheme30.flows = scheme30.createOAuthFlows();
                scheme30.flows.clientCredentials = scheme30.flows.createClientCredentialsOAuthFlow();
                scheme30.flows.clientCredentials.tokenUrl = scheme.tokenUrl;
                if (!NodeCompat.isNullOrUndefined(scheme.scopes)) {
                    scheme.scopes.getScopeNames().forEach((function (scheme, scheme30) {
                        return function (scopeName) {
                            scheme30.flows.clientCredentials.addScope(scopeName, scheme.scopes.getScopeDescription(scopeName));
                        };
                    })(scheme, scheme30));
                }
            }
        }
        this.mapNode(scheme, scheme30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitScopes(io.apicurio.datamodels.openapi.v2.models.Oas20Scopes)
     * @param {Oas20Scopes} node
     */
    Oas20to30TransformationVisitor.prototype.visitScopes = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitXML(io.apicurio.datamodels.openapi.models.OasXML)
     * @param {OasXML} node
     */
    Oas20to30TransformationVisitor.prototype.visitXML = function (node) {
        var parent30 = this.lookup(node.parent());
        var xml30 = parent30.createXML();
        parent30.xml = xml30;
        xml30.name = node.name;
        xml30.namespace = node.namespace;
        xml30.prefix = node.prefix;
        xml30.attribute = node.attribute;
        xml30.wrapped = node.wrapped;
        this.mapNode(node, xml30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitSchemaDefinition(io.apicurio.datamodels.core.models.common.IDefinition)
     * @param {*} node
     */
    Oas20to30TransformationVisitor.prototype.visitSchemaDefinition = function (node) {
        var sd20 = node;
        var parent30 = this.getOrCreateComponents();
        var schemaDef30 = parent30.createSchemaDefinition(sd20.getName());
        parent30.addSchemaDefinition(sd20.getName(), schemaDef30);
        this.toSchema(sd20, schemaDef30, true);
        this.mapNode(sd20, schemaDef30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitPropertySchema(io.apicurio.datamodels.openapi.models.IOasPropertySchema)
     * @param {*} node
     */
    Oas20to30TransformationVisitor.prototype.visitPropertySchema = function (node) {
        var ps20 = node;
        var parent30 = this.lookup(ps20.parent());
        var property30 = parent30.createPropertySchema(ps20.getPropertyName());
        parent30.addProperty(ps20.getPropertyName(), property30);
        this.toSchema(ps20, property30, true);
        this.mapNode(ps20, property30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitAdditionalPropertiesSchema(io.apicurio.datamodels.openapi.models.OasSchema)
     * @param {OasSchema} node
     */
    Oas20to30TransformationVisitor.prototype.visitAdditionalPropertiesSchema = function (node) {
        var parent30 = this.lookup(node.parent());
        var additionalProps30 = parent30.createAdditionalPropertiesSchema();
        parent30.additionalProperties = additionalProps30;
        this.toSchema(node, additionalProps30, true);
        this.mapNode(node, additionalProps30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitAllOfSchema(io.apicurio.datamodels.openapi.models.OasSchema)
     * @param {OasSchema} node
     */
    Oas20to30TransformationVisitor.prototype.visitAllOfSchema = function (node) {
        var parent30 = this.lookup(node.parent());
        var allOf30 = parent30.createAllOfSchema();
        if (NodeCompat.isNullOrUndefined(parent30.allOf)) {
            parent30.allOf = ([]);
        }
        /* add */ (parent30.allOf.push(allOf30) > 0);
        this.toSchema(node, allOf30, true);
        this.mapNode(node, allOf30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.visitors.IOasVisitor#visitItemsSchema(io.apicurio.datamodels.openapi.models.OasSchema)
     * @param {OasSchema} node
     */
    Oas20to30TransformationVisitor.prototype.visitItemsSchema = function (node) {
        var parent30 = this.lookup(node.parent());
        var items30 = parent30.createItemsSchema();
        if (!NodeCompat.isNullOrUndefined(parent30.items) && NodeCompat.isNode(parent30.items)) {
            var items = ([]);
            /* add */ (items.push(items30) > 0);
            parent30.items = items;
        }
        else {
            parent30.items = items30;
        }
        this.toSchema(node, items30, true);
        this.mapNode(node, items30);
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitDefinitions(io.apicurio.datamodels.openapi.v2.models.Oas20Definitions)
     * @param {Oas20Definitions} node
     */
    Oas20to30TransformationVisitor.prototype.visitDefinitions = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitParameterDefinitions(io.apicurio.datamodels.openapi.v2.models.Oas20ParameterDefinitions)
     * @param {Oas20ParameterDefinitions} node
     */
    Oas20to30TransformationVisitor.prototype.visitParameterDefinitions = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor#visitResponseDefinitions(io.apicurio.datamodels.openapi.v2.models.Oas20ResponseDefinitions)
     * @param {Oas20ResponseDefinitions} node
     */
    Oas20to30TransformationVisitor.prototype.visitResponseDefinitions = function (node) {
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitExtension(io.apicurio.datamodels.core.models.Extension)
     * @param {Extension} node
     */
    Oas20to30TransformationVisitor.prototype.visitExtension = function (node) {
        var parent30 = this.lookup(node.parent());
        var ext30 = parent30.createExtension();
        ext30.name = node.name;
        ext30.value = node.value;
        parent30.addExtension(node.name, ext30);
    };
    /**
     * @see io.apicurio.datamodels.core.visitors.IVisitor#visitValidationProblem(io.apicurio.datamodels.core.models.ValidationProblem)
     * @param {ValidationProblem} problem
     */
    Oas20to30TransformationVisitor.prototype.visitValidationProblem = function (problem) {
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.mapNode = function (from, to) {
        var nodePath = Library.createNodePath(from);
        var mapIndex = nodePath.toString();
        /* put */ (this._nodeMap[mapIndex] = to);
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.lookup = function (node) {
        var nodePath = Library.createNodePath(node);
        var mapIndex = nodePath.toString();
        return /* get */ (function (m, k) { return m[k] === undefined ? null : m[k]; })(this._nodeMap, mapIndex);
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.getOrCreateComponents = function () {
        if (NodeCompat.isNullOrUndefined(this.doc30.components)) {
            this.doc30.components = this.doc30.createComponents();
        }
        return this.doc30.components;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.toSchema = function (from, schema30, isSchema) {
        var type = NodeCompat.getProperty(from, Constants.PROP_TYPE);
        var format = NodeCompat.getProperty(from, Constants.PROP_FORMAT);
        var items = NodeCompat.getProperty(from, Constants.PROP_ITEMS);
        var default_ = NodeCompat.getProperty(from, Constants.PROP_DEFAULT);
        var maximum = NodeCompat.getProperty(from, Constants.PROP_MAXIMUM);
        var exclusiveMaximum = NodeCompat.getProperty(from, Constants.PROP_EXCLUSIVE_MAXIMUM);
        var minimum = NodeCompat.getProperty(from, Constants.PROP_MINIMUM);
        var exclusiveMinimum = NodeCompat.getProperty(from, Constants.PROP_EXCLUSIVE_MINIMUM);
        var maxLength = NodeCompat.getProperty(from, Constants.PROP_MAX_LENGTH);
        var minLength = NodeCompat.getProperty(from, Constants.PROP_MIN_LENGTH);
        var pattern = NodeCompat.getProperty(from, Constants.PROP_PATTERN);
        var maxItems = NodeCompat.getProperty(from, Constants.PROP_MAX_ITEMS);
        var minItems = NodeCompat.getProperty(from, Constants.PROP_MIN_ITEMS);
        var uniqueItems = NodeCompat.getProperty(from, Constants.PROP_UNIQUE_ITEMS);
        var enum_ = NodeCompat.getProperty(from, Constants.PROP_ENUM);
        var multipleOf = NodeCompat.getProperty(from, Constants.PROP_MULTIPLE_OF);
        schema30.type = type;
        schema30.format = format;
        if (NodeCompat.equals(type, "file")) {
            schema30.type = "string";
            schema30.format = "binary";
        }
        if (NodeCompat.isNode(items)) {
            items.setAttribute("_transformation_items-parent", schema30);
        }
        else if (NodeCompat.isList(items)) {
        }
        schema30.default_ = default_;
        schema30.maximum = maximum;
        schema30.exclusiveMaximum = exclusiveMaximum;
        schema30.minimum = minimum;
        schema30.exclusiveMinimum = exclusiveMinimum;
        schema30.maxLength = maxLength;
        schema30.minLength = minLength;
        schema30.pattern = pattern;
        schema30.maxItems = maxItems;
        schema30.minItems = minItems;
        schema30.uniqueItems = uniqueItems;
        schema30.enum_ = enum_;
        schema30.multipleOf = multipleOf;
        if (isSchema) {
            var schema20 = from;
            schema30.$ref = this.updateRef(schema20.$ref);
            if (schema20.hasAdditionalPropertiesBoolean()) {
                schema30.additionalProperties = schema20.additionalProperties;
            }
            schema30.readOnly = schema20.readOnly;
            schema30.example = schema20.example;
            schema30.title = schema20.title;
            schema30.description = schema20.description;
            schema30.maxProperties = schema20.maxProperties;
            schema30.minProperties = schema20.minProperties;
            schema30.required = schema20.required;
            if (!NodeCompat.isNullOrUndefined(schema20.discriminator)) {
                schema30.discriminator = schema30.createDiscriminator();
                schema30.discriminator.propertyName = schema20.discriminator;
            }
        }
        return schema30;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.findItemsParent = function (node) {
        var itemsParent = node.getAttribute("_transformation_items-parent");
        if (NodeCompat.isNullOrUndefined(itemsParent)) {
            itemsParent = this.lookup(node.parent());
        }
        return itemsParent;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.findParentOperation = function (node) {
        var finder = new OperationFinder();
        Library.visitTree(node, finder, TraverserDirection.up);
        return finder.found;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.findProduces = function (node) {
        var finder = new ConsumesProducesFinder();
        Library.visitTree(node, finder, TraverserDirection.up);
        var produces = finder.produces;
        if (NodeCompat.isNullOrUndefined(produces) || /* size */ produces.length === 0) {
            produces = ([]);
            /* add */ (produces.push("*/*") > 0);
        }
        return produces;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.findConsumes = function (node) {
        var finder = new ConsumesProducesFinder();
        Library.visitTree(node, finder, TraverserDirection.up);
        var consumes = finder.consumes;
        if (NodeCompat.isNullOrUndefined(consumes) || /* size */ consumes.length === 0) {
            consumes = ([]);
            /* add */ (consumes.push("*/*") > 0);
        }
        return consumes;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.collectionFormatToStyleAndExplode = function (node, param30) {
        if (NodeCompat.equals(node.type, "array") && NodeCompat.equals(node.collectionFormat, "multi") && (NodeCompat.equals(node.in, "query") || NodeCompat.equals(node.in, "cookie"))) {
            param30.style = "form";
            param30.explode = true;
            return;
        }
        if (NodeCompat.equals(node.type, "array") && NodeCompat.equals(node.collectionFormat, "csv") && (NodeCompat.equals(node.in, "query") || NodeCompat.equals(node.in, "cookie"))) {
            param30.style = "form";
            param30.explode = false;
            return;
        }
        if (NodeCompat.equals(node.type, "array") && NodeCompat.equals(node.collectionFormat, "csv") && (NodeCompat.equals(node.in, "path") || NodeCompat.equals(node.in, "header"))) {
            param30.style = "simple";
            return;
        }
        if (NodeCompat.equals(node.type, "array") && NodeCompat.equals(node.collectionFormat, "ssv") && NodeCompat.equals(node.in, "query")) {
            param30.style = "spaceDelimited";
            return;
        }
        if (NodeCompat.equals(node.type, "array") && NodeCompat.equals(node.collectionFormat, "pipes") && NodeCompat.equals(node.in, "query")) {
            param30.style = "pipeDelimited";
            return;
        }
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.isFormDataMimeType = function (mimetype) {
        return !NodeCompat.isNullOrUndefined(mimetype) && (NodeCompat.equals(mimetype, "multipart/form-data") || NodeCompat.equals(mimetype, "application/x-www-form-urlencoded"));
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.hasFormDataMimeType = function (mimetypes) {
        if (!NodeCompat.isNullOrUndefined(mimetypes)) {
            for (var index194 = 0; index194 < mimetypes.length; index194++) {
                var mt = mimetypes[index194];
                {
                    if (this.isFormDataMimeType(mt)) {
                        return true;
                    }
                }
            }
        }
        return false;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.isRef = function (node) {
        var $ref = NodeCompat.getProperty(node, Constants.PROP_$REF);
        return !NodeCompat.isNullOrUndefined($ref) && $ref.length > 0;
    };
    /*private*/ Oas20to30TransformationVisitor.prototype.updateRef = function ($ref) {
        if (NodeCompat.isNullOrUndefined($ref) || $ref.length === 0) {
            return $ref;
        }
        var split = $ref.split("/");
        if (NodeCompat.equals(split[0], "#")) {
            if (NodeCompat.equals(split[1], "definitions")) {
                return /* replace */ $ref.split("#/definitions/").join("#/components/schemas/");
            }
            else if (NodeCompat.equals(split[1], "parameters")) {
                return /* replace */ $ref.split("#/parameters/").join("#/components/parameters/");
            }
            else if (NodeCompat.equals(split[1], "responses")) {
                return /* replace */ $ref.split("#/responses/").join("#/components/responses/");
            }
        }
        return $ref;
    };
    /**
     * Called when visiting is complete.  Any additional processing of the result can
     * be done here.
     * @private
     */
    /*private*/ Oas20to30TransformationVisitor.prototype.postProcess = function () {
        this._postProcessResponses.forEach(function (response) {
            var largest = 0;
            var srcMt = null;
            {
                var array196 = response.getMediaTypes();
                for (var index195 = 0; index195 < array196.length; index195++) {
                    var mt = array196[index195];
                    {
                        var size = JsonCompat.stringify(Library.writeNode(mt.schema)).length;
                        if (size > largest) {
                            largest = size;
                            srcMt = mt;
                        }
                    }
                }
            }
            {
                var array198 = response.getMediaTypes();
                for (var index197 = 0; index197 < array198.length; index197++) {
                    var mt = array198[index197];
                    {
                        if (srcMt !== mt) {
                            var src = Library.writeNode(srcMt.schema);
                            Library.readNode(src, mt.schema);
                        }
                    }
                }
            }
        });
    };
    return Oas20to30TransformationVisitor;
}());
export { Oas20to30TransformationVisitor };
Oas20to30TransformationVisitor["__class"] = "io.apicurio.datamodels.openapi.visitors.transform.Oas20to30TransformationVisitor";
Oas20to30TransformationVisitor["__interfaces"] = ["io.apicurio.datamodels.core.visitors.IVisitor", "io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor", "io.apicurio.datamodels.openapi.visitors.IOasVisitor"];
//# sourceMappingURL=Oas20to30TransformationVisitor.js.map