"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }


var _chunkIBKL4TDUcjs = require('./chunk-IBKL4TDU.cjs');



var _chunkV6PBBEPGcjs = require('./chunk-V6PBBEPG.cjs');

// src/generators/markdown.ts
_chunkV6PBBEPGcjs.init_cjs_shims.call(void 0, );
var _promises = require('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 = _chunkV6PBBEPGcjs.getOutputFile.call(void 0, file, "md");
    _chunkIBKL4TDUcjs.writeTrace.call(void 0, `Writing type markdown file ${declarations}`, config);
    return _promises.writeFile.call(void 0, declarations, generateMarkdown(schema, generatedBy));
  } catch (error) {
    _chunkIBKL4TDUcjs.writeError.call(void 0, 
      `Error writing markdown file for ${file.name}

Error:
${_optionalChain([error, 'optionalAccess', _ => _.message]) ? error.message : JSON.stringify(error)}${_optionalChain([error, 'optionalAccess', _2 => _2.stack]) ? `
Stack Trace: ${error.stack}` : ""}

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




exports.generateMarkdown = generateMarkdown; exports.generateMarkdownFile = generateMarkdownFile;
