{"version":3,"file":"createModuleAugmentation.cjs","names":["getPathHash","writeFileIfChanged"],"sources":["../../../src/createType/createModuleAugmentation.ts"],"sourcesContent":["import { mkdir } from 'node:fs/promises';\nimport { basename, extname, join, relative } from 'node:path';\nimport { kebabCaseToCamelCase, normalizePath } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport fg from 'fast-glob';\nimport { getPathHash } from '../utils';\nimport { writeFileIfChanged } from '../writeFileIfChanged';\n\nexport const getTypeName = (key: string): string =>\n  `${kebabCaseToCamelCase(key)}Content`;\n\n/** Returns lines like: [Locales.FRENCH]: 1; */\nconst formatLocales = (locales: Locale[]) =>\n  locales.map((locale) => `    \"${locale}\": 1;`).join('\\n');\n\nconst zodToTsString = (schema: any): string => {\n  if (!schema) return 'any';\n\n  // Support both real Zod objects (_def) and serialized versions (def or nested)\n  const def = schema._def ?? schema.def ?? schema;\n\n  // Handle serialized type names (sometimes 'type' instead of 'typeName')\n  const typeName = def.typeName ?? def.type;\n\n  switch (typeName) {\n    case 'ZodString':\n    case 'string':\n      return 'string';\n    case 'ZodNumber':\n    case 'number':\n      return 'number';\n    case 'ZodBoolean':\n    case 'boolean':\n      return 'boolean';\n    case 'ZodNull':\n    case 'null':\n      return 'null';\n    case 'ZodUndefined':\n    case 'undefined':\n      return 'undefined';\n    case 'ZodArray':\n    case 'array':\n      return `${zodToTsString(def.type ?? def.element)}[]`;\n    case 'ZodObject':\n    case 'object': {\n      const shape = typeof def.shape === 'function' ? def.shape() : def.shape;\n      if (!shape) return 'Record<string, any>';\n\n      const entries = Object.entries(shape)\n        .map(([k, v]) => `      \"${k}\": ${zodToTsString(v)};`)\n        .join('\\n');\n      return `{\\n${entries}\\n    }`;\n    }\n    case 'ZodOptional':\n    case 'optional':\n      return `${zodToTsString(def.innerType ?? def.wrapped)} | undefined`;\n    case 'ZodNullable':\n    case 'nullable':\n      return `${zodToTsString(def.innerType ?? def.wrapped)} | null`;\n    case 'ZodUnion':\n    case 'union': {\n      const options = def.options ?? [];\n      return options.map(zodToTsString).join(' | ');\n    }\n    case 'ZodIntersection':\n    case 'intersection':\n      return `${zodToTsString(def.left)} & ${zodToTsString(def.right)}`;\n    case 'ZodEnum':\n    case 'enum': {\n      const values = def.values ?? [];\n      return values.map((v: string) => `\"${v}\"`).join(' | ');\n    }\n    case 'ZodLiteral':\n    case 'literal': {\n      const value = def.value;\n      return typeof value === 'string' ? `\"${value}\"` : String(value);\n    }\n    default:\n      return 'any';\n  }\n};\n\ntype ZodToTsFns = {\n  zodToTs: (schema: any, opts?: any) => { node: any };\n  printNode: (node: any) => string;\n  createAuxiliaryTypeStore: () => any;\n};\n\n/** Generate the content of the module augmentation file */\nconst generateTypeIndexContent = (\n  typeFiles: string[],\n  configuration: IntlayerConfig,\n  zodToTsFns: ZodToTsFns | null\n): string => {\n  const { internationalization, system, editor } = configuration;\n  const { moduleAugmentationDir } = system;\n  const { enabled } = editor;\n  const { locales, requiredLocales, strictMode } = internationalization;\n\n  let fileContent = 'import \"intlayer\";\\n';\n\n  // Build dictionary refs\n  const dictionariesRef = typeFiles.map((dictionaryPath) => ({\n    relativePath: `./${relative(moduleAugmentationDir, dictionaryPath)}`,\n    id: basename(dictionaryPath, extname(dictionaryPath)),\n    hash: `_${getPathHash(dictionaryPath)}`,\n  }));\n\n  // Import all dictionaries\n  for (const dictionary of dictionariesRef) {\n    fileContent += `import ${dictionary.hash} from '${dictionary.relativePath}';\\n`;\n  }\n  fileContent += '\\n';\n\n  // Dictionary map entries (id: typeof <hash>)\n  const formattedDictionaryMap: string = dictionariesRef\n    .map((dictionary) => `    \"${dictionary.id}\": typeof ${dictionary.hash};`)\n    .join('\\n');\n\n  // Ensure required ⊆ declared; if empty, default required = declared\n  const declared = locales;\n  const requiredSanitized = requiredLocales?.length\n    ? requiredLocales.filter((requiredLocales) =>\n        declared.includes(requiredLocales)\n      )\n    : declared;\n\n  const formattedDeclaredLocales = formatLocales(declared);\n  const formattedRequiredLocales = formatLocales(requiredSanitized);\n\n  // Build schema registry\n  const schemas = configuration.schemas ?? {};\n  const formattedSchemas = Object.entries(schemas)\n    .map(([key, schema]) => {\n      let typeStr = 'any';\n\n      if (schema) {\n        try {\n          if (zodToTsFns) {\n            const { node } = zodToTsFns.zodToTs(schema, {\n              auxiliaryTypeStore: zodToTsFns.createAuxiliaryTypeStore(),\n            });\n            // 133 is the kind for AnyKeyword in TypeScript\n            if ((node as any).kind !== 133) {\n              typeStr = zodToTsFns.printNode(node);\n            } else {\n              typeStr = zodToTsString(schema);\n            }\n          } else {\n            typeStr = zodToTsString(schema);\n          }\n        } catch (_e) {\n          // Fallback to custom string generator\n          typeStr = zodToTsString(schema);\n        }\n      }\n      return `    \"${key}\": ${typeStr};`;\n    })\n    .join('\\n');\n\n  // Choose strict mode registry key\n  const strictKey =\n    strictMode === 'strict'\n      ? 'strict'\n      : strictMode === 'inclusive'\n        ? 'inclusive'\n        : 'loose';\n\n  /**\n   * Module augmentation that ONLY adds keys to registries.\n   * No types/aliases redefined here—avoids merge conflicts.\n   */\n  fileContent += `declare module 'intlayer' {\\n`;\n  // Dictionaries registry\n  fileContent += `  interface __DictionaryRegistry {\\n${formattedDictionaryMap}\\n  }\\n\\n`;\n  // Locales registries\n  fileContent += `  interface __DeclaredLocalesRegistry {\\n${formattedDeclaredLocales}\\n  }\\n\\n`;\n  fileContent += `  interface __RequiredLocalesRegistry {\\n${formattedRequiredLocales}\\n  }\\n\\n`;\n  // Schema registry\n  fileContent += `  interface __SchemaRegistry {\\n${formattedSchemas}\\n  }\\n\\n`;\n  // Resolved strict mode (narrow the literal at build time)\n  fileContent += `  interface __StrictModeRegistry { mode: '${strictKey}' }\\n\\n`;\n  // Editor registry\n  fileContent += `  interface __EditorRegistry { enabled : ${enabled} } \\n`;\n  fileContent += `}\\n`;\n\n  return fileContent;\n};\n\n/** Generate the index file merging all the types */\nexport const createModuleAugmentation = async (\n  configuration: IntlayerConfig\n) => {\n  const { moduleAugmentationDir, typesDir } = configuration.system;\n\n  await mkdir(moduleAugmentationDir, { recursive: true });\n\n  const dictionariesTypesDefinitions: string[] = await fg(\n    normalizePath(`${typesDir}/*.ts`),\n    { ignore: ['**/*.d.ts'] }\n  );\n\n  let zodToTsFns: ZodToTsFns | null = null;\n  try {\n    const mod = await import('zod-to-ts');\n    zodToTsFns = {\n      zodToTs: mod.zodToTs,\n      printNode: mod.printNode,\n      createAuxiliaryTypeStore: mod.createAuxiliaryTypeStore,\n    };\n  } catch {\n    // typescript peer dep not installed (plain JS project), use fallback\n  }\n\n  const tsContent = generateTypeIndexContent(\n    dictionariesTypesDefinitions,\n    configuration,\n    zodToTsFns\n  );\n\n  const tsFilePath = join(moduleAugmentationDir, 'intlayer.d.ts');\n  await writeFileIfChanged(tsFilePath, tsContent);\n};\n"],"mappings":";;;;;;;;;;;AASA,MAAa,eAAe,QAC1B,oDAAwB,GAAG,EAAE;;AAG/B,MAAM,iBAAiB,YACrB,QAAQ,KAAK,WAAW,QAAQ,OAAO,MAAM,EAAE,KAAK,IAAI;AAE1D,MAAM,iBAAiB,WAAwB;CAC7C,IAAI,CAAC,QAAQ,OAAO;CAGpB,MAAM,MAAM,OAAO,QAAQ,OAAO,OAAO;CAKzC,QAFiB,IAAI,YAAY,IAAI,MAErC;EACE,KAAK;EACL,KAAK,UACH,OAAO;EACT,KAAK;EACL,KAAK,UACH,OAAO;EACT,KAAK;EACL,KAAK,WACH,OAAO;EACT,KAAK;EACL,KAAK,QACH,OAAO;EACT,KAAK;EACL,KAAK,aACH,OAAO;EACT,KAAK;EACL,KAAK,SACH,OAAO,GAAG,cAAc,IAAI,QAAQ,IAAI,OAAO,EAAE;EACnD,KAAK;EACL,KAAK,UAAU;GACb,MAAM,QAAQ,OAAO,IAAI,UAAU,aAAa,IAAI,MAAM,IAAI,IAAI;GAClE,IAAI,CAAC,OAAO,OAAO;GAKnB,OAAO,MAHS,OAAO,QAAQ,KAAK,EACjC,KAAK,CAAC,G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