"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
var document_1 = require("../ast/document");
var graphql_2 = require("../utils/graphql");
function coverage(schema, sources) {
    var coverage = {
        sources: sources,
        types: {},
    };
    var typeMap = schema.getTypeMap();
    var typeInfo = new graphql_1.TypeInfo(schema);
    var visitor = function (source) { return ({
        Field: function (node) {
            var fieldDef = typeInfo.getFieldDef();
            var parent = typeInfo.getParentType();
            if (parent && fieldDef) {
                var locations = coverage.types[parent.name].children[fieldDef.name].locations[source.name];
                coverage.types[parent.name].hits++;
                coverage.types[parent.name].children[fieldDef.name].locations[source.name] = [node.loc].concat(locations || []);
                coverage.types[parent.name].children[fieldDef.name].hits++;
            }
        },
    }); };
    for (var typename in typeMap) {
        if (!graphql_2.isForIntrospection(typename) && !graphql_2.isPrimitive(typename)) {
            var type = typeMap[typename];
            if (graphql_1.isObjectType(type)) {
                var typeCoverage = {
                    hits: 0,
                    type: type,
                    children: {},
                };
                var fieldMap = type.getFields();
                for (var fieldname in fieldMap) {
                    var field = fieldMap[fieldname];
                    typeCoverage.children[field.name] = {
                        hits: 0,
                        locations: {},
                    };
                }
                coverage.types[type.name] = typeCoverage;
            }
        }
    }
    var documents = coverage.sources.map(document_1.readDocument);
    documents.forEach(function (doc, i) {
        var source = coverage.sources[i];
        doc.operations.forEach(function (op) {
            graphql_1.visit(op.node, graphql_1.visitWithTypeInfo(typeInfo, visitor(source)));
        });
        doc.fragments.forEach(function (fr) {
            graphql_1.visit(fr.node, graphql_1.visitWithTypeInfo(typeInfo, visitor(source)));
        });
    });
    return coverage;
}
exports.coverage = coverage;
//# sourceMappingURL=index.js.map