import { i as defineEventHandler, e as createError, K as readBody, Z as writeEnvFile, L as readEnvFile } from '../../nitro/nitro.mjs';
import 'node:path';
import 'node:module';
import 'node:fs';
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 KEY_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
const config_put = defineEventHandler(async (event) => {
  const cwd = process.env.AIDEV_CWD;
  if (!cwd) {
    throw createError({ statusCode: 500, statusMessage: "AIDEV_CWD not set" });
  }
  const body = await readBody(event);
  if (!body || typeof body.values !== "object" || body.values === null) {
    throw createError({
      statusCode: 400,
      statusMessage: "Body must be { values: Record<string, string> }"
    });
  }
  const kv = {};
  for (const [k, v] of Object.entries(body.values)) {
    if (!KEY_PATTERN.test(k)) {
      throw createError({ statusCode: 400, statusMessage: `Invalid key: ${k}` });
    }
    if (v == null) continue;
    kv[k] = String(v);
  }
  writeEnvFile(cwd, kv);
  return readEnvFile(cwd);
});

export { config_put as default };
//# sourceMappingURL=config.put.mjs.map
