import { compileAssetArchive, compileStaticStickerCatalog } from "./index.js";

function argument(name: string): string {
  const index = process.argv.indexOf(name);
  const value = index >= 0 ? process.argv[index + 1] : undefined;
  if (!value) throw new Error(`missing ${name}`);
  return value;
}

try {
  const expectedCount = process.argv.includes("--expected-count") ? Number(argument("--expected-count")) : undefined;
  const result = process.argv.includes("--catalog")
    ? compileStaticStickerCatalog({
      ...(expectedCount === undefined ? {} : { expectedCount }),
      outputDirectory: argument("--output"),
      releaseVersion: argument("--release-version"),
      sourceRoot: argument("--source-root"),
    })
    : compileAssetArchive({
      manifestPath: argument("--manifest"),
      outputDirectory: argument("--output"),
      releaseVersion: argument("--release-version"),
    });
  process.stdout.write(`${JSON.stringify(result.report, null, 2)}\n`);
} catch (error) {
  process.stderr.write(`${error instanceof Error ? error.message : "asset compilation failed"}\n`);
  process.exitCode = 1;
}
