var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var graphqlSchema_exports = {};
__export(graphqlSchema_exports, {
  getGraphQLSchemaInfo: () => getGraphQLSchemaInfo,
  getSchema: () => getSchema
});
module.exports = __toCommonJS(graphqlSchema_exports);
var import_keys = __toESM(require("@babel/runtime-corejs3/core-js/object/keys"));
var import_stringify = __toESM(require("@babel/runtime-corejs3/core-js/json/stringify"));
var import_map = __toESM(require("@babel/runtime-corejs3/core-js/instance/map"));
var import_filter = __toESM(require("@babel/runtime-corejs3/core-js/instance/filter"));
var import_for_each = __toESM(require("@babel/runtime-corejs3/core-js/instance/for-each"));
var import_includes = __toESM(require("@babel/runtime-corejs3/core-js/instance/includes"));
var import_node_crypto = __toESM(require("node:crypto"));
var import_plugin_helpers = require("@graphql-codegen/plugin-helpers");
var import_code_file_loader = require("@graphql-tools/code-file-loader");
var import_load = require("@graphql-tools/load");
var import_graphql = require("graphql");
var import_graphql_server = require("@cedarjs/graphql-server");
var import_project = require("./project");
const getSchema = async () => {
  const userProjectPaths = (0, import_project.getUserProjectPaths)();
  const schemaPointerMap = {
    [(0, import_graphql.print)(import_graphql_server.rootSchema.schema)]: {},
    "graphql/**/*.sdl.{js,ts}": {},
    "directives/**/*.{js,ts}": {},
    "subscriptions/**/*.{js,ts}": {}
  };
  const loadSchemaConfig = {
    assumeValidSDL: true,
    sort: true,
    convertExtensions: true,
    includeSources: true,
    cwd: userProjectPaths.api.src,
    schema: (0, import_keys.default)(schemaPointerMap),
    generates: {
      [userProjectPaths.generated.schema]: {
        plugins: ["schema-ast"]
      }
    },
    silent: false,
    errorsOnly: false,
    pluginContext: {},
    loaders: [new import_code_file_loader.CodeFileLoader()]
  };
  return (0, import_load.loadSchema)(schemaPointerMap, loadSchemaConfig);
};
const getGraphQLSchemaInfo = async () => {
  const schema = await getSchema();
  const documentNode = (0, import_plugin_helpers.getCachedDocumentNodeFromSchema)(schema);
  const ast = (0, import_stringify.default)(documentNode.definitions);
  const id = import_node_crypto.default.createHash("md5").update(ast).digest("hex");
  const definitions = [];
  (0, import_graphql.visit)(documentNode, {
    DirectiveDefinition: (node) => {
      definitions.push(node);
    },
    ScalarTypeDefinition: (node) => {
      definitions.push(node);
    },
    ObjectTypeDefinition: (node) => {
      definitions.push(node);
    },
    InputObjectTypeDefinition: (node) => {
      definitions.push(node);
    },
    OperationDefinition: (node) => {
      definitions.push(node);
    }
  });
  const relationships = getGraphQLRelationships(definitions);
  return {
    id,
    ast,
    definitions,
    relationships
  };
};
const findNameAndValue = (field) => {
  if (field.name?.value) {
    return field.name.value;
  }
  if (field.type) {
    return findNameAndValue(field.type);
  }
  return void 0;
};
const getGraphQLRelationships = (definitions) => {
  var _context;
  const relationships = [];
  const objectTypeNames = (0, import_map.default)(_context = (0, import_filter.default)(definitions).call(definitions, (definition) => {
    return definition.kind === "ObjectTypeDefinition";
  })).call(_context, (definition) => {
    return definition.name.value;
  });
  (0, import_for_each.default)(definitions).call(definitions, (definition) => {
    if (definition.kind === "ObjectTypeDefinition" || definition.kind === "FieldDefinition") {
      const typeName = definition.name?.value;
      if (definition.fields) {
        var _context2;
        (0, import_for_each.default)(_context2 = definition.fields).call(_context2, (field) => {
          const relatedFieldTypeName = findNameAndValue(field.type);
          if (relatedFieldTypeName && (0, import_includes.default)(objectTypeNames).call(objectTypeNames, relatedFieldTypeName)) {
            relationships.push({
              source: typeName,
              target: relatedFieldTypeName,
              label: field.name.value
            });
          }
        });
      }
    }
  });
  return relationships;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getGraphQLSchemaInfo,
  getSchema
});
//# sourceMappingURL=graphqlSchema.js.map
