import { d as defineEventHandler, b as getRouterParam, c as createError, A as findByIssue } 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 fullTerminal_get = defineEventHandler(async (event) => {
  const id = getRouterParam(event, "id");
  if (!id) {
    throw createError({
      statusCode: 400,
      message: "Issue ID required"
    });
  }
  const executionLogs = await findByIssue(id);
  const logFiles = executionLogs.filter((log) => log.terminalLog).map((log) => {
    var _a, _b;
    return {
      column: (_b = (_a = log.column) == null ? void 0 : _a.slug) != null ? _b : "",
      sessionId: log.sessionId,
      filename: "terminal.log",
      logs: parseLogContent(log.terminalLog || "")
    };
  });
  logFiles.sort((a, b) => {
    const colCompare = a.column.localeCompare(b.column);
    if (colCompare !== 0) return colCompare;
    return a.sessionId.localeCompare(b.sessionId);
  });
  return {
    issueKey: id,
    logFiles
  };
});
function parseLogContent(content) {
  var _a, _b;
  const lines = content.split("\n").filter(Boolean);
  const logs = [];
  for (const line of lines) {
    const match = line.match(/^\[(.+?)\]\s*\[(\w+)\]\s*(.+)$/);
    if (match) {
      const timestamp = match[1];
      const levelStr = (_b = (_a = match[2]) == null ? void 0 : _a.toLowerCase()) != null ? _b : "info";
      const message = match[3];
      if (timestamp && message) {
        logs.push({
          timestamp,
          level: levelStr,
          message
        });
      }
    } else {
      logs.push({
        timestamp: "",
        level: "info",
        message: line
      });
    }
  }
  return logs;
}

export { fullTerminal_get as default };
//# sourceMappingURL=full-terminal.get.mjs.map
