import { JsonCompat } from '../../compat/JsonCompat';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { Constants } from '../Constants';
/**
 * Constructor.
 * @class
 * @author eric.wittmann@gmail.com
 */
var DataModelReader = /** @class */ (function () {
    function DataModelReader() {
    }
    /**
     * Reads the extension properties from the input and stores them in the extensible node.
     * @param {*} json
     * @param {ExtensibleNode} node
     */
    DataModelReader.prototype.readExtensions = function (json, node) {
        JsonCompat.keys(json).forEach(function (key) {
            if ( /* startsWith */(function (str, searchString, position) {
                if (position === void 0) { position = 0; }
                return str.substr(position, searchString.length) === searchString;
            })(key, Constants.EXTENSION_PREFIX)) {
                var extension = node.createExtension();
                extension.name = key;
                extension.value = JsonCompat.consumePropertyObject(json, key);
                node.addExtension(key, extension);
            }
        });
    };
    /**
     * Reads all remaining properties.  Anything left is an "extra" (or unexpected) property.  These
     * are not extension properties - they are actually properties that SHOULD NOT have existed on
     * the node.  (all extension properties must start with "x-" and are consumed by "readExtensions".
     * @param {*} json
     * @param {Node} node
     */
    DataModelReader.prototype.readExtraProperties = function (json, node) {
        JsonCompat.keys(json).forEach(function (key) {
            var value = JsonCompat.consumePropertyObject(json, key);
            node.addExtraProperty(key, value);
            LoggerCompat.warn("Found unexpected data model property: %s", key);
        });
    };
    /**
     * Reads the root document.
     * @param {*} json
     * @param {Document} node
     */
    DataModelReader.prototype.readDocument = function (json, node) {
        var info = JsonCompat.consumeProperty(json, Constants.PROP_INFO);
        var tags = JsonCompat.consumePropertyArray(json, Constants.PROP_TAGS);
        var externalDocs = JsonCompat.consumeProperty(json, Constants.PROP_EXTERNAL_DOCS);
        if (info != null) {
            node.info = node.createInfo();
            this.readInfo(info, node.info);
        }
        if (tags != null) {
            var tagModels = ([]);
            for (var index131 = 0; index131 < tags.length; index131++) {
                var tag = tags[index131];
                {
                    var tagModel = node.createTag();
                    this.readTag(tag, tagModel);
                    /* add */ (tagModels.push(tagModel) > 0);
                }
            }
            node.tags = tagModels;
        }
        if (externalDocs != null) {
            node.externalDocs = node.createExternalDocumentation();
            this.readExternalDocumentation(externalDocs, node.externalDocs);
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads an info object into a data model instance (info node).
     * @param {*} json
     * @param {Info} node
     */
    DataModelReader.prototype.readInfo = function (json, node) {
        var title = JsonCompat.consumePropertyString(json, Constants.PROP_TITLE);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var termsOfService = JsonCompat.consumePropertyString(json, Constants.PROP_TERMS_OF_SERVICE);
        var contact = JsonCompat.consumeProperty(json, Constants.PROP_CONTACT);
        var license = JsonCompat.consumeProperty(json, Constants.PROP_LICENSE);
        var version = JsonCompat.consumePropertyString(json, Constants.PROP_VERSION);
        node.title = title;
        node.description = description;
        node.termsOfService = termsOfService;
        node.version = version;
        if (contact != null) {
            node.contact = node.createContact();
            this.readContact(contact, node.contact);
        }
        if (license != null) {
            node.license = node.createLicense();
            this.readLicense(license, node.license);
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a contact object info into the data model.
     * @param {*} json
     * @param {Contact} node
     */
    DataModelReader.prototype.readContact = function (json, node) {
        var name = JsonCompat.consumePropertyString(json, Constants.PROP_NAME);
        var url = JsonCompat.consumePropertyString(json, Constants.PROP_URL);
        var email = JsonCompat.consumePropertyString(json, Constants.PROP_EMAIL);
        node.name = name;
        node.url = url;
        node.email = email;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a license object info into the data model.
     * @param {*} json
     * @param {License} node
     */
    DataModelReader.prototype.readLicense = function (json, node) {
        var name = JsonCompat.consumePropertyString(json, Constants.PROP_NAME);
        var url = JsonCompat.consumePropertyString(json, Constants.PROP_URL);
        node.name = name;
        node.url = url;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads an tag object into a data model instance (tag node).
     * @param {*} json
     * @param {Tag} node
     */
    DataModelReader.prototype.readTag = function (json, node) {
        var name = JsonCompat.consumePropertyString(json, Constants.PROP_NAME);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var externalDocs = JsonCompat.consumeProperty(json, Constants.PROP_EXTERNAL_DOCS);
        node.name = name;
        node.description = description;
        if (externalDocs != null) {
            var externalDocsModel = node.createExternalDocumentation();
            this.readExternalDocumentation(externalDocs, externalDocsModel);
            node.externalDocs = externalDocsModel;
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a server model.
     * @param {*} json
     * @param {Server} node
     */
    DataModelReader.prototype.readServer = function (json, node) {
        var _this = this;
        var url = JsonCompat.consumePropertyString(json, Constants.PROP_URL);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var variables = JsonCompat.consumeProperty(json, Constants.PROP_VARIABLES);
        node.url = url;
        node.description = description;
        if (variables != null) {
            JsonCompat.keys(variables).forEach((function (variables) {
                return function (key) {
                    var serverVariable = JsonCompat.consumeProperty(variables, key);
                    var serverVariableModel = node.createServerVariable(key);
                    _this.readServerVariable(serverVariable, serverVariableModel);
                    node.addServerVariable(key, serverVariableModel);
                };
            })(variables));
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a server variable into the data model.
     * @param {*} json
     * @param {ServerVariable} node
     */
    DataModelReader.prototype.readServerVariable = function (json, node) {
        var enum_ = JsonCompat.consumePropertyStringArray(json, Constants.PROP_ENUM);
        var default_ = JsonCompat.consumePropertyString(json, Constants.PROP_DEFAULT);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        node.enum_ = enum_;
        node.default_ = default_;
        node.description = description;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a security requirement.
     * @param {*} json
     * @param {SecurityRequirement} node
     */
    DataModelReader.prototype.readSecurityRequirement = function (json, node) {
        JsonCompat.keys(json).forEach(function (key) {
            var scopes = JsonCompat.consumePropertyStringArray(json, key);
            node.addSecurityRequirementItem(key, scopes);
        });
    };
    /**
     * Reads a security scheme.
     * @param {*} json
     * @param {SecurityScheme} node
     */
    DataModelReader.prototype.readSecurityScheme = function (json, node) {
        var type = JsonCompat.consumePropertyString(json, Constants.PROP_TYPE);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var name = JsonCompat.consumePropertyString(json, Constants.PROP_NAME);
        var __in = JsonCompat.consumePropertyString(json, Constants.PROP_IN);
        node.type = type;
        node.description = description;
        node.name = name;
        node.in = __in;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads an external documentation into the data model.
     * @param {*} json
     * @param {ExternalDocumentation} node
     */
    DataModelReader.prototype.readExternalDocumentation = function (json, node) {
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var url = JsonCompat.consumePropertyString(json, Constants.PROP_URL);
        node.description = description;
        node.url = url;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a schema.
     * @param {*} json
     * @param {Schema} node
     */
    DataModelReader.prototype.readSchema = function (json, node) {
        var $ref = JsonCompat.consumePropertyString(json, Constants.PROP_$REF);
        node.$ref = $ref;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a single parameter.
     * @param {*} json
     * @param {AaiParameter} node
     */
    DataModelReader.prototype.readAaiParameter = function (json, node) {
        var $ref = JsonCompat.consumePropertyString(json, Constants.PROP_$REF);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var schema = JsonCompat.consumeProperty(json, Constants.PROP_SCHEMA);
        var location = JsonCompat.consumePropertyString(json, Constants.PROP_LOCATION);
        node.$ref = $ref;
        node.description = description;
        node.schema = schema;
        node.location = location;
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a single parameter.
     * @param {*} json
     * @param {Parameter} node
     */
    DataModelReader.prototype.readParameter = function (json, node) {
        var $ref = JsonCompat.consumePropertyString(json, Constants.PROP_$REF);
        var name = JsonCompat.consumePropertyString(json, Constants.PROP_NAME);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var schema = JsonCompat.consumeProperty(json, Constants.PROP_SCHEMA);
        node.$ref = $ref;
        node.name = name;
        node.description = description;
        if (schema != null) {
            node.schema = node.createSchema();
            this.readSchema(schema, node.schema);
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads a single operation.
     * @param {*} json
     * @param {Operation} node
     */
    DataModelReader.prototype.readOperation = function (json, node) {
        var operationId = JsonCompat.consumePropertyString(json, Constants.PROP_OPERATION_ID);
        var summary = JsonCompat.consumePropertyString(json, Constants.PROP_SUMMARY);
        var description = JsonCompat.consumePropertyString(json, Constants.PROP_DESCRIPTION);
        var externalDocs = JsonCompat.consumeProperty(json, Constants.PROP_EXTERNAL_DOCS);
        node.operationId = operationId;
        node.summary = summary;
        node.description = description;
        if (externalDocs != null) {
            node.externalDocs = node.createExternalDocumentation();
            this.readExternalDocumentation(externalDocs, node.externalDocs);
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads an OAS 3.0 OAuth Flows object from the given JS data.
     * @param {*} json
     * @param {OAuthFlows} node
     */
    DataModelReader.prototype.readOAuthFlows = function (json, node) {
        var implicit = JsonCompat.consumeProperty(json, Constants.PROP_IMPLICIT);
        var password = JsonCompat.consumeProperty(json, Constants.PROP_PASSWORD);
        var clientCredentials = JsonCompat.consumeProperty(json, Constants.PROP_CLIENT_CREDENTIALS);
        var authorizationCode = JsonCompat.consumeProperty(json, Constants.PROP_AUTHORIZATION_CODE);
        if (implicit != null) {
            node.implicit = node.createImplicitOAuthFlow();
            this.readOAuthFlow(implicit, node.implicit);
        }
        if (password != null) {
            node.password = node.createPasswordOAuthFlow();
            this.readOAuthFlow(password, node.password);
        }
        if (clientCredentials != null) {
            node.clientCredentials = node.createClientCredentialsOAuthFlow();
            this.readOAuthFlow(clientCredentials, node.clientCredentials);
        }
        if (authorizationCode != null) {
            node.authorizationCode = node.createAuthorizationCodeOAuthFlow();
            this.readOAuthFlow(authorizationCode, node.authorizationCode);
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    /**
     * Reads an OAS 3.0 OAuth Flow object from the given JS data.
     * @param {*} json
     * @param {OAuthFlow} node
     */
    DataModelReader.prototype.readOAuthFlow = function (json, node) {
        var authorizationUrl = JsonCompat.consumePropertyString(json, Constants.PROP_AUTHORIZATION_URL);
        var tokenUrl = JsonCompat.consumePropertyString(json, Constants.PROP_TOKEN_URL);
        var refreshUrl = JsonCompat.consumePropertyString(json, Constants.PROP_REFRESH_URL);
        var scopes = JsonCompat.consumeProperty(json, Constants.PROP_SCOPES);
        node.authorizationUrl = authorizationUrl;
        node.tokenUrl = tokenUrl;
        node.refreshUrl = refreshUrl;
        if (scopes != null) {
            JsonCompat.keys(scopes).forEach((function (scopes) {
                return function (scope) {
                    var description = JsonCompat.consumePropertyString(scopes, scope);
                    node.addScope(scope, description);
                };
            })(scopes));
        }
        this.readExtensions(json, node);
        this.readExtraProperties(json, node);
    };
    return DataModelReader;
}());
export { DataModelReader };
DataModelReader["__class"] = "io.apicurio.datamodels.core.io.DataModelReader";
//# sourceMappingURL=DataModelReader.js.map