#!/usr/bin/env node
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));

// src/server/fb-wizard.ts
var readline = __toESM(require("readline"));
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var FIELD_TYPES = ["text", "email", "number", "textarea", "select", "checkbox", "date"];
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
function ask(question) {
  return new Promise((resolve) => {
    rl.question(question, (answer) => {
      resolve(answer.trim());
    });
  });
}
function slugify(name) {
  return name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
}
function bold(text) {
  return `\x1B[1m${text}\x1B[0m`;
}
function cyan(text) {
  return `\x1B[36m${text}\x1B[0m`;
}
function green(text) {
  return `\x1B[32m${text}\x1B[0m`;
}
function dim(text) {
  return `\x1B[2m${text}\x1B[0m`;
}
async function collectField(formName, fieldIndex) {
  console.log("");
  console.log(dim(`  --- Field ${fieldIndex + 1} ---`));
  const name = await ask(`  Field name ${dim("(or 'done' to finish)")}: `);
  if (name.toLowerCase() === "done" || name === "") return null;
  const label = await ask(`  Display label ${dim(`[${name}]`)}: `);
  console.log(`  Types: ${FIELD_TYPES.map((t, i) => `${i + 1}) ${t}`).join("  ")}`);
  const typeInput = await ask(`  Field type ${dim("[1]")}: `);
  const typeIndex = typeInput ? parseInt(typeInput, 10) - 1 : 0;
  const type = FIELD_TYPES[typeIndex] || "text";
  const reqInput = await ask(`  Required? ${dim("[y/N]")}: `);
  const required = reqInput.toLowerCase() === "y" || reqInput.toLowerCase() === "yes";
  const field = {
    name: slugify(name) || name,
    type,
    required,
    label: label || name
  };
  if (type === "select") {
    const optionsStr = await ask(`  Options ${dim("(comma-separated)")}: `);
    field.options = optionsStr.split(",").map((o) => o.trim()).filter(Boolean);
    if (field.options.length === 0) {
      field.options = ["Option 1", "Option 2", "Option 3"];
    }
  }
  return field;
}
async function collectForm(formIndex) {
  console.log("");
  console.log(bold(cyan(`=== Form ${formIndex + 1} ===`)));
  const name = await ask("  Form name: ");
  const description = await ask(`  Description ${dim("[optional]")}: `);
  const fields = [];
  let fieldIndex = 0;
  while (true) {
    const field = await collectField(name, fieldIndex);
    if (!field) break;
    fields.push(field);
    fieldIndex++;
  }
  if (fields.length === 0) {
    console.log(dim("  No fields added \u2014 creating default 'name' field"));
    fields.push({ name: "name", type: "text", required: true, label: "Name" });
  }
  return {
    id: slugify(name) || `form-${formIndex + 1}`,
    name: name || `Form ${formIndex + 1}`,
    description: description || `${name || "Form"} submission form`,
    fields
  };
}
async function main() {
  console.log("");
  console.log(bold(cyan("  Form Builder Wizard")));
  console.log(dim("  Define your forms and their fields. Output: forms-config.json"));
  console.log("");
  const countStr = await ask(`How many forms? ${dim("[1]")}: `);
  const count = Math.max(1, parseInt(countStr, 10) || 1);
  const forms = [];
  for (let i = 0; i < count; i++) {
    const form = await collectForm(i);
    forms.push(form);
  }
  const config = {
    forms,
    created_at: (/* @__PURE__ */ new Date()).toISOString()
  };
  const outPath = path.join(process.cwd(), "forms-config.json");
  fs.writeFileSync(outPath, JSON.stringify(config, null, 2) + "\n");
  console.log("");
  console.log(green(`  Wrote ${outPath}`));
  console.log(dim(`  ${forms.length} form(s), ${forms.reduce((s, f) => s + f.fields.length, 0)} total field(s)`));
  console.log("");
  rl.close();
}
main().catch((err) => {
  console.error("Wizard error:", err);
  rl.close();
  process.exit(1);
});
