import { i as defineEventHandler, e as createError, O as resolveLogPath, u as getQuery } from '../../nitro/nitro.mjs';
import * as fs from 'node:fs';
import 'node:path';
import 'node:module';
import 'node:os';
import 'dotenv';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:crypto';
import 'node:async_hooks';
import 'node:url';
import 'node:net';
import '@iconify/utils';
import 'consola';

const DEFAULT_LIMIT = 1e3;
const MAX_LIMIT = 5e4;
const logs_get = defineEventHandler((event) => {
  var _a;
  const cwd = process.env.AIDEV_CWD;
  if (!cwd) {
    throw createError({ statusCode: 500, statusMessage: "AIDEV_CWD not set" });
  }
  const file = resolveLogPath(cwd);
  const query = getQuery(event);
  const rawLimit = Number.parseInt(String((_a = query.limit) != null ? _a : ""), 10);
  const limit = Number.isFinite(rawLimit) && rawLimit > 0 ? Math.min(rawLimit, MAX_LIMIT) : DEFAULT_LIMIT;
  const q = typeof query.q === "string" ? query.q : "";
  const needle = q.toLowerCase();
  if (!fs.existsSync(file)) {
    return {
      path: file,
      exists: false,
      total: 0,
      shown: 0,
      truncated: false,
      limit,
      query: q,
      lines: []
    };
  }
  const raw = fs.readFileSync(file, "utf8");
  const all = raw.split(/\r?\n/);
  if (all.length > 0 && all[all.length - 1] === "") all.pop();
  const total = all.length;
  const tail = total > limit ? all.slice(total - limit) : all;
  const lines = needle ? tail.filter((l) => l.toLowerCase().includes(needle)) : tail;
  return {
    path: file,
    exists: true,
    total,
    shown: lines.length,
    truncated: total > tail.length,
    limit,
    query: q,
    lines
  };
});

export { logs_get as default };
//# sourceMappingURL=logs.get.mjs.map
