var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* Generated from Java with JSweet 2.3.7 - http://www.jsweet.org */
import { CombinedAllNodeVisitor } from '../../combined/visitors/CombinedAllNodeVisitor';
import { NodeCompat } from '../../compat/NodeCompat';
import { RegexCompat } from '../../compat/RegexCompat';
/**
 * Constructor.
 * @param {ValidationRuleMetaData} ruleInfo
 * @class
 * @extends CombinedAllNodeVisitor
 * @author eric.wittmann@gmail.com
 */
var ValidationRule = /** @class */ (function (_super) {
    __extends(ValidationRule, _super);
    function ValidationRule(ruleInfo) {
        var _this = _super.call(this) || this;
        (function () {
            ValidationRule.HTTP_STATUS_CODES = ( /* asList */["100", "101", "102", "1XX", "10X", "200", "201", "202", "203", "204", "205", "206", "207", "208", "226", "2XX", "20X", "21X", "22X", "300", "301", "302", "303", "304", "305", "306", "307", "308", "3XX", "30X", "400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417", "4XX", "40X", "41X", "421", "422", "423", "424", "426", "427", "428", "429", "431", "451", "42X", "43X", "44X", "45X", "500", "501", "502", "503", "504", "505", "506", "507", "508", "510", "511", "5XX", "50X", "51X"].slice(0).slice(0));
        })();
        if (_this.reporter === undefined)
            _this.reporter = null;
        if (_this.ruleInfo === undefined)
            _this.ruleInfo = null;
        _this.ruleInfo = ruleInfo;
        return _this;
    }
    /**
     * Sets the validation problem reporter.
     * @param {*} reporter
     */
    ValidationRule.prototype.setReporter = function (reporter) {
        this.reporter = reporter;
    };
    /**
     * Called by validation rules to report an error.
     * @param {Node} node
     * @param {string} property
     * @param {*} messageParams
     */
    ValidationRule.prototype.report = function (node, property, messageParams) {
        var messageTemplate = this.ruleInfo.messageTemplate;
        var message = this.resolveMessage(messageTemplate, messageParams);
        this.reporter.report(this.ruleInfo, node, property, message);
    };
    /**
     * Creates a message from a template and set of params.
     * @param {string} messageTemplate
     * @param {*} messageParams
     * @return {string}
     * @private
     */
    ValidationRule.prototype.resolveMessage = function (messageTemplate, messageParams) {
        if (messageParams == null || /* size */ Object.keys(messageParams).length === 0) {
            return messageTemplate;
        }
        var matches = RegexCompat.findMatches(messageTemplate, "\\$\\{\\s*\'\\s*([a-zA-Z0-9]+)\\s*\'\\s*\\}");
        if ( /* size */matches.length === 0) {
            return messageTemplate;
        }
        var rval = messageTemplate;
        for (var index132 = 0; index132 < matches.length; index132++) {
            var match = matches[index132];
            {
                var mval = match[0];
                var paramName = match[1];
                var paramValue = (function (m, k) { return m[k] === undefined ? null : m[k]; })(messageParams, paramName);
                var start = rval.indexOf(mval);
                var end = start + mval.length;
                rval = rval.substring(0, start) + paramValue + rval.substring(end);
            }
        }
        return rval;
    };
    /**
     * Reports a validation error if the property is not valid.
     * @param {boolean} isValid
     * @param {Node} node
     * @param {string} property
     * @param {*} messageParams
     */
    ValidationRule.prototype.reportIfInvalid = function (isValid, node, property, messageParams) {
        if (!isValid) {
            this.report(node, property, messageParams);
        }
    };
    /**
     * Reports a validation error if the given condition is true.
     * @param {boolean} condition
     * @param {Node} node
     * @param {string} property
     * @param {*} messageParams
     */
    ValidationRule.prototype.reportIf = function (condition, node, property, messageParams) {
        if (condition) {
            this.report(node, property, messageParams);
        }
    };
    /**
     * Utility function to report path related errors.
     * @param {Node} node
     * @param {*} messageParams
     */
    ValidationRule.prototype.reportPathError = function (node, messageParams) {
        this.report(node, null, messageParams);
    };
    /**
     * Check if a property was defined.
     * @param {*} propertyValue
     * @return {boolean} {boolean}
     */
    ValidationRule.prototype.isDefined = function (propertyValue) {
        return !NodeCompat.isNullOrUndefined(propertyValue);
    };
    /**
     * Check if the property value exists (is not undefined and is not null).
     * @param {*} propertyValue
     * @return {boolean} {boolean}
     */
    ValidationRule.prototype.hasValue = function (propertyValue) {
        return this.isDefined(propertyValue);
    };
    /**
     * Checks the path template against the regular expression and returns match result.
     *
     * @param {string} pathTemplate
     * @return {boolean} {boolean}
     */
    ValidationRule.prototype.isPathWellFormed = function (pathTemplate) {
        return RegexCompat.matches(pathTemplate, ValidationRule.PATH_MATCH_REGEX);
    };
    /**
     * Finds all occurences of path segment patterns in a path template.
     *
     * @param {string} pathTemplate
     * @return {ValidationRule.PathSegment[]} {PathSegment[]}
     */
    ValidationRule.prototype.getPathSegments = function (pathTemplate) {
        var pathSegments = ([]);
        if (pathTemplate == null || /* equals */ (function (o1, o2) { if (o1 && o1.equals) {
            return o1.equals(o2);
        }
        else {
            return o1 === o2;
        } })("", pathTemplate)) {
            return pathSegments;
        }
        var normalizedPath = pathTemplate;
        if (pathTemplate.lastIndexOf("/") === pathTemplate.length - 1) {
            normalizedPath = pathTemplate.substring(0, pathTemplate.length - 1);
        }
        var segId = 0;
        var matches = RegexCompat.findMatches(normalizedPath, ValidationRule.SEG_MATCH_REGEX);
        for (var index133 = 0; index133 < matches.length; index133++) {
            var mi = matches[index133];
            {
                var pathSegment = new ValidationRule.PathSegment();
                pathSegment.segId = segId;
                pathSegment.prefix = mi[1];
                var fn = mi[3];
                if (fn != null && !(fn.trim().length === 0)) {
                    pathSegment.formalName = fn;
                    pathSegment.normalizedName = "__param__" + segId;
                }
                /* add */ (pathSegments.push(pathSegment) > 0);
                segId++;
            }
        }
        return pathSegments;
    };
    /**
     * Check if a value is either null or undefined.
     * @param {*} value
     * @return {boolean} {boolean}
     */
    ValidationRule.prototype.isNullOrUndefined = function (value) {
        return NodeCompat.isNullOrUndefined(value);
    };
    /**
     * Returns true only if the given value is a valid URL.
     * @param {string} propertyValue
     * @return {boolean} {boolean}
     */
    ValidationRule.prototype.isValidUrl = function (propertyValue) {
        return propertyValue.length < 2083 && RegexCompat.matches(propertyValue, ValidationRule.URL_MATCH_REGEX);
    };
    /**
     * Returns true only if the given value is a valid URL template.
     * @param {string} propertyValue
     * @return {boolean}
     */
    ValidationRule.prototype.isValidUrlTemplate = function (propertyValue) {
        return true;
    };
    /**
     * Returns true only if the given value is valid GFM style markup.
     * @param {string} propertyValue
     * @return {boolean}
     */
    ValidationRule.prototype.isValidGFM = function (propertyValue) {
        return true;
    };
    /**
     * Returns true only if the given value is valid CommonMark style markup.
     * @param {string} propertyValue
     * @return {boolean}
     */
    ValidationRule.prototype.isValidCommonMark = function (propertyValue) {
        return true;
    };
    /**
     * Returns true only if the given value is a valid email address.
     * @param {string} propertyValue
     * @return {boolean}
     */
    ValidationRule.prototype.isValidEmailAddress = function (propertyValue) {
        return RegexCompat.matches(propertyValue, ValidationRule.EMAIL_MATCH_REGEX);
    };
    /**
     * Returns true only if the given value is a valid mime-type.
     * @param {string[]} propertyValue
     * @return {boolean}
     */
    ValidationRule.prototype.isValidMimeType = function (propertyValue) {
        for (var index134 = 0; index134 < propertyValue.length; index134++) {
            var v = propertyValue[index134];
            {
                if (!RegexCompat.matches(v, ValidationRule.MIME_TYPE_MATCH_REGEX)) {
                    return false;
                }
            }
        }
        return true;
    };
    /**
     * Returns true if the given value is an item in the enum list.
     * @param {string} value
     * @param {Array} items
     * @return {boolean}
     */
    ValidationRule.prototype.isValidEnumItem = function (value, items) {
        for (var index135 = 0; index135 < items.length; index135++) {
            var item = items[index135];
            {
                if (item != null && /* equals */ (function (o1, o2) { if (o1 && o1.equals) {
                    return o1.equals(o2);
                }
                else {
                    return o1 === o2;
                } })(item, value)) {
                    return true;
                }
            }
        }
        return false;
    };
    /**
     * Returns true if the given value is valid according to the schema provided.
     * @param {*} value
     * @param {Node} node
     * @return {boolean}
     */
    ValidationRule.prototype.isValidForType = function (value, node) {
        return true;
    };
    /**
     * Returns true if the given status code is a valid HTTP response code.
     * @param {string} statusCode
     * @return {boolean}
     */
    ValidationRule.prototype.isValidHttpCode = function (statusCode) {
        return ValidationRule.HTTP_STATUS_CODES.indexOf(statusCode) !== -1;
    };
    /**
     * Turns a list of args into a map suitable for use as template arguments.  The args must come
     * in pairs of "key, value".  For example:
     *
     * map("key1", "value1", "key2", Boolean.TRUE);
     *
     * That would return a {@link Map} two mappings.
     * @param {Array} args
     * @return {*}
     */
    ValidationRule.prototype.map = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        if (args == null || args.length < 2) {
            return null;
        }
        var rval = ({});
        for (var idx = 0; idx < args.length; idx += 2) {
            {
                var key = args[idx].toString();
                var value = args[idx + 1];
                /* put */ (rval[key] = value);
            }
            ;
        }
        return rval;
    };
    /**
     * Creates an array.
     * @param {Array} args
     * @return {Array}
     */
    ValidationRule.prototype.array = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        return args;
    };
    /**
     * Returns true if the two values are equal.
     * @param {*} value1
     * @param {*} value2
     * @return {boolean}
     */
    ValidationRule.prototype.equals = function (value1, value2) {
        return NodeCompat.equals(value1, value2);
    };
    /**
     * List of valid HTTP response status codes from:  https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
     */
    ValidationRule.HTTP_STATUS_CODES = null;
    ValidationRule.PATH_MATCH_REGEX = "^(\\/[^{}\\/]*(\\{[a-zA-Z_][0-9a-zA-Z_]*\\})?)+$";
    ValidationRule.SEG_MATCH_REGEX = "\\/([^{}\\/]*)(\\{([a-zA-Z_][0-9a-zA-Z_]*)\\})?";
    ValidationRule.URL_MATCH_REGEX = "^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$";
    ValidationRule.EMAIL_MATCH_REGEX = "^[\\w!#$%&\'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&\'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
    ValidationRule.MIME_TYPE_MATCH_REGEX = "^.*\\/.*(;.*)?$";
    return ValidationRule;
}(CombinedAllNodeVisitor));
export { ValidationRule };
ValidationRule["__class"] = "io.apicurio.datamodels.core.validation.ValidationRule";
ValidationRule["__interfaces"] = ["io.apicurio.datamodels.core.visitors.IVisitor", "io.apicurio.datamodels.asyncapi.v2.visitors.IAai20Visitor", "io.apicurio.datamodels.openapi.v3.visitors.IOas30Visitor", "io.apicurio.datamodels.asyncapi.visitors.IAaiVisitor", "io.apicurio.datamodels.openapi.v2.visitors.IOas20Visitor", "io.apicurio.datamodels.openapi.visitors.IOasVisitor"];
(function (ValidationRule) {
    /**
     * Type encapsulating information about a path segment.
     * Consumers of this type should not rely on normalizedName property which is only provided to weed out duplicates.
     * @class
     */
    var PathSegment = /** @class */ (function () {
        function PathSegment() {
            if (this.segId === undefined)
                this.segId = 0;
            if (this.prefix === undefined)
                this.prefix = null;
            if (this.formalName === undefined)
                this.formalName = null;
            if (this.normalizedName === undefined)
                this.normalizedName = null;
        }
        return PathSegment;
    }());
    ValidationRule.PathSegment = PathSegment;
    PathSegment["__class"] = "io.apicurio.datamodels.core.validation.ValidationRule.PathSegment";
})(ValidationRule || (ValidationRule = {}));
//# sourceMappingURL=ValidationRule.js.map