import { BREAK, Kind, visit } from "graphql";
import { __DEV__ } from "@apollo/client/utilities/environment";
import { invariant } from "@apollo/client/utilities/invariant";
export function shouldInclude({ directives }, variables) {
    if (!directives || !directives.length) {
        return true;
    }
    return getInclusionDirectives(directives).every(({ directive, ifArgument }) => {
        let evaledValue = false;
        if (ifArgument.value.kind === "Variable") {
            evaledValue =
                variables && variables[ifArgument.value.name.value];
            invariant(evaledValue !== void 0, 2, directive.name.value);
        }
        else {
            evaledValue = ifArgument.value.value;
        }
        return directive.name.value === "skip" ? !evaledValue : evaledValue;
    });
}
export function getDirectiveNames(root) {
    const names = [];
    visit(root, {
        Directive(node) {
            names.push(node.name.value);
        },
    });
    return names;
}
export const hasAnyDirectives = (names, root) => hasDirectives(names, root, false);
export const hasAllDirectives = (names, root) => hasDirectives(names, root, true);
export function hasDirectives(names, root, all) {
    const nameSet = new Set(names);
    const uniqueCount = nameSet.size;
    visit(root, {
        Directive(node) {
            if (nameSet.delete(node.name.value) && (!all || !nameSet.size)) {
                return BREAK;
            }
        },
    });
    // If we found all the names, nameSet will be empty. If we only care about
    // finding some of them, the < condition is sufficient.
    return all ? !nameSet.size : nameSet.size < uniqueCount;
}
export function hasClientExports(document) {
    return document && hasDirectives(["client", "export"], document, true);
}
function isInclusionDirective({ name: { value } }) {
    return value === "skip" || value === "include";
}
export function getInclusionDirectives(directives) {
    const result = [];
    if (directives && directives.length) {
        directives.forEach((directive) => {
            if (!isInclusionDirective(directive))
                return;
            const directiveArguments = directive.arguments;
            const directiveName = directive.name.value;
            invariant(directiveArguments && directiveArguments.length === 1, 3, directiveName);
            const ifArgument = directiveArguments[0];
            invariant(ifArgument.name && ifArgument.name.value === "if", 4, directiveName);
            const ifValue = ifArgument.value;
            // means it has to be a variable value if this is a valid @skip or @include directive
            invariant(ifValue &&
                (ifValue.kind === "Variable" || ifValue.kind === "BooleanValue"), 5, directiveName);
            result.push({ directive, ifArgument });
        });
    }
    return result;
}
/** @internal */
export function getFragmentMaskMode(fragment) {
    const directive = fragment.directives?.find(({ name }) => name.value === "unmask");
    if (!directive) {
        return "mask";
    }
    const modeArg = directive.arguments?.find(({ name }) => name.value === "mode");
    if (__DEV__) {
        if (modeArg) {
            if (modeArg.value.kind === Kind.VARIABLE) {
                __DEV__ && invariant.warn(6);
            }
            else if (modeArg.value.kind !== Kind.STRING) {
                __DEV__ && invariant.warn(7);
            }
            else if (modeArg.value.value !== "migrate") {
                __DEV__ && invariant.warn(8, modeArg.value.value);
            }
        }
    }
    if (modeArg &&
        "value" in modeArg.value &&
        modeArg.value.value === "migrate") {
        return "migrate";
    }
    return "unmask";
}
//# sourceMappingURL=directives.js.map
