import { d as defineEventHandler, r as readBody, c as createError } from '../../nitro/nitro.mjs';
import { readFile, writeFile } from 'fs/promises';
import { join } from 'path';
import { parse, stringify } from 'yaml';
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 'child_process';
import 'node:child_process';
import 'node:url';

const index_post = defineEventHandler(async (event) => {
  var _a;
  const body = await readBody(event);
  if (!body.slug || !body.name || !body.segment) {
    throw createError({
      statusCode: 400,
      message: "slug, name, and segment are required"
    });
  }
  const validSegments = ["definition", "action", "finish"];
  if (!validSegments.includes(body.segment)) {
    throw createError({
      statusCode: 400,
      message: "segment must be: definition, action, or finish"
    });
  }
  if (!/^[a-z][a-z0-9-]*$/.test(body.slug)) {
    throw createError({
      statusCode: 400,
      message: "slug must be kebab-case (lowercase letters, numbers, hyphens)"
    });
  }
  const catalogPath = join(process.cwd(), "templates", "catalog.yaml");
  try {
    const content = await readFile(catalogPath, "utf-8");
    const catalog = parse(content);
    if (!catalog.segments) {
      catalog.segments = {
        definition: { description: "Preparation and qualification phases", columns: [] },
        action: { description: "Implementation and validation phases", columns: [] },
        finish: { description: "Finalization and deployment phases", columns: [] }
      };
    }
    for (const segmentName of validSegments) {
      const segment = catalog.segments[segmentName];
      if ((_a = segment == null ? void 0 : segment.columns) == null ? void 0 : _a.some((c) => c.slug === body.slug)) {
        throw createError({
          statusCode: 409,
          message: `Column '${body.slug}' already exists in ${segmentName} segment`
        });
      }
    }
    if (!catalog.segments[body.segment]) {
      catalog.segments[body.segment] = { description: "", columns: [] };
    }
    if (!catalog.segments[body.segment].columns) {
      catalog.segments[body.segment].columns = [];
    }
    const newColumn = {
      slug: body.slug,
      name: body.name
    };
    if (body.description) {
      newColumn.description = body.description;
    }
    catalog.segments[body.segment].columns.push(newColumn);
    await writeFile(catalogPath, stringify(catalog), "utf-8");
    return {
      success: true,
      column: newColumn,
      segment: body.segment
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    throw createError({
      statusCode: 500,
      message: `Failed to update catalog: ${error instanceof Error ? error.message : "Unknown error"}`
    });
  }
});

export { index_post as default };
//# sourceMappingURL=index.post.mjs.map
