{"version":3,"file":"NotificationTranslationTopic.mjs","names":[],"sources":["../../../../../src/i18n/TranslationBuilder/notifications/NotificationTranslationTopic.ts"],"sourcesContent":["import { TranslationTopic } from '../../TranslationBuilder';\nimport type { Notification } from 'stream-chat';\nimport type { NotificationTranslatorOptions } from './types';\nimport { translatorsByNotificationType } from './translatorsByNotificationType';\nimport type { TranslationTopicOptions, Translator } from '../../index';\nimport type { StreamTFunction } from '../../types';\n\nconst translateByNotificationType: Translator<NotificationTranslatorOptions> = ({\n  options: { notification },\n  ...params\n}) => {\n  if (!notification?.type) return null;\n  const translator = translatorsByNotificationType[notification.type];\n  if (!translator) return null;\n  return translator({ ...params, options: { notification } });\n};\n\nexport const defaultNotificationTranslators: Record<\n  string,\n  Translator<NotificationTranslatorOptions>\n> = {\n  '*': translateByNotificationType,\n};\n\nexport class NotificationTranslationTopic extends TranslationTopic<NotificationTranslatorOptions> {\n  constructor({ i18next, translators }: TranslationTopicOptions) {\n    super({ i18next, translators: defaultNotificationTranslators });\n    if (translators) {\n      Object.entries(translators).forEach(([name, translator]) => {\n        this.setTranslator(name, translator);\n      });\n    }\n  }\n\n  translate = (value: string, key: string, options: { notification?: Notification }) => {\n    const { notification } = options;\n    // i18next's `TFunction` is untyped; the translators take the SDK's `StreamTFunction`.\n    const t = this.i18next.t as unknown as StreamTFunction;\n    if (!notification) return value;\n    const byType = notification.type\n      ? this.translators.get(notification.type)\n      : undefined;\n    if (byType) return byType({ key, options, t, value }) || value;\n\n    const byFallback = this.translators.get('*');\n    const translated = byFallback?.({ key, options, t, value }) ?? null;\n    if (translated) return translated;\n    if (!notification.message) return value;\n\n    // No translator claims this type (e.g. a newer `stream-chat` or integrator code): render the\n    // English message rather than a blank or a raw dotted key.\n    return notification.message;\n  };\n}\n"],"mappings":";;;AAOA,IAAM,+BAA0E,EAC9E,SAAS,EAAE,gBACX,GAAG,aACC;CACJ,IAAI,CAAC,cAAc,MAAM,OAAO;CAChC,MAAM,aAAa,8BAA8B,aAAa;CAC9D,IAAI,CAAC,YAAY,OAAO;CACxB,OAAO,WAAW;EAAE,GAAG;EAAQ,SAAS,EAAE,aAAa;CAAE,CAAC;AAC5D;AAEA,IAAa,iCAGT,EACF,KAAK,4BACP;AAEA,IAAa,+BAAb,cAAkD,iBAAgD;CAChG,YAAY,EAAE,SAAS,eAAwC;EAC7D,MAAM;GAAE;GAAS,aAAa;EAA+B,CAAC;EAC9D,IAAI,aACF,OAAO,QAAQ,WAAW,CAAC,CAAC,SAAS,CAAC,MAAM,gBAAgB;GAC1D,KAAK,cAAc,MAAM,UAAU;EACrC,CAAC;CAEL;CAEA,aAAa,OAAe,KAAa,YAA6C;EACpF,MAAM,EAAE,iBAAiB;EAEzB,MAAM,IAAI,KAAK,QAAQ;EACvB,IAAI,CAAC,cAAc,OAAO;EAC1B,MAAM,SAAS,aAAa,OACxB,KAAK,YAAY,IAAI,aAAa,IAAI,IACtC,KAAA;EACJ,IAAI,QAAQ,OAAO,OAAO;GAAE;GAAK;GAAS;GAAG;EAAM,CAAC,KAAK;EAGzD,MAAM,aADa,KAAK,YAAY,IAAI,GACrB,CAAA,GAAa;GAAE;GAAK;GAAS;GAAG;EAAM,CAAC,KAAK;EAC/D,IAAI,YAAY,OAAO;EACvB,IAAI,CAAC,aAAa,SAAS,OAAO;EAIlC,OAAO,aAAa;CACtB;AACF"}