import { existsSync, mkdirSync, writeFileSync } from "fs";
import { join, dirname } from "path";
import { generate as generateViews } from "./views-generator.js";
import { generate as generateAppTsx } from "./app-tsx-generator.js";
import { generate as generatePackageJson } from "./package-json-generator.js";
import {
  loadHashManifest,
  reconcileWrites,
  summarize,
  HASHES_DIR,
  HASHES_FILE
} from "./regen-safety.js";
async function generate(context) {
  const { spec, outputDir } = context;
  const frontendDir = context.frontendDir || "frontend";
  const projectRoot = frontendDir === "." ? outputDir : join(outputDir, frontendDir);
  const proposed = {};
  const viewFiles = await generateViews({ spec });
  Object.assign(proposed, viewFiles);
  proposed["src/App.tsx"] = await generateAppTsx({ spec });
  proposed["package.json"] = await generatePackageJson({ spec });
  const prevManifest = loadHashManifest(projectRoot);
  const result = reconcileWrites(projectRoot, proposed, prevManifest);
  console.log(summarize(result, projectRoot));
  const toWrite = {
    ...result.approvedWrites,
    [`${HASHES_DIR}/${HASHES_FILE}`]: JSON.stringify(result.manifest, null, 2) + "\n"
  };
  for (const [rel, content] of Object.entries(toWrite)) {
    const abs = join(projectRoot, rel);
    const dir = dirname(abs);
    if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
    writeFileSync(abs, content, "utf-8");
  }
  return "";
}
var stdin_default = generate;
export {
  stdin_default as default,
  generate
};
