"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var stringSimilarity = require("string-similarity");
var graphql_1 = require("../utils/graphql");
function similar(schema, typeName, threshold) {
    if (threshold === void 0) { threshold = 0.4; }
    var types = Object.keys(schema.getTypeMap()).filter(function (name) { return !graphql_1.isPrimitive(name) && !graphql_1.isCommon(name); });
    var results = {};
    if (typeof typeName !== 'undefined' && types.indexOf(typeName) === -1) {
        throw new Error("Type '" + typeName + "' doesn't exist");
    }
    (typeName ? [typeName] : types).forEach(function (name) {
        var found = similarTo(schema.getType(name), types, schema, threshold);
        if (found) {
            results[name] = found;
        }
    });
    return results;
}
exports.similar = similar;
function similarTo(type, typeNames, schema, threshold) {
    var typeMap = schema.getTypeMap();
    var types = typeNames
        .filter(function (name) { return name !== type.name; })
        .map(function (name) { return stripType(typeMap[name].toString()); });
    var result = stringSimilarity.findBestMatch(stripType(type.toString()), types);
    if (result.bestMatch.rating < threshold) {
        return;
    }
    return __assign({}, result, { ratings: result.ratings.filter(function (r) { return r.rating >= threshold && r.target !== result.bestMatch.target; }) });
}
function stripType(type) {
    return type
        .trim()
        .replace(/(^[a-z]+ [^\{]+{)|(\}$)g/, '')
        .trim();
}
//# sourceMappingURL=index.js.map