"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/eventcatalog-config-file-utils.js
var eventcatalog_config_file_utils_exports = {};
__export(eventcatalog_config_file_utils_exports, {
  addPropertyToFrontMatter: () => addPropertyToFrontMatter,
  cleanup: () => cleanup,
  getEventCatalogConfigFile: () => getEventCatalogConfigFile,
  verifyRequiredFieldsAreInCatalogConfigFile: () => verifyRequiredFieldsAreInCatalogConfigFile,
  writeEventCatalogConfigFile: () => writeEventCatalogConfigFile
});
module.exports = __toCommonJS(eventcatalog_config_file_utils_exports);
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 });
    }
  }
};
var writeEventCatalogConfigFile = async (projectDirectory, newConfig) => {
  try {
    const configFilePath = import_node_path.default.join(projectDirectory, "eventcatalog.config.js");
    let content = await (0, import_promises.readFile)(configFilePath, "utf8");
    const startIndex = content.indexOf("export default {");
    if (startIndex === -1) {
      return;
    }
    Object.entries(newConfig).forEach(([key, value]) => {
      const valueString = JSON.stringify(value, null, 2).replace(/"/g, "'").replace(/\n/g, "\n  ");
      const keyRegex = new RegExp(`(${key}\\s*:)([^,}]+)`, "g");
      if (content.match(keyRegex)) {
        content = content.replace(keyRegex, `$1 ${valueString}`);
      } else {
        const insertPosition = content.indexOf("{", startIndex) + 1;
        content = content.slice(0, insertPosition) + `
  ${key}: ${valueString},` + content.slice(insertPosition);
      }
    });
    await (0, import_promises.writeFile)(configFilePath, content);
  } finally {
    await cleanup(projectDirectory);
  }
};
var verifyRequiredFieldsAreInCatalogConfigFile = async (projectDirectory) => {
  try {
    const config = await getEventCatalogConfigFile(projectDirectory);
    if (!config.cId) {
      await writeEventCatalogConfigFile(projectDirectory, { cId: (0, import_uuid.v4)() });
    }
  } catch (error) {
  }
};
function addPropertyToFrontMatter(input, newProperty, newValue) {
  const file = (0, import_gray_matter.default)(input);
  return import_gray_matter.default.stringify(file.content, { ...file.data, [newProperty]: newValue });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addPropertyToFrontMatter,
  cleanup,
  getEventCatalogConfigFile,
  verifyRequiredFieldsAreInCatalogConfigFile,
  writeEventCatalogConfigFile
});
