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 { ReferenceUtil } from '../../../util/ReferenceUtil';
import { ValidationRule } from '../../ValidationRule';
import { OasParameter } from '../../../../openapi/models/OasParameter';
/**
 * Constructor.
 * @param {ValidationRuleMetaData} ruleInfo
 * @class
 * @extends ValidationRule
 * @author eric.wittmann@gmail.com
 */
var OasParameterUniquenessValidationRule = /** @class */ (function (_super) {
    __extends(OasParameterUniquenessValidationRule, _super);
    function OasParameterUniquenessValidationRule(ruleInfo) {
        return _super.call(this, ruleInfo) || this;
    }
    /**
     * Given a 'in' and a 'name' for a parameter, return the # of parameters in the list
     * of parameters that match.
     * @param {OasParameter[]} params
     * @param {string} paramName
     * @param {string} paramIn
     * @return {number}
     * @private
     */
    /*private*/ OasParameterUniquenessValidationRule.prototype.getParamCount = function (params, paramName, paramIn) {
        var count = 0;
        for (var index156 = 0; index156 < params.length; index156++) {
            var param = params[index156];
            {
                if (this.hasValue(param) && this.equals(param.name, paramName) && this.equals(param.in, paramIn)) {
                    count++;
                }
            }
        }
        return count;
    };
    /**
     * Validates that all parameter name and "in" combinations are unique
     * @param {OasParameter[]} params
     * @private
     */
    /*private*/ OasParameterUniquenessValidationRule.prototype.ensureUnique = function (params) {
        if (!this.hasValue(params)) {
            return;
        }
        var resolvedParams = ([]);
        params.forEach((function (resolvedParams) {
            return function (param) {
                var resolveNodeRef = ReferenceUtil.resolveNodeRef$io_apicurio_datamodels_core_models_Node(param);
                if (resolveNodeRef != null && resolveNodeRef instanceof OasParameter) {
                    /* add */ (resolvedParams.push(resolveNodeRef) > 0);
                }
                else {
                    /* add */ (resolvedParams.push(param) > 0);
                }
            };
        })(resolvedParams));
        var idx = 0;
        for (var index157 = 0; index157 < resolvedParams.length; index157++) {
            var resolvedParam = resolvedParams[index157];
            {
                if (this.hasValue(resolvedParam) && this.hasValue(resolvedParam.in) && this.hasValue(resolvedParam.name) && !this.equals(resolvedParam.in, "body")) {
                    var count = this.getParamCount(resolvedParams, resolvedParam.name, resolvedParam.in);
                    if (count > 1) {
                        var param = params[idx];
                        this.report(param, "in", this.map("paramIn", resolvedParam.in, "paramName", resolvedParam.name));
                    }
                }
                idx++;
            }
        }
    };
    /**
     * @see io.apicurio.datamodels.combined.visitors.CombinedAllNodeVisitor#visitPathItem(io.apicurio.datamodels.openapi.models.OasPathItem)
     * @param {OasPathItem} node
     */
    OasParameterUniquenessValidationRule.prototype.visitPathItem = function (node) {
        this.ensureUnique(node.parameters);
    };
    /**
     * @see io.apicurio.datamodels.combined.visitors.CombinedAllNodeVisitor#visitOperation(io.apicurio.datamodels.core.models.common.Operation)
     * @param {Operation} node
     */
    OasParameterUniquenessValidationRule.prototype.visitOperation = function (node) {
        var op = node;
        this.ensureUnique(op.parameters);
    };
    return OasParameterUniquenessValidationRule;
}(ValidationRule));
export { OasParameterUniquenessValidationRule };
OasParameterUniquenessValidationRule["__class"] = "io.apicurio.datamodels.core.validation.rules.other.OasParameterUniquenessValidationRule";
OasParameterUniquenessValidationRule["__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=OasParameterUniquenessValidationRule.js.map