import { init_esm_shims, __name } from './4RNWLMHM.js';
import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises';
import { join } from 'node:path';
import { z } from 'zod';

// src/commands/starknet.ts
init_esm_shims();
var artifactIndexSchema = z.object({
  contracts: z.array(z.object({
    contract_name: z.string(),
    artifacts: z.object({
      sierra: z.string(),
      casm: z.string()
    })
  }))
});
var buildKebabCase = /* @__PURE__ */ __name((name) => name.replaceAll(/[A-Z]/g, (character) => `-${character.toLowerCase()}`).replace(/^-/, ""), "buildKebabCase");
var buildCamelCase = /* @__PURE__ */ __name((name) => name.replace(/^[A-Z]/, (character) => character.toLowerCase()), "buildCamelCase");
var buildTypescriptSdk = /* @__PURE__ */ __name(async (packageName, targetDirectory, srcDirectory) => {
  const buildDirectory = join(targetDirectory, "release");
  const abiDirectory = join(srcDirectory, "abi");
  const casmDirectory = join(srcDirectory, "casm");
  const sierraDirectory = join(srcDirectory, "sierra");
  await mkdir(abiDirectory, {
    recursive: true
  });
  await mkdir(casmDirectory, {
    recursive: true
  });
  await mkdir(sierraDirectory, {
    recursive: true
  });
  const packageNames = packageName.split(",").map((name) => name.trim());
  const allContracts = [];
  for (const pkgName of packageNames) {
    const packageContracts = artifactIndexSchema.parse(JSON.parse(await readFile(join(buildDirectory, `${pkgName}.starknet_artifacts.json`), "utf-8"))).contracts.map(({ contract_name, artifacts }) => ({
      name: buildCamelCase(contract_name),
      basename: buildKebabCase(contract_name),
      artifacts
    }));
    allContracts.push(...packageContracts);
  }
  const contracts = allContracts;
  for (const { name, basename, artifacts } of contracts) {
    const contractFilename = join(buildDirectory, artifacts.sierra);
    await writeFile(join(abiDirectory, `${basename}.ts`), `export const ${name} = ${JSON.stringify(JSON.parse(await readFile(contractFilename, "utf-8")).abi)} as const`);
    await copyFile(join(buildDirectory, artifacts.casm), join(casmDirectory, `${basename}.json`));
    await copyFile(contractFilename, join(sierraDirectory, `${basename}.json`));
  }
  await writeFile(join(srcDirectory, "abi.ts"), contracts.map(({ basename }) => `export * from "./abi/${basename}.js";`).join("\n"));
  await writeFile(join(srcDirectory, "casm.ts"), contracts.map(({ name, basename }) => `export { default as ${name} } from "./casm/${basename}.json";`).join("\n"));
  await writeFile(join(srcDirectory, "sierra.ts"), contracts.map(({ name, basename }) => `export { default as ${name} } from "./sierra/${basename}.json";`).join("\n"));
}, "buildTypescriptSdk");

export { buildTypescriptSdk };
//# sourceMappingURL=BTXB2LKA.js.map
//# sourceMappingURL=BTXB2LKA.js.map