import {
  generateDeclarationFile
} from "./chunk-ZKGXNMK5.js";
import {
  generateJsonSchemaFile
} from "./chunk-6RROZSGU.js";
import {
  generateMarkdownFile
} from "./chunk-2KLJYM3Z.js";
import {
  writeError,
  writeTrace
} from "./chunk-IYT3SLKC.js";
import {
  init_esm_shims,
  joinPaths
} from "./chunk-TMZJSJ66.js";

// src/generate.ts
init_esm_shims();
import { glob } from "glob";
import { loadSchema } from "untyped/loader";
var getGenerateAction = (config) => async (options) => {
  writeTrace(
    `Running Storm Untyped with options: ${JSON.stringify(options)}`,
    config
  );
  const files = await glob(options.entry || "**/{untyped.ts,*.untyped.ts}", {
    ignore: [
      "**/{*.stories.tsx,*.stories.ts,*.spec.tsx,*.spec.ts}",
      "**/dist/**",
      "**/tmp/**",
      "**/node_modules/**",
      "**/.git/**",
      "**/.cache/**",
      "**/.nx/**"
    ],
    withFileTypes: true,
    cwd: config.workspaceRoot
  });
  await Promise.all(
    files.map(async (file) => {
      writeTrace(
        `Generating files for schema file: ${joinPaths(file.parentPath, file.name)}`,
        config
      );
      let schema;
      try {
        schema = await loadSchema(joinPaths(file.parentPath, file.name), {
          jiti: {
            fsCache: config.skipCache ? false : joinPaths(
              config.directories.cache || joinPaths(
                config.workspaceRoot,
                "node_modules/.cache/storm"
              ),
              "jiti"
            ),
            interopDefault: true
          }
        });
      } catch (error) {
        writeError(
          `Error while parsing schema file: ${joinPaths(file.parentPath, file.name)}

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

Parsed schema:
${JSON.stringify(schema)}
`,
          config
        );
        throw error;
      }
      const promises = [];
      promises.push(generateDeclarationFile(schema, file, config));
      promises.push(generateMarkdownFile(schema, file, config));
      promises.push(generateJsonSchemaFile(schema, file, config));
      return Promise.all(promises);
    })
  );
};

export {
  getGenerateAction
};
