import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant";
import { valueToObjectRepresentation } from "./storeUtils.js";
// Checks the document for errors and throws an exception if there is an error.
export function checkDocument(doc, expectedType) {
    invariant(doc && doc.kind === "Document", 12);
    const operations = doc.definitions
        .filter((d) => d.kind !== "FragmentDefinition")
        .map((definition) => {
        if (definition.kind !== "OperationDefinition") {
            throw newInvariantError(13, definition.kind);
        }
        return definition;
    });
    invariant(operations.length <= 1, 14, operations.length);
    if (expectedType) {
        invariant(
            operations.length == 1 && operations[0].operation === expectedType,
            15,
            expectedType,
            expectedType,
            operations[0].operation
        );
    }
    return doc;
}
export function getOperationDefinition(doc) {
    checkDocument(doc);
    return doc.definitions.filter((definition) => definition.kind === "OperationDefinition")[0];
}
export 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
export function getFragmentDefinitions(doc) {
    return doc.definitions.filter((definition) => definition.kind === "FragmentDefinition");
}
export function getQueryDefinition(doc) {
    const queryDef = getOperationDefinition(doc);
    invariant(queryDef && queryDef.operation === "query", 16);
    return queryDef;
}
export function getFragmentDefinition(doc) {
    invariant(doc.kind === "Document", 17);
    invariant(doc.definitions.length <= 1, 18);
    const fragmentDef = doc.definitions[0];
    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.
 */
export 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 newInvariantError(20);
}
export function getDefaultValues(definition) {
    const defaultValues = {};
    const defs = definition && definition.variableDefinitions;
    if (defs && defs.length) {
        defs.forEach((def) => {
            if (def.defaultValue) {
                valueToObjectRepresentation(defaultValues, def.variable.name, def.defaultValue);
            }
        });
    }
    return defaultValues;
}
//# sourceMappingURL=getFromAST.js.map
