"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkDocument = checkDocument;
exports.getOperationDefinition = getOperationDefinition;
exports.getOperationName = getOperationName;
exports.getFragmentDefinitions = getFragmentDefinitions;
exports.getQueryDefinition = getQueryDefinition;
exports.getFragmentDefinition = getFragmentDefinition;
exports.getMainDefinition = getMainDefinition;
exports.getDefaultValues = getDefaultValues;
const invariant_1 = require("@apollo/client/utilities/invariant");
const storeUtils_js_1 = require("./storeUtils.cjs");
// Checks the document for errors and throws an exception if there is an error.
function checkDocument(doc, expectedType) {
    (0, invariant_1.invariant)(doc && doc.kind === "Document", 12);
    const operations = doc.definitions
        .filter((d) => d.kind !== "FragmentDefinition")
        .map((definition) => {
        if (definition.kind !== "OperationDefinition") {
            throw (0, invariant_1.newInvariantError)(13, definition.kind);
        }
        return definition;
    });
    (0, invariant_1.invariant)(operations.length <= 1, 14, operations.length);
    if (expectedType) {
        (0, invariant_1.invariant)(
            operations.length == 1 && operations[0].operation === expectedType,
            15,
            expectedType,
            expectedType,
            operations[0].operation
        );
    }
    return doc;
}
function getOperationDefinition(doc) {
    checkDocument(doc);
    return doc.definitions.filter((definition) => definition.kind === "OperationDefinition")[0];
}
function getOperationName(doc) {
    return (doc.definitions
        .filter((definition) => definition.kind === "OperationDefinition" && !!definition.name)
        .map((x) => x.name.value)[0] || null);
}
// Returns the FragmentDefinitions from a particular document as an array
function getFragmentDefinitions(doc) {
    return doc.definitions.filter((definition) => definition.kind === "FragmentDefinition");
}
function getQueryDefinition(doc) {
    const queryDef = getOperationDefinition(doc);
    (0, invariant_1.invariant)(queryDef && queryDef.operation === "query", 16);
    return queryDef;
}
function getFragmentDefinition(doc) {
    (0, invariant_1.invariant)(doc.kind === "Document", 17);
    (0, invariant_1.invariant)(doc.definitions.length <= 1, 18);
    const fragmentDef = doc.definitions[0];
    (0, invariant_1.invariant)(fragmentDef.kind === "FragmentDefinition", 19);
    return fragmentDef;
}
/**
 * Returns the first operation definition found in this document.
 * If no operation definition is found, the first fragment definition will be returned.
 * If no definitions are found, an error will be thrown.
 */
function getMainDefinition(queryDoc) {
    checkDocument(queryDoc);
    let fragmentDefinition;
    for (let definition of queryDoc.definitions) {
        if (definition.kind === "OperationDefinition") {
            const operation = definition.operation;
            if (operation === "query" ||
                operation === "mutation" ||
                operation === "subscription") {
                return definition;
            }
        }
        if (definition.kind === "FragmentDefinition" && !fragmentDefinition) {
            // we do this because we want to allow multiple fragment definitions
            // to precede an operation definition.
            fragmentDefinition = definition;
        }
    }
    if (fragmentDefinition) {
        return fragmentDefinition;
    }
    throw (0, invariant_1.newInvariantError)(20);
}
function getDefaultValues(definition) {
    const defaultValues = {};
    const defs = definition && definition.variableDefinitions;
    if (defs && defs.length) {
        defs.forEach((def) => {
            if (def.defaultValue) {
                (0, storeUtils_js_1.valueToObjectRepresentation)(defaultValues, def.variable.name, def.defaultValue);
            }
        });
    }
    return defaultValues;
}
//# sourceMappingURL=getFromAST.cjs.map
