import { d as defineEventHandler, r as readBody, c as createError } from '../../../nitro/nitro.mjs';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { spawnSync } from 'child_process';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'stream';
import 'events';
import 'http';
import 'crypto';
import 'buffer';
import 'zlib';
import 'https';
import 'net';
import 'tls';
import 'url';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'vue-router';
import '@prisma/client';
import '@prisma/adapter-better-sqlite3';
import 'fs';
import 'node:child_process';
import 'node:url';

function parseChangelog(content) {
  const entries = [];
  const versionBlocks = content.split(/(?=^## \[)/m).filter((b) => b.trim());
  for (const block of versionBlocks) {
    const versionMatch = block.match(/^## \[([^\]]+)\] - (\d{4}-\d{2}-\d{2})/);
    if (!versionMatch) continue;
    const rawTag = versionMatch[1];
    const date = versionMatch[2];
    if ((rawTag == null ? void 0 : rawTag.toLowerCase()) === "unreleased") continue;
    const tag = (rawTag == null ? void 0 : rawTag.startsWith("v")) ? rawTag : `v${rawTag}`;
    const sections = [];
    const sectionBlocks = block.split(/(?=^### )/m);
    for (const sectionBlock of sectionBlocks) {
      const sectionMatch = sectionBlock.match(/^### (.+)/);
      if (!sectionMatch) continue;
      const items = [];
      const itemMatches = sectionBlock.matchAll(/^- (.+)$/gm);
      for (const m of itemMatches) {
        if (m[1]) items.push(m[1]);
      }
      if (items.length > 0 && sectionMatch[1]) {
        sections.push({ title: sectionMatch[1].trim(), items });
      }
    }
    if (tag && date) {
      entries.push({ tag, date, sections });
    }
  }
  return entries;
}
const summary_post = defineEventHandler(async (event) => {
  var _a;
  const body = await readBody(event);
  const lang = (body == null ? void 0 : body.lang) === "fr" ? "fr" : "en";
  const lastSeenTag = (body == null ? void 0 : body.lastSeenTag) || null;
  const filename = lang === "fr" ? "CHANGELOG.fr.md" : "CHANGELOG.md";
  const changelogPath = join(process.cwd(), filename);
  let content;
  try {
    content = await readFile(changelogPath, "utf-8");
  } catch {
    throw createError({
      statusCode: 404,
      message: "Changelog not found"
    });
  }
  const allEntries = parseChangelog(content);
  if (!allEntries.length) {
    throw createError({
      statusCode: 404,
      message: "No changelog entries"
    });
  }
  const topTag = ((_a = allEntries[0]) == null ? void 0 : _a.tag) || "";
  let diffEntries;
  let isFirstVisit = false;
  if (!lastSeenTag) {
    diffEntries = allEntries;
    isFirstVisit = true;
  } else {
    const lastSeenIndex = allEntries.findIndex((e) => e.tag === lastSeenTag);
    if (lastSeenIndex === -1) {
      diffEntries = allEntries[0] ? [allEntries[0]] : [];
    } else if (lastSeenIndex === 0) {
      return {
        success: true,
        data: { topTag, entries: [], summary: null, isFirstVisit: false }
      };
    } else {
      diffEntries = allEntries.slice(0, lastSeenIndex);
    }
  }
  const summaryEntries = diffEntries.slice(0, 5);
  let changelogText = "";
  for (const entry of summaryEntries) {
    changelogText += `## ${entry.tag} (${entry.date})
`;
    for (const section of entry.sections) {
      changelogText += `### ${section.title}
`;
      for (const item of section.items) {
        changelogText += `- ${item}
`;
      }
    }
    changelogText += "\n";
  }
  let summary = null;
  try {
    const promptLang = lang === "fr" ? `R\xE9sume en 1-2 phrases les nouveaut\xE9s principales de ce changelog. Sois concis et percutant. R\xE9ponds uniquement avec le r\xE9sum\xE9, sans pr\xE9fixe.

${changelogText}` : `Summarize in 1-2 sentences the main updates from this changelog. Be concise and impactful. Reply only with the summary, no prefix.

${changelogText}`;
    const proc = spawnSync("claude", ["-p", promptLang, "--model", "haiku"], {
      encoding: "utf-8",
      timeout: 3e4,
      maxBuffer: 1024 * 1024
    });
    if (proc.status === 0 && proc.stdout) {
      summary = proc.stdout.trim();
    }
  } catch {
  }
  return {
    success: true,
    data: { topTag, entries: diffEntries, summary, isFirstVisit }
  };
});

export { summary_post as default };
//# sourceMappingURL=summary.post.mjs.map
