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 __());
    };
})();
import { OasInvalidPropertyNameRule } from './OasInvalidPropertyNameRule';
/**
 * Constructor.
 * @param {ValidationRuleMetaData} ruleInfo
 * @class
 * @extends OasInvalidPropertyNameRule
 * @author eric.wittmann@gmail.com
 */
var OasIdenticalPathTemplateRule = /** @class */ (function (_super) {
    __extends(OasIdenticalPathTemplateRule, _super);
    function OasIdenticalPathTemplateRule(ruleInfo) {
        var _this = _super.call(this, ruleInfo) || this;
        _this.indexedPathTemplates = ({});
        return _this;
    }
    /**
     * Utility function to find other paths that are semantically similar to the path that is being checked against.
     * Two paths that differ only in formal parameter name are considered identical.
     * For example, paths /test/{var1} and /test/{var2} are identical.
     * See OAS 3 Specification's Path Templates section for more details.
     *
     * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#path-templating-matching
     *
     * @param {string} pathToCheck
     * @param pathIndex
     * @return {string[]}
     * @private
     */
    /*private*/ OasIdenticalPathTemplateRule.prototype.findIdenticalPaths = function (pathToCheck) {
        var identicalPaths = ([]);
        var pathSegments = (function (m, k) { return m[k] === undefined ? null : m[k]; })(this.indexedPathTemplates, pathToCheck);
        {
            var array150 = /* keySet */ Object.keys(this.indexedPathTemplates);
            for (var index149 = 0; index149 < array150.length; index149++) {
                var checkAgainst = array150[index149];
                {
                    if (this.equals(checkAgainst, pathToCheck)) {
                        continue;
                    }
                    var segmentsIdentical = true;
                    var pathSegmentsToCheckAgainst = (function (m, k) { return m[k] === undefined ? null : m[k]; })(this.indexedPathTemplates, checkAgainst);
                    if ( /* size */pathSegments.length !== /* size */ pathSegmentsToCheckAgainst.length) {
                        segmentsIdentical = false;
                    }
                    else {
                        var idx = 0;
                        for (var index151 = 0; index151 < pathSegments.length; index151++) {
                            var pathSegment = pathSegments[index151];
                            {
                                segmentsIdentical = segmentsIdentical && this.isSegmentIdentical(pathSegment, /* get */ pathSegmentsToCheckAgainst[idx]);
                                idx++;
                            }
                        }
                    }
                    if (segmentsIdentical) {
                        /* add */ (identicalPaths.push(checkAgainst) > 0);
                    }
                }
            }
        }
        return identicalPaths;
    };
    /**
     * Utility function to test the equality of two path segments.
     * Segments are considered equal if they have same prefixes (if any) and same "normalized name".
     *
     * @param {ValidationRule.PathSegment} segment1
     * @param {ValidationRule.PathSegment} segment2
     * @return {boolean} {boolean}
     * @private
     */
    /*private*/ OasIdenticalPathTemplateRule.prototype.isSegmentIdentical = function (segment1, segment2) {
        if (this.equals(segment1.prefix, segment2.prefix)) {
            if (!this.hasValue(segment1.normalizedName) && !this.hasValue(segment2.normalizedName)) {
                return true;
            }
            if ((!this.hasValue(segment1.normalizedName) && this.hasValue(segment2.normalizedName)) || (this.hasValue(segment1.normalizedName) && !this.hasValue(segment2.normalizedName))) {
                return false;
            }
            return this.equals(segment1.normalizedName, segment2.normalizedName);
        }
        return false;
    };
    /**
     * @see io.apicurio.datamodels.combined.visitors.CombinedAllNodeVisitor#visitPaths(io.apicurio.datamodels.openapi.models.OasPaths)
     * @param {OasPaths} node
     */
    OasIdenticalPathTemplateRule.prototype.visitPaths = function (node) {
        var _this = this;
        node.getPathItems().forEach(function (pathItem) {
            var pathTemplate = pathItem.getPath();
            if (_this.isPathWellFormed(pathTemplate)) {
                var pathSegments = _this.getPathSegments(pathTemplate);
                /* put */ (_this.indexedPathTemplates[pathTemplate] = pathSegments);
            }
        });
    };
    /**
     * @see io.apicurio.datamodels.combined.visitors.CombinedAllNodeVisitor#visitPathItem(io.apicurio.datamodels.openapi.models.OasPathItem)
     * @param {OasPathItem} node
     */
    OasIdenticalPathTemplateRule.prototype.visitPathItem = function (node) {
        var pathTemplate = node.getPath();
        if (this.isPathWellFormed(pathTemplate)) {
            var identicalPaths = this.findIdenticalPaths(pathTemplate);
            if ( /* size */identicalPaths.length > 0) {
                this.reportPathError(node, this.map("path", node.getPath()));
            }
        }
    };
    return OasIdenticalPathTemplateRule;
}(OasInvalidPropertyNameRule));
export { OasIdenticalPathTemplateRule };
OasIdenticalPathTemplateRule["__class"] = "io.apicurio.datamodels.core.validation.rules.invalid.name.OasIdenticalPathTemplateRule";
OasIdenticalPathTemplateRule["__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"];
//# sourceMappingURL=OasIdenticalPathTemplateRule.js.map