"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeAndPrintDocumentNode = exports.generateDocumentHash = void 0;
const tslib_1 = require("tslib");
const crypto = tslib_1.__importStar(require("crypto"));
const documents_1 = require("@graphql-tools/documents");
const graphql_1 = require("graphql");
/**
 * This function generates a hash from a document node.
 */
function generateDocumentHash(operation, algorithm) {
    const shasum = crypto.createHash(algorithm);
    shasum.update(operation);
    return shasum.digest('hex');
}
exports.generateDocumentHash = generateDocumentHash;
/**
 * Normalizes and prints a document node.
 */
function normalizeAndPrintDocumentNode(documentNode) {
    /**
     * This removes all client specific directives/fields from the document
     * that the server does not know about.
     * In a future version this should be more configurable.
     * If you look at this and want to customize it.
     * Send a PR :)
     */
    const sanitizedDocument = (0, graphql_1.visit)(documentNode, {
        [graphql_1.Kind.FIELD](field) {
            if (field.directives?.some(directive => directive.name.value === 'client')) {
                return null;
            }
        },
        [graphql_1.Kind.DIRECTIVE](directive) {
            if (directive.name.value === 'connection') {
                return null;
            }
        },
    });
    return (0, documents_1.printExecutableGraphQLDocument)(sanitizedDocument);
}
exports.normalizeAndPrintDocumentNode = normalizeAndPrintDocumentNode;
