import { d as defineEventHandler, r as readBody, c as createError, G as executeClaudePrompt, H as parseClaudeJson } from '../../../nitro/nitro.mjs';
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 'path';
import 'fs/promises';
import 'child_process';
import 'node:child_process';
import 'node:url';

const autocomplete_post = defineEventHandler(async (event) => {
  var _a, _b, _c, _d, _e, _f;
  const body = await readBody(event);
  if (!((_a = body == null ? void 0 : body.title) == null ? void 0 : _a.trim())) {
    throw createError({
      statusCode: 400,
      message: "Title is required"
    });
  }
  const lang = body.lang || "fr";
  const title = body.title.trim();
  const existingFields = [];
  if ((_b = body.description) == null ? void 0 : _b.trim()) {
    existingFields.push(`Existing description: "${body.description.trim()}"`);
  }
  if (body.priority) {
    existingFields.push(`Existing priority: ${body.priority}`);
  }
  if ((_c = body.labels) == null ? void 0 : _c.length) {
    existingFields.push(`Existing labels: ${body.labels.join(", ")}`);
  }
  if ((_d = body.acceptance_criteria) == null ? void 0 : _d.length) {
    existingFields.push(`Existing criteria: ${body.acceptance_criteria.join("; ")}`);
  }
  if (body.parent_key) {
    existingFields.push(`Parent issue: ${body.parent_key}`);
  }
  const outputLang = lang === "fr" ? "French" : "English";
  const contextSection = existingFields.length > 0 ? `
Existing context (use this information to improve and complete):
${existingFields.join("\n")}
` : "";
  const prompt = `OUTPUT LANGUAGE: ${outputLang} (MANDATORY)

You are an assistant that helps create development tickets.
You MUST write ALL text content in ${outputLang}. No exceptions.

Title: "${title}"
${contextSection}
Generate a JSON with these fields:
- description: ${((_e = body.description) == null ? void 0 : _e.trim()) ? "Improve the existing description" : "A detailed description (2-4 sentences)"} [WRITE IN ${outputLang.toUpperCase()}]
- priority: P0, P1, P2, or P3 ${body.priority ? "(adjust if relevant)" : "(P2 by default)"}
- labels: Array from [bug, feature, enhancement, documentation, urgent, refactoring, security, performance, ui, api]
- acceptance_criteria: JSON array of strings (2-5 items), e.g. ["criterion 1", "criterion 2"]. ${((_f = body.acceptance_criteria) == null ? void 0 : _f.length) ? "Improve and complete existing criteria." : ""} [WRITE IN ${outputLang.toUpperCase()}]
- semver: "patch", "minor", or "major"

REMINDER: Output MUST be in ${outputLang}. Translate any French content to ${outputLang}.

JSON only, no markdown.`;
  const result = executeClaudePrompt(prompt, 6e4);
  if (!result.success) {
    throw createError({
      statusCode: 500,
      message: result.error || "Claude failed"
    });
  }
  try {
    const data = parseClaudeJson(result.output || "");
    if (data.acceptance_criteria) {
      if (typeof data.acceptance_criteria === "string") {
        const str = data.acceptance_criteria;
        const separator = str.includes("\n") ? "\n" : ";";
        data.acceptance_criteria = str.split(separator).map((line) => line.replace(/^[-•*]\s*/, "").trim()).filter((line) => line.length > 0);
      }
    }
    return data;
  } catch {
    throw createError({
      statusCode: 500,
      message: "Failed to parse Claude response"
    });
  }
});

export { autocomplete_post as default };
//# sourceMappingURL=autocomplete.post.mjs.map
