text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const graphql_1 = require("graphql");
  const graphql_2 = require("./utilities/graphql");
  function printFields(fields) {
      if (!fields)
          return '[]';
      return ('[' +
          fields
              .map(field => `"${field.scope.parentType.name}.${field.fieldDef.name}"`)
              .join(', ') +
          ']');
  }
  exports.printFields = printFields;
  function matchesField(field) {
      return (otherField) => {
          return field.fieldDef.name === otherField.fieldDef.name;
      };
  }
  exports.matchesField = matchesField;
  function groupBy(keyFunction) {
      return (iterable) => {
          const result = new Map();
          for (const element of iterable) {
              const key = keyFunction(element);
              const group = result.get(key);
              if (group) {
                  group.push(element);
              }
              else {
                  result.set(key, [element]);
              }
          }
          return result;
      };
  }
  exports.groupByResponseName = groupBy(field => graphql_2.getResponseName(field.fieldNode));
  exports.groupByParentType = groupBy(field => field.scope.parentType);
  function selectionSetFromFieldSet(fields, parentType) {
      return {
          kind: graphql_1.Kind.SELECTION_SET,
          selections: Array.from(exports.groupByParentType(fields)).flatMap(([typeCondition, fieldsByParentType]) => wrapInInlineFragmentIfNeeded(Array.from(exports.groupByResponseName(fieldsByParentType).values()).map(fieldsByResponseName => {
              return combineFields(fieldsByResponseName)
                  .fieldNode;
          }), typeCondition, parentType)),
      };
  }
  exports.selectionSetFromFieldSet = selectionSetFromFieldSet;
  function wrapInInlineFragmentIfNeeded(selections, typeCondition, parentType) {
      return typeCondition === parentType
          ? selections
          : [
              {
                  kind: graphql_1.Kind.INLINE_FRAGMENT,
                  typeCondition: {
                      kind: graphql_1.Kind.NAMED_TYPE,
                      name: {
                          kind: graphql_1.Kind.NAME,
                          value: typeCondition.name,
                      },
                  },
                  selectionSet: { kind: graphql_1.Kind.SELECTION_SET, selections },
              },
          ];
  }
  function combineFields(fields) {
      const { scope, fieldNode, fieldDef } = fields[0];
      const returnType = graphql_1.getNamedType(fieldDef.type);
      if (graphql_1.isCompositeType(returnType)) {
          return {
              scope,
              fieldNode: {
                  ...fieldNode,
                  selectionSet: mergeSelectionSets(fields.map(field => field.fieldNode)),
              },
              fieldDef,
          };
      }
      else {
          return { scope, fieldNode, fieldDef };
      }
  }
  function mergeSelectionSets(fieldNodes) {
      const selections = [];
      for (const fieldNode of fieldNodes) {
          if (!fieldNode.selectionSet)
              continue;
          selections.push(...fieldNode.selectionSet.selections);
      }
      return {
          kind: 'SelectionSet',
          selections,
      };
  }
  //# sourceMappingURL=FieldSet.js.map