{"version":3,"file":"generate.mjs","sources":["../../src/client/generate.ts"],"sourcesContent":["import { DocumentParts, inline, NewLine } from '../output/writer'\nimport { paramsFactory } from './params-factory'\nimport { appClient } from './app-client'\nimport { deployTypes } from './deploy-types'\nimport { utilityTypes } from './utility-types'\nimport { imports } from './imports'\nimport { createGeneratorContext, GeneratorOptions } from './generator-context'\nimport { appTypes } from './app-types'\nimport { callComposerType } from './call-composer-types'\nimport { Arc56Contract, ProgramSourceInfo, StructField } from '@algorandfoundation/algokit-utils/types/app-arc56'\nimport { appFactory } from './app-factory'\nimport { Sanitizer } from '../util/sanitization'\n\nfunction convertStructs(s: StructField[], sanitizer: Sanitizer): StructField[] {\n  return s.map(\n    ({ name, type }) =>\n      ({\n        name: sanitizer.makeSafePropertyIdentifier(name),\n        type: typeof type === 'string' ? type : convertStructs(type, sanitizer),\n      }) satisfies StructField,\n  )\n}\n\nfunction shrinkAppSpec(app: Arc56Contract, options: GeneratorOptions): Arc56Contract {\n  const strippedAppSpec = structuredClone(app)\n\n  // Only keep the source info if it is needed for error mapping\n  const shrinkSourceInfo = (sourceInfo: ProgramSourceInfo['sourceInfo']) => {\n    return sourceInfo\n      .filter((entry) => entry.errorMessage)\n      .map((entry) => ({\n        pc: entry.pc,\n        errorMessage: entry.errorMessage,\n        // Keep minimal context for error mapping if available\n        ...(entry.teal !== undefined && { teal: entry.teal }),\n      }))\n  }\n\n  // Keep only source info entries that can be used for approval and clear program error mapping\n  if (strippedAppSpec.sourceInfo?.approval?.sourceInfo && strippedAppSpec.sourceInfo.approval.sourceInfo.length > 0) {\n    strippedAppSpec.sourceInfo.approval.sourceInfo = shrinkSourceInfo(strippedAppSpec.sourceInfo.approval.sourceInfo)\n  }\n\n  if (strippedAppSpec.sourceInfo?.clear?.sourceInfo && strippedAppSpec.sourceInfo.clear.sourceInfo.length > 0) {\n    strippedAppSpec.sourceInfo.clear.sourceInfo = shrinkSourceInfo(strippedAppSpec.sourceInfo.clear.sourceInfo)\n  }\n\n  if (strippedAppSpec.compilerInfo) {\n    delete strippedAppSpec.compilerInfo\n  }\n\n  // These are used for deploying but not for calling deployed apps\n  if (options.mode === 'minimal') {\n    if (strippedAppSpec.source) {\n      delete strippedAppSpec.source\n    }\n    if (strippedAppSpec.byteCode) {\n      delete strippedAppSpec.byteCode\n    }\n    if (strippedAppSpec.templateVariables) {\n      delete strippedAppSpec.templateVariables\n    }\n    if (strippedAppSpec.scratchVariables) {\n      delete strippedAppSpec.scratchVariables\n    }\n  }\n  return strippedAppSpec\n}\n\nexport function* generate(app: Arc56Contract, options?: Partial<GeneratorOptions>): DocumentParts {\n  const resolvedOptions: GeneratorOptions = {\n    // Set defaults\n    preserveNames: false,\n    mode: 'full',\n    ...options,\n  }\n  const reduceAppSpec = shrinkAppSpec(app, resolvedOptions)\n\n  const ctx = createGeneratorContext(reduceAppSpec, resolvedOptions)\n  yield `/* eslint-disable */`\n  yield `/**`\n  yield ` * This file was automatically generated by @algorandfoundation/algokit-client-generator.`\n  yield ` * DO NOT MODIFY IT BY HAND.`\n  yield ` * requires: @algorandfoundation/algokit-utils: ^7`\n  yield ` */`\n\n  yield* imports(ctx)\n  // Change the structs definition to sanitize property names according to the defined rules\n  // for instance, this may (unless you passed in --preserve-names) convert properties like my_prop to myProp\n  reduceAppSpec.structs = Object.fromEntries(\n    Object.keys(reduceAppSpec.structs).map((key) => [key, convertStructs(reduceAppSpec.structs[key], ctx.sanitizer)]),\n  )\n  yield* inline('export const APP_SPEC: Arc56Contract = ', JSON.stringify(reduceAppSpec), ' as unknown as Arc56Contract')\n  yield NewLine\n\n  yield* utilityTypes()\n  yield NewLine\n  yield* appTypes(ctx)\n\n  if (ctx.mode === 'full') {\n    yield* deployTypes(ctx)\n  }\n  yield NewLine\n\n  // Write a call factory\n  yield* paramsFactory(ctx)\n  yield NewLine\n  // Write a factory in full mode\n  if (ctx.mode === 'full') {\n    yield* appFactory(ctx)\n  }\n  // Write a client\n  yield* appClient(ctx)\n\n  yield* callComposerType(ctx)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