import { d as defineEventHandler, u as useKubbAgentContext, s as setHeaders } from '../../nitro/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:perf_hooks';
import 'fs-extra';
import 'js-runtime';
import 'node:module';
import '@kubb/react-fabric';
import '@kubb/react-fabric/parsers';
import '@kubb/react-fabric/plugins';
import 'node:process';
import 'node:os';
import 'node:url';
import 'semver';
import 'natural-orderby';
import 'execa';
import 'string-argv';
import 'jiti';
import 'node:async_hooks';

const generate_post = defineEventHandler(async (event) => {
  const { events, onGenerate } = useKubbAgentContext();
  setHeaders(event, {
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache",
    Connection: "keep-alive"
  });
  const stream = new ReadableStream({
    start(controller) {
      const queue = [];
      let draining = false;
      function send2(type, ...data) {
        const sseEvent = { type, data, timestamp: Date.now() };
        const message = `data: ${JSON.stringify(sseEvent)}

`;
        queue.push(message);
        drain();
      }
      async function drain() {
        if (draining) return;
        draining = true;
        while (queue.length > 0) {
          const message = queue.shift();
          if (message) {
            controller.enqueue(message);
          }
        }
        draining = false;
      }
      events.on("plugin:start", (plugin) => send2("plugin:start", plugin));
      events.on("plugin:end", (plugin, meta) => send2("plugin:end", plugin, meta));
      events.on("files:processing:start", (files) => send2("files:processing:start", { total: files.length }));
      events.on("file:processing:update", (meta) => {
        send2("file:processing:update", {
          file: meta.file.path,
          processed: meta.processed,
          total: meta.total,
          percentage: meta.percentage
        });
      });
      events.on("files:processing:end", (files) => send2("files:processing:end", { total: files.length }));
      events.on("info", (message, info) => send2("info", message, info));
      events.on("success", (message, info) => send2("success", message, info));
      events.on("warn", (message, info) => send2("warn", message, info));
      events.on("generation:start", (config) => {
        var _a;
        send2("generation:start", {
          name: config.name,
          plugins: ((_a = config.plugins) == null ? void 0 : _a.length) || 0
        });
      });
      events.on("generation:end", (config, files, sources) => {
        const sourcesRecord = {};
        sources.forEach((value, key) => {
          sourcesRecord[key] = value;
        });
        send2("generation:end", config, files, sourcesRecord);
      });
      events.on("error", (error) => {
        send2("error", {
          message: error.message,
          stack: error.stack
        });
      });
      onGenerate().catch((error) => {
        const sseEvent = {
          type: "error",
          data: [
            {
              message: error instanceof Error ? error.message : "Unknown error",
              stack: error instanceof Error ? error.stack : void 0
            }
          ],
          timestamp: Date.now()
        };
        queue.push(`data: ${JSON.stringify(sseEvent)}

`);
      }).finally(() => {
        const endEvent = { type: "lifecycle:end", data: [], timestamp: Date.now() };
        queue.push(`data: ${JSON.stringify(endEvent)}

`);
        drain().then(() => {
          controller.close();
        });
      });
    }
  });
  return stream;
});

export { generate_post as default };
//# sourceMappingURL=generate.post.mjs.map
