// src/map-catalog-to-astro.js
import path from "path";
var COLLECTION_KEYS = [
  "agents",
  "events",
  "commands",
  "services",
  "domains",
  "flows",
  "pages",
  "changelogs",
  "queries",
  "channels",
  "ubiquitousLanguages",
  "dependencies"
];
function mapCatalogToAstro({ filePath, astroDir, projectDir }) {
  const relativeFilePath = removeBasePath(filePath, projectDir);
  if (isLikeC4Source(relativeFilePath)) {
    return [];
  }
  if (!isCatalogRelated(relativeFilePath)) {
    return [];
  }
  const baseTargetPaths = getBaseTargetPaths(relativeFilePath);
  const relativeTargetPath = getRelativeTargetPath(relativeFilePath);
  return baseTargetPaths.map((base) => path.join(astroDir, base, relativeTargetPath));
}
function removeBasePath(fullPath, basePath) {
  const relativePath = path.relative(basePath, fullPath);
  return relativePath.startsWith("..") ? fullPath : relativePath;
}
function isCollectionKey(key) {
  return COLLECTION_KEYS.includes(key);
}
function isLikeC4Source(filePath) {
  return filePath.endsWith(".c4") || filePath.endsWith(".likec4");
}
function isCatalogRelated(filePath) {
  const filePathArr = filePath.split(path.sep).filter(Boolean);
  if ([
    "eventcatalog.config.js",
    // config file at root
    "eventcatalog.styles.css",
    // custom styles file at root
    "components",
    // custom components
    "snippets",
    // custom snippets
    "public",
    // public assets
    ".env",
    // env file
    ...COLLECTION_KEYS
  ].includes(filePathArr[0])) {
    return true;
  }
  return false;
}
function getBaseTargetPaths(filePath) {
  const filePathArr = filePath.split(path.sep).filter(Boolean);
  if (isCollectionKey(filePathArr[0])) {
    const hasExtension = (str) => /\.[a-zA-Z0-9]{2,}$/.test(str);
    if (hasExtension(filePath)) {
      return [path.join("public", "generated")];
    }
    return [path.join("public", "generated")];
  }
  if (filePathArr[0] == "components") {
    return [path.join("src", "custom-defined-components")];
  }
  if (filePathArr[0] == "snippets") {
    return [path.join("src")];
  }
  if (filePathArr[0] == "public") {
    return [path.join("public")];
  }
  return [path.join("/")];
}
function getRelativeTargetPath(filePath) {
  const filePathArr = filePath.split(path.sep).filter(Boolean);
  if (filePathArr[0] == "public" || filePathArr[0] == "components") {
    filePathArr.shift();
  }
  if (filePathArr[0] == "snippets") {
    return path.join("snippets", ...filePathArr.slice(1));
  }
  const relativePath = [];
  for (let i = filePathArr.length - 1; i >= 0; i--) {
    relativePath.unshift(filePathArr[i]);
    if (isCollectionKey(filePathArr[i]) && filePathArr[i] != "snippets") break;
  }
  return path.join(...relativePath);
}

export {
  mapCatalogToAstro
};
