"use strict";
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);

// src/resolve-catalog-dependencies.js
var resolve_catalog_dependencies_exports = {};
__export(resolve_catalog_dependencies_exports, {
  default: () => resolve_catalog_dependencies_default
});
module.exports = __toCommonJS(resolve_catalog_dependencies_exports);

// src/eventcatalog-config-file-utils.js
var import_promises = require("fs/promises");
var import_node_fs = require("fs");
var import_node_path = __toESM(require("path"), 1);
var import_uuid = require("uuid");
var import_url = require("url");
var import_node_os = require("os");
var import_gray_matter = __toESM(require("gray-matter"), 1);
async function cleanup(projectDirectory) {
  const filePath = import_node_path.default.join(projectDirectory, "eventcatalog.config.mjs");
  if ((0, import_node_fs.existsSync)(filePath)) {
    await (0, import_promises.rm)(filePath);
  }
}
var getEventCatalogConfigFile = async (projectDirectory) => {
  let tempDir;
  try {
    let configFilePath = import_node_path.default.join(projectDirectory, "eventcatalog.config.js");
    const filePath = import_node_path.default.join(projectDirectory, "package.json");
    const packageJson = JSON.parse(await (0, import_promises.readFile)(filePath, "utf-8"));
    if (packageJson?.type !== "module") {
      tempDir = await (0, import_promises.mkdtemp)(import_node_path.default.join((0, import_node_os.tmpdir)(), "eventcatalog-config-"));
      configFilePath = import_node_path.default.join(tempDir, "eventcatalog.config.mjs");
      await (0, import_promises.copyFile)(import_node_path.default.join(projectDirectory, "eventcatalog.config.js"), configFilePath);
    }
    const configFileURL = `${(0, import_url.pathToFileURL)(configFilePath).href}?t=${Date.now()}`;
    const config = await import(
      /* @vite-ignore */
      configFileURL
    );
    return config.default;
  } finally {
    await cleanup(projectDirectory);
    if (tempDir) {
      await (0, import_promises.rm)(tempDir, { recursive: true, force: true });
    }
  }
};

// src/resolve-catalog-dependencies.js
var import_node_path2 = __toESM(require("path"), 1);
var import_node_fs2 = __toESM(require("fs"), 1);
var import_gray_matter2 = __toESM(require("gray-matter"), 1);
var resolve_catalog_dependencies_default = async (catalogDir, core) => {
  const catalogConfig = await getEventCatalogConfigFile(catalogDir);
  const dependencies = catalogConfig?.dependencies ?? null;
  if (!dependencies) {
    return;
  }
  const dependenciesDir = import_node_path2.default.join(catalogDir, "dependencies");
  if (import_node_fs2.default.existsSync(dependenciesDir)) {
    import_node_fs2.default.rmSync(dependenciesDir, { recursive: true, force: true });
  }
  import_node_fs2.default.mkdirSync(dependenciesDir, { recursive: true });
  const resourceTypes = Object.keys(dependencies);
  for (const resourceType of resourceTypes) {
    for (const dependency of dependencies[resourceType]) {
      const frontmatter = {
        id: dependency.id,
        name: dependency.id,
        version: dependency.version || "1.0.0"
      };
      const markdown = import_gray_matter2.default.stringify(
        {
          content: [
            ":::warning",
            "You are running EventCatalog with dependencies enabled.",
            "This resource is mocked and is a dependency. This means that the resource is managed and owned by another catalog.",
            ":::"
          ].join("\n\n")
        },
        frontmatter
      );
      const resourceFile = import_node_path2.default.join(dependenciesDir, resourceType, dependency.id, `index.md`);
      import_node_fs2.default.mkdirSync(import_node_path2.default.dirname(resourceFile), { recursive: true });
      import_node_fs2.default.writeFileSync(resourceFile, markdown);
    }
  }
  return;
};
