"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeAst = void 0;
var graphql_1 = require("graphql");
function uniqueBy(array, iteratee) {
    var e_1, _a;
    var _b;
    var FilteredMap = new Map();
    var result = [];
    try {
        for (var array_1 = __values(array), array_1_1 = array_1.next(); !array_1_1.done; array_1_1 = array_1.next()) {
            var item = array_1_1.value;
            if (item.kind === 'Field') {
                var uniqueValue = iteratee(item);
                var existing = FilteredMap.get(uniqueValue);
                if ((_b = item.directives) === null || _b === void 0 ? void 0 : _b.length) {
                    var itemClone = __assign({}, item);
                    result.push(itemClone);
                }
                else if ((existing === null || existing === void 0 ? void 0 : existing.selectionSet) && item.selectionSet) {
                    existing.selectionSet.selections = __spreadArray(__spreadArray([], __read(existing.selectionSet.selections), false), __read(item.selectionSet.selections), false);
                }
                else if (!existing) {
                    var itemClone = __assign({}, item);
                    FilteredMap.set(uniqueValue, itemClone);
                    result.push(itemClone);
                }
            }
            else {
                result.push(item);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (array_1_1 && !array_1_1.done && (_a = array_1.return)) _a.call(array_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return result;
}
function inlineRelevantFragmentSpreads(fragmentDefinitions, selections, selectionSetType) {
    var e_2, _a;
    var _b;
    var selectionSetTypeName = selectionSetType
        ? (0, graphql_1.getNamedType)(selectionSetType).name
        : null;
    var outputSelections = [];
    var seenSpreads = [];
    try {
        for (var selections_1 = __values(selections), selections_1_1 = selections_1.next(); !selections_1_1.done; selections_1_1 = selections_1.next()) {
            var selection = selections_1_1.value;
            if (selection.kind === 'FragmentSpread') {
                var fragmentName = selection.name.value;
                if (!selection.directives || selection.directives.length === 0) {
                    if (seenSpreads.includes(fragmentName)) {
                        continue;
                    }
                    else {
                        seenSpreads.push(fragmentName);
                    }
                }
                var fragmentDefinition = fragmentDefinitions[selection.name.value];
                if (fragmentDefinition) {
                    var typeCondition = fragmentDefinition.typeCondition, directives = fragmentDefinition.directives, selectionSet = fragmentDefinition.selectionSet;
                    selection = {
                        kind: graphql_1.Kind.INLINE_FRAGMENT,
                        typeCondition: typeCondition,
                        directives: directives,
                        selectionSet: selectionSet,
                    };
                }
            }
            if (selection.kind === graphql_1.Kind.INLINE_FRAGMENT &&
                (!selection.directives || ((_b = selection.directives) === null || _b === void 0 ? void 0 : _b.length) === 0)) {
                var fragmentTypeName = selection.typeCondition
                    ? selection.typeCondition.name.value
                    : null;
                if (!fragmentTypeName || fragmentTypeName === selectionSetTypeName) {
                    outputSelections.push.apply(outputSelections, __spreadArray([], __read(inlineRelevantFragmentSpreads(fragmentDefinitions, selection.selectionSet.selections, selectionSetType)), false));
                    continue;
                }
            }
            outputSelections.push(selection);
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (selections_1_1 && !selections_1_1.done && (_a = selections_1.return)) _a.call(selections_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return outputSelections;
}
function mergeAst(documentAST, schema) {
    var e_3, _a;
    var typeInfo = schema ? new graphql_1.TypeInfo(schema) : null;
    var fragmentDefinitions = Object.create(null);
    try {
        for (var _b = __values(documentAST.definitions), _c = _b.next(); !_c.done; _c = _b.next()) {
            var definition = _c.value;
            if (definition.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
                fragmentDefinitions[definition.name.value] = definition;
            }
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
        }
        finally { if (e_3) throw e_3.error; }
    }
    var flattenVisitors = {
        SelectionSet: function (node) {
            var selectionSetType = typeInfo ? typeInfo.getParentType() : null;
            var selections = node.selections;
            selections = inlineRelevantFragmentSpreads(fragmentDefinitions, selections, selectionSetType);
            return __assign(__assign({}, node), { selections: selections });
        },
        FragmentDefinition: function () {
            return null;
        },
    };
    var flattenedAST = (0, graphql_1.visit)(documentAST, typeInfo ? (0, graphql_1.visitWithTypeInfo)(typeInfo, flattenVisitors) : flattenVisitors);
    var deduplicateVisitors = {
        SelectionSet: function (node) {
            var selections = node.selections;
            selections = uniqueBy(selections, function (selection) {
                return selection.alias ? selection.alias.value : selection.name.value;
            });
            return __assign(__assign({}, node), { selections: selections });
        },
        FragmentDefinition: function () {
            return null;
        },
    };
    return (0, graphql_1.visit)(flattenedAST, deduplicateVisitors);
}
exports.mergeAst = mergeAst;
//# sourceMappingURL=merge-ast.js.map