// src/output.ts
import fs from "fs-extra";
import path from "path";
import chalk from "chalk";
async function writeOutput(outPath, techs, config, format = "json", assetsOutPath) {
  const outDirectory = path.dirname(outPath);
  await fs.ensureDir(outDirectory);
  let availableLogos = /* @__PURE__ */ new Set();
  if (assetsOutPath) {
    availableLogos = await copyAssets(techs, assetsOutPath);
  }
  if (format === "json") {
    await fs.writeJSON(
      outPath,
      {
        generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
        tech: techs
      },
      { spaces: 2 }
    );
    console.log(chalk.blue(`\u2192 Saved ${techs.length} tech entries to ${outPath}`));
  } else if (format === "markdown") {
    const relativeAssetsPath = assetsOutPath ? path.relative(outDirectory, assetsOutPath).replace(/\\/g, "/") : void 0;
    const mdContent = generateMarkdown(techs, relativeAssetsPath, availableLogos);
    const mdPath = outPath.endsWith(".json") ? outPath.replace(/\.json$/, ".md") : outPath;
    await fs.writeFile(mdPath, mdContent);
    console.log(chalk.blue(`\u2192 Saved markdown to ${mdPath}`));
  }
}
async function copyAssets(techs, dest) {
  const srcDir = path.resolve(__dirname, "../assets/logos");
  const copied = /* @__PURE__ */ new Set();
  let count = 0;
  for (const t of techs) {
    if (!t.logo) continue;
    const srcFile = path.join(srcDir, t.logo);
    const destFile = path.join(dest, t.logo);
    if (await fs.pathExists(srcFile)) {
      await fs.ensureDir(path.dirname(destFile));
      await fs.copy(srcFile, destFile);
      copied.add(t.logo);
      count++;
    } else {
    }
  }
  if (count > 0) {
    console.log(chalk.blue(`\u2192 Copied ${count} logos to ${dest}`));
  }
  return copied;
}
function generateMarkdown(techs, assetsPath, availableLogos) {
  const grouped = {};
  for (const t of techs) {
    const type = t.type || "misc";
    if (!grouped[type]) grouped[type] = [];
    grouped[type].push(t);
  }
  let md = "# Tech Stack\n\n";
  const order = [
    "language",
    "frontend",
    "backend",
    "framework",
    "library",
    "database",
    "orm",
    "auth",
    "api",
    "cloud",
    "hosting",
    "ci",
    "devops",
    "container",
    "testing",
    "build",
    "tooling",
    "misc"
  ];
  const sortedKeys = Object.keys(grouped).sort((a, b) => {
    const idxA = order.indexOf(a);
    const idxB = order.indexOf(b);
    if (idxA === -1 && idxB === -1) return a.localeCompare(b);
    if (idxA === -1) return 1;
    if (idxB === -1) return -1;
    return idxA - idxB;
  });
  for (const type of sortedKeys) {
    const title = type.charAt(0).toUpperCase() + type.slice(1);
    md += `## ${title}

`;
    for (const t of grouped[type]) {
      if (assetsPath && t.logo && availableLogos?.has(t.logo)) {
        const logoUrl = `${assetsPath}/${t.logo}`;
        md += `- <img src="${logoUrl}" alt="${t.name}" width="24" height="24" /> **${t.name}**
`;
      } else {
        md += `- **${t.name}**
`;
      }
    }
    md += "\n";
  }
  return md;
}

export {
  writeOutput
};
