{"version":3,"sources":["../src/NotificationServicesPushController/utils/get-notification-message.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/naming-convention */\nimport type { Types } from '../../NotificationServicesController';\nimport { Constants } from '../../NotificationServicesController';\nimport { getAmount, formatAmount } from './get-notification-data';\n\nexport type TranslationKeys = {\n  pushPlatformNotificationsFundsSentTitle: () => string;\n  pushPlatformNotificationsFundsSentDescriptionDefault: () => string;\n  pushPlatformNotificationsFundsSentDescription: (\n    ...args: [string, string]\n  ) => string;\n  pushPlatformNotificationsFundsReceivedTitle: () => string;\n  pushPlatformNotificationsFundsReceivedDescriptionDefault: () => string;\n  pushPlatformNotificationsFundsReceivedDescription: (\n    ...args: [string, string]\n  ) => string;\n  pushPlatformNotificationsSwapCompletedTitle: () => string;\n  pushPlatformNotificationsSwapCompletedDescription: () => string;\n  pushPlatformNotificationsNftSentTitle: () => string;\n  pushPlatformNotificationsNftSentDescription: () => string;\n  pushPlatformNotificationsNftReceivedTitle: () => string;\n  pushPlatformNotificationsNftReceivedDescription: () => string;\n  pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle: () => string;\n  pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription: () => string;\n  pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle: () => string;\n  pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription: () => string;\n  pushPlatformNotificationsStakingLidoStakeCompletedTitle: () => string;\n  pushPlatformNotificationsStakingLidoStakeCompletedDescription: () => string;\n  pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle: () => string;\n  pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription: () => string;\n  pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle: () => string;\n  pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription: () => string;\n  pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle: () => string;\n  pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription: () => string;\n};\n\ntype PushNotificationMessage = {\n  title: string;\n  description: string;\n};\n\ntype NotificationMessage<N extends Types.INotification> = {\n  title: string | null;\n  defaultDescription: string | null;\n  getDescription?: (n: N) => string | null;\n};\n\ntype NotificationMessageDict = {\n  [K in Constants.TRIGGER_TYPES]?: NotificationMessage<\n    Extract<Types.INotification, { type: K }>\n  >;\n};\n\n/**\n * On Chain Push Notification Messages.\n * This is a list of all the push notifications we support. Update this for synced notifications on mobile and extension\n *\n * @param translationKeys - all translations supported\n * @returns A translation push message object.\n */\nexport const createOnChainPushNotificationMessages = (\n  translationKeys: TranslationKeys,\n): NotificationMessageDict => {\n  type TranslationFn = <K extends keyof TranslationKeys>(\n    ...args: [K, ...Parameters<TranslationKeys[K]>]\n  ) => string;\n  const t: TranslationFn = (...args) => {\n    const [key, ...otherArgs] = args;\n\n    // Coerce types for the translation function\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const fn: any = translationKeys[key];\n    return fn(...otherArgs);\n  };\n\n  return {\n    erc20_sent: {\n      title: t('pushPlatformNotificationsFundsSentTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsFundsSentDescriptionDefault',\n      ),\n      getDescription: (n) => {\n        const symbol = n?.data?.token?.symbol;\n        const tokenAmount = n?.data?.token?.amount;\n        const tokenDecimals = n?.data?.token?.decimals;\n        if (!symbol || !tokenAmount || !tokenDecimals) {\n          return null;\n        }\n\n        const amount = getAmount(tokenAmount, tokenDecimals, {\n          shouldEllipse: true,\n        });\n        return t(\n          'pushPlatformNotificationsFundsSentDescription',\n          amount,\n          symbol,\n        );\n      },\n    },\n    eth_sent: {\n      title: t('pushPlatformNotificationsFundsSentTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsFundsSentDescriptionDefault',\n      ),\n      getDescription: (n) => {\n        const symbol = getChainSymbol(n?.chain_id);\n        const tokenAmount = n?.data?.amount?.eth;\n        if (!symbol || !tokenAmount) {\n          return null;\n        }\n\n        const amount = formatAmount(parseFloat(tokenAmount), {\n          shouldEllipse: true,\n        });\n        return t(\n          'pushPlatformNotificationsFundsSentDescription',\n          amount,\n          symbol,\n        );\n      },\n    },\n    erc20_received: {\n      title: t('pushPlatformNotificationsFundsReceivedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsFundsReceivedDescriptionDefault',\n      ),\n      getDescription: (n) => {\n        const symbol = n?.data?.token?.symbol;\n        const tokenAmount = n?.data?.token?.amount;\n        const tokenDecimals = n?.data?.token?.decimals;\n        if (!symbol || !tokenAmount || !tokenDecimals) {\n          return null;\n        }\n\n        const amount = getAmount(tokenAmount, tokenDecimals, {\n          shouldEllipse: true,\n        });\n        return t(\n          'pushPlatformNotificationsFundsReceivedDescription',\n          amount,\n          symbol,\n        );\n      },\n    },\n    eth_received: {\n      title: t('pushPlatformNotificationsFundsReceivedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsFundsReceivedDescriptionDefault',\n      ),\n      getDescription: (n) => {\n        const symbol = getChainSymbol(n?.chain_id);\n        const tokenAmount = n?.data?.amount?.eth;\n        if (!symbol || !tokenAmount) {\n          return null;\n        }\n\n        const amount = formatAmount(parseFloat(tokenAmount), {\n          shouldEllipse: true,\n        });\n        return t(\n          'pushPlatformNotificationsFundsReceivedDescription',\n          amount,\n          symbol,\n        );\n      },\n    },\n    metamask_swap_completed: {\n      title: t('pushPlatformNotificationsSwapCompletedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsSwapCompletedDescription',\n      ),\n    },\n    erc721_sent: {\n      title: t('pushPlatformNotificationsNftSentTitle'),\n      defaultDescription: t('pushPlatformNotificationsNftSentDescription'),\n    },\n    erc1155_sent: {\n      title: t('pushPlatformNotificationsNftSentTitle'),\n      defaultDescription: t('pushPlatformNotificationsNftSentDescription'),\n    },\n    erc721_received: {\n      title: t('pushPlatformNotificationsNftReceivedTitle'),\n      defaultDescription: t('pushPlatformNotificationsNftReceivedDescription'),\n    },\n    erc1155_received: {\n      title: t('pushPlatformNotificationsNftReceivedTitle'),\n      defaultDescription: t('pushPlatformNotificationsNftReceivedDescription'),\n    },\n    rocketpool_stake_completed: {\n      title: t('pushPlatformNotificationsStakingRocketpoolStakeCompletedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsStakingRocketpoolStakeCompletedDescription',\n      ),\n    },\n    rocketpool_unstake_completed: {\n      title: t(\n        'pushPlatformNotificationsStakingRocketpoolUnstakeCompletedTitle',\n      ),\n      defaultDescription: t(\n        'pushPlatformNotificationsStakingRocketpoolUnstakeCompletedDescription',\n      ),\n    },\n    lido_stake_completed: {\n      title: t('pushPlatformNotificationsStakingLidoStakeCompletedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsStakingLidoStakeCompletedDescription',\n      ),\n    },\n    lido_stake_ready_to_be_withdrawn: {\n      title: t(\n        'pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnTitle',\n      ),\n      defaultDescription: t(\n        'pushPlatformNotificationsStakingLidoStakeReadyToBeWithdrawnDescription',\n      ),\n    },\n    lido_withdrawal_requested: {\n      title: t('pushPlatformNotificationsStakingLidoWithdrawalRequestedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsStakingLidoWithdrawalRequestedDescription',\n      ),\n    },\n    lido_withdrawal_completed: {\n      title: t('pushPlatformNotificationsStakingLidoWithdrawalCompletedTitle'),\n      defaultDescription: t(\n        'pushPlatformNotificationsStakingLidoWithdrawalCompletedDescription',\n      ),\n    },\n  };\n};\n\n/**\n * Retrieves the symbol associated with a given chain ID.\n *\n * @param chainId - The ID of the chain.\n * @returns The symbol associated with the chain ID, or null if not found.\n */\nfunction getChainSymbol(chainId: number) {\n  return Constants.CHAIN_SYMBOLS[chainId] ?? null;\n}\n\n/**\n * Checks if the given value is an OnChainRawNotification object.\n *\n * @param n - The value to check.\n * @returns True if the value is an OnChainRawNotification object, false otherwise.\n */\nexport function isOnChainNotification(\n  n: unknown,\n): n is Types.OnChainRawNotification {\n  const assumed = n as Types.OnChainRawNotification;\n\n  // We don't have a validation/parsing library to check all possible types of an on chain notification\n  // It is safe enough just to check \"some\" fields, and catch any errors down the line if the shape is bad.\n  const isValidEnoughToBeOnChainNotification = [\n    assumed?.id,\n    assumed?.data,\n    assumed?.trigger_id,\n  ].every((field) => field !== undefined);\n  return isValidEnoughToBeOnChainNotification;\n}\n\n/**\n * Creates a push notification message based on the given on-chain raw notification.\n *\n * @param n - processed notification.\n * @param translations - translates keys into text\n * @returns The push notification message object, or null if the notification is invalid.\n */\nexport function createOnChainPushNotificationMessage(\n  n: Types.INotification,\n  translations: TranslationKeys,\n): PushNotificationMessage | null {\n  if (!n?.type) {\n    return null;\n  }\n  const notificationMessage =\n    createOnChainPushNotificationMessages(translations)[n.type];\n\n  if (!notificationMessage) {\n    return null;\n  }\n\n  let description: string | null = null;\n  try {\n    description =\n      // eslint-disable-next-line @typescript-eslint/no-explicit-any\n      notificationMessage?.getDescription?.(n as any) ??\n      notificationMessage.defaultDescription ??\n      null;\n  } catch (e) {\n    description = notificationMessage.defaultDescription ?? null;\n  }\n\n  return {\n    title: notificationMessage.title ?? '', // Ensure title is always a string\n    description: description ?? '', // Fallback to empty string if null\n  };\n}\n"],"mappings":";;;;;;;;;AA4DO,IAAM,wCAAwC,CACnD,oBAC4B;AAI5B,QAAM,IAAmB,IAAI,SAAS;AACpC,UAAM,CAAC,KAAK,GAAG,SAAS,IAAI;AAI5B,UAAM,KAAU,gBAAgB,GAAG;AACnC,WAAO,GAAG,GAAG,SAAS;AAAA,EACxB;AAEA,SAAO;AAAA,IACL,YAAY;AAAA,MACV,OAAO,EAAE,yCAAyC;AAAA,MAClD,oBAAoB;AAAA,QAClB;AAAA,MACF;AAAA,MACA,gBAAgB,CAAC,MAAM;AACrB,cAAM,SAAS,GAAG,MAAM,OAAO;AAC/B,cAAM,cAAc,GAAG,MAAM,OAAO;AACpC,cAAM,gBAAgB,GAAG,MAAM,OAAO;AACtC,YAAI,CAAC,UAAU,CAAC,eAAe,CAAC,eAAe;AAC7C,iBAAO;AAAA,QACT;AAEA,cAAM,SAAS,UAAU,aAAa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