import { t as trpc } from './trpc-client-Bucti7HD.js';
import { diff } from 'deep-object-diff';
import '@trpc/client';

const GET = async ({ params }) => {
  const { agent } = params;
  const [{ rows }, config, results] = await Promise.all([
    trpc.tests.list.query({ agent }),
    trpc.tests.config.query({ agent }),
    trpc.tests.latestResult.query({ agent })
  ]);
  return new Response(JSON.stringify({ rows, config, results }), {
    headers: { "content-type": "application/json" }
  });
};
const POST = async ({ params, request }) => {
  const { agent } = params;
  const seed = await request.json();
  const mergeFlag = Boolean(seed.mergeContexts);
  if (mergeFlag) {
    try {
      const suite = await trpc.tests.suite.query({ agent });
      const base = suite?.meta?.defaultContext || {};
      const provided = typeof seed.context === "string" ? JSON.parse(seed.context || "{}") : seed.context || {};
      const patch = diff(base, provided);
      seed.context = JSON.stringify(patch ?? {});
    } catch {
      const provided = typeof seed.context === "string" ? seed.context : JSON.stringify(seed.context || {});
      seed.context = provided;
    }
  } else {
    if (typeof seed.context !== "string") {
      seed.context = JSON.stringify(seed.context || {});
    }
  }
  const row = await trpc.tests.addTest.mutate({ agent, seed });
  return new Response(JSON.stringify(row), { headers: { "content-type": "application/json" } });
};

export { GET, POST };
//# sourceMappingURL=_server.ts-BmTnkE0T.js.map
