import {
  writeError,
  writeTrace
} from "./chunk-AZBH2WKK.js";
import {
  getOutputFile,
  init_esm_shims
} from "./chunk-MVKH3GOZ.js";

// src/generators/markdown.ts
init_esm_shims();
import { writeFile } from "node:fs/promises";
function generateMarkdown(schema, generatedBy = "@storm-software/untyped") {
  return `
<!-- Generated by ${generatedBy} -->
<!-- Do not edit this file directly -->

${generateMarkdownLevel(schema, schema.title || "", "#").join("\n")}

`;
}
function generateMarkdownLevel(schema, title, level) {
  const lines = [];
  lines.push(`${level} ${title}`);
  if (schema.type === "object") {
    for (const key in schema.properties) {
      const val = schema.properties[key];
      lines.push("", ...generateMarkdownLevel(val, `\`${key}\``, level + "#"));
    }
    return lines;
  }
  lines.push(
    `- **Type**: \`${schema.markdownType || schema.tsType || schema.type}\``
  );
  if ("default" in schema) {
    lines.push(`- **Default**: \`${JSON.stringify(schema.default)}\``);
  }
  lines.push("");
  if (schema.title) {
    lines.push("> " + schema.title, "");
  }
  if (schema.description) {
    lines.push("", schema.description, "");
  }
  return lines;
}
function generateMarkdownFile(schema, file, generatedBy = "@storm-software/untyped", config) {
  try {
    const declarations = getOutputFile(file, "md");
    writeTrace(`Writing type markdown file ${declarations}`, config);
    return writeFile(declarations, generateMarkdown(schema, generatedBy));
  } catch (error) {
    writeError(
      `Error writing markdown file for ${file.name}

Error:
${error?.message ? error.message : JSON.stringify(error)}${error?.stack ? `
Stack Trace: ${error.stack}` : ""}

Parsed schema:
${JSON.stringify(schema)}
`,
      config
    );
    throw error;
  }
}

export {
  generateMarkdown,
  generateMarkdownFile
};
