'use strict';

var U7VZULNU_cjs = require('./U7VZULNU.cjs');
var promises = require('fs/promises');
var path = require('path');
var zod = require('zod');

// src/commands/starknet.ts
U7VZULNU_cjs.init_cjs_shims();
var artifactIndexSchema = zod.z.object({
  contracts: zod.z.array(zod.z.object({
    contract_name: zod.z.string(),
    artifacts: zod.z.object({
      sierra: zod.z.string(),
      casm: zod.z.string()
    })
  }))
});
var buildKebabCase = /* @__PURE__ */ U7VZULNU_cjs.__name((name) => name.replaceAll(/[A-Z]/g, (character) => `-${character.toLowerCase()}`).replace(/^-/, ""), "buildKebabCase");
var buildCamelCase = /* @__PURE__ */ U7VZULNU_cjs.__name((name) => name.replace(/^[A-Z]/, (character) => character.toLowerCase()), "buildCamelCase");
var buildTypescriptSdk = /* @__PURE__ */ U7VZULNU_cjs.__name(async (packageName, targetDirectory, srcDirectory) => {
  const buildDirectory = path.join(targetDirectory, "release");
  const abiDirectory = path.join(srcDirectory, "abi");
  const casmDirectory = path.join(srcDirectory, "casm");
  const sierraDirectory = path.join(srcDirectory, "sierra");
  await promises.mkdir(abiDirectory, {
    recursive: true
  });
  await promises.mkdir(casmDirectory, {
    recursive: true
  });
  await promises.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 promises.readFile(path.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 = path.join(buildDirectory, artifacts.sierra);
    await promises.writeFile(path.join(abiDirectory, `${basename}.ts`), `export const ${name} = ${JSON.stringify(JSON.parse(await promises.readFile(contractFilename, "utf-8")).abi)} as const`);
    await promises.copyFile(path.join(buildDirectory, artifacts.casm), path.join(casmDirectory, `${basename}.json`));
    await promises.copyFile(contractFilename, path.join(sierraDirectory, `${basename}.json`));
  }
  await promises.writeFile(path.join(srcDirectory, "abi.ts"), contracts.map(({ basename }) => `export * from "./abi/${basename}.js";`).join("\n"));
  await promises.writeFile(path.join(srcDirectory, "casm.ts"), contracts.map(({ name, basename }) => `export { default as ${name} } from "./casm/${basename}.json";`).join("\n"));
  await promises.writeFile(path.join(srcDirectory, "sierra.ts"), contracts.map(({ name, basename }) => `export { default as ${name} } from "./sierra/${basename}.json";`).join("\n"));
}, "buildTypescriptSdk");

exports.buildTypescriptSdk = buildTypescriptSdk;
//# sourceMappingURL=DB5GRF5K.cjs.map
//# sourceMappingURL=DB5GRF5K.cjs.map