"use strict";;
const {
    __DEV__
} = require("@apollo/client/utilities/environment");

Object.defineProperty(exports, "__esModule", { value: true });
exports.hasAllDirectives = exports.hasAnyDirectives = void 0;
exports.shouldInclude = shouldInclude;
exports.getDirectiveNames = getDirectiveNames;
exports.hasDirectives = hasDirectives;
exports.hasClientExports = hasClientExports;
exports.getInclusionDirectives = getInclusionDirectives;
exports.getFragmentMaskMode = getFragmentMaskMode;
const graphql_1 = require("graphql");
const environment_1 = require("@apollo/client/utilities/environment");
const invariant_1 = require("@apollo/client/utilities/invariant");
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];
            (0, invariant_1.invariant)(evaledValue !== void 0, 2, directive.name.value);
        }
        else {
            evaledValue = ifArgument.value.value;
        }
        return directive.name.value === "skip" ? !evaledValue : evaledValue;
    });
}
function getDirectiveNames(root) {
    const names = [];
    (0, graphql_1.visit)(root, {
        Directive(node) {
            names.push(node.name.value);
        },
    });
    return names;
}
const hasAnyDirectives = (names, root) => hasDirectives(names, root, false);
exports.hasAnyDirectives = hasAnyDirectives;
const hasAllDirectives = (names, root) => hasDirectives(names, root, true);
exports.hasAllDirectives = hasAllDirectives;
function hasDirectives(names, root, all) {
    const nameSet = new Set(names);
    const uniqueCount = nameSet.size;
    (0, graphql_1.visit)(root, {
        Directive(node) {
            if (nameSet.delete(node.name.value) && (!all || !nameSet.size)) {
                return graphql_1.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;
}
function hasClientExports(document) {
    return document && hasDirectives(["client", "export"], document, true);
}
function isInclusionDirective({ name: { value } }) {
    return value === "skip" || value === "include";
}
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;
            (0, invariant_1.invariant)(directiveArguments && directiveArguments.length === 1, 3, directiveName);
            const ifArgument = directiveArguments[0];
            (0, invariant_1.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
            (0, invariant_1.invariant)(ifValue &&
                (ifValue.kind === "Variable" || ifValue.kind === "BooleanValue"), 5, directiveName);
            result.push({ directive, ifArgument });
        });
    }
    return result;
}
/** @internal */
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 (environment_1.__DEV__) {
        if (modeArg) {
            if (modeArg.value.kind === graphql_1.Kind.VARIABLE) {
                __DEV__ && invariant_1.invariant.warn(6);
            }
            else if (modeArg.value.kind !== graphql_1.Kind.STRING) {
                __DEV__ && invariant_1.invariant.warn(7);
            }
            else if (modeArg.value.value !== "migrate") {
                __DEV__ && invariant_1.invariant.warn(8, modeArg.value.value);
            }
        }
    }
    if (modeArg &&
        "value" in modeArg.value &&
        modeArg.value.value === "migrate") {
        return "migrate";
    }
    return "unmask";
}
//# sourceMappingURL=directives.cjs.map
