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

// src/generators/dts.ts
init_esm_shims();
import { writeFile } from "node:fs/promises";
import { generateTypes } from "untyped";
function generateDeclaration(schema) {
  return `
// Generated by @storm-software/untyped
// Do not edit this file directly

${generateTypes(schema, {
    addExport: true,
    partial: true,
    interfaceName: `${schema.title?.replaceAll(" ", "") || "Type"}Schema`
  })}

`;
}
function generateDeclarationFile(schema, file, config) {
  try {
    const declarations = getOutputFile(file, "d.ts");
    writeTrace(`Writing type declaration file ${declarations}`, config);
    return writeFile(declarations, generateDeclaration(schema));
  } catch (error) {
    writeError(
      `Error writing declaration 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 {
  generateDeclaration,
  generateDeclarationFile
};
