{"version":3,"file":"process-notifications.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/processors/process-notifications.ts"],"names":[],"mappings":";;;AAAA,8EAG0C;AAQ1C,+EAAsE;AACtE,qFAGwC;AACxC,iFAAuE;AAEvE,MAAM,qBAAqB,GAAG,CAC5B,YAAkC,EACS,EAAE,CAC7C,wDAAkC,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;AAE5D,MAAM,qBAAqB,GAAG,CAC5B,YAAkC,EACkB,EAAE,CACtD,YAAY,CAAC,IAAI,KAAK,mCAAa,CAAC,qBAAqB,CAAC;AAE5D,MAAM,kBAAkB,GAAG,CACzB,YAAkC,EACG,EAAE,CACvC,YAAY,CAAC,IAAI,KAAK,mCAAa,CAAC,IAAI,CAAC;AAE3C;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CACjC,YAAkC,EAClC,oBAA8B,EAAE;IAEhC,MAAM,iBAAiB,GAAG,CAAC,cAAqB,EAAS,EAAE;QACzD,MAAM,IAAI,GAAW,YAAY,EAAE,IAAI,CAAC;QACxC,MAAM,IAAI,KAAK,CAAC,4CAA4C,IAAI,EAAE,CAAC,CAAC;IACtE,CAAC,CAAC;IAEF,IAAI,qBAAqB,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,MAAM,qBAAqB,GAAG,IAAA,yDAA0B,EAAC,YAAY,CAAC,CAAC;QACvE,qBAAqB,CAAC,MAAM,GAAG,IAAA,wDAAyB,EACtD,qBAAqB,EACrB,iBAAiB,CAClB,CAAC;QACF,OAAO,qBAAqB,CAAC;IAC/B,CAAC;IAED,IAAI,kBAAkB,CAAC,YAAY,CAAC,EAAE,CAAC;QACrC,OAAO,IAAA,oDAAuB,EAAC,YAAY,CAAC,CAAC;IAC/C,CAAC;IAED,IAAI,qBAAqB,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,OAAO,IAAA,mDAAuB,EAAC,YAAY,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,iBAAiB,CAAC,YAAY,CAAC,CAAC;AACzC,CAAC;AA3BD,kDA2BC;AAED;;;;;;GAMG;AACH,SAAgB,uBAAuB,CACrC,YAAkC,EAClC,oBAA8B,EAAE;IAEhC,IAAI,CAAC;QACH,MAAM,qBAAqB,GAAG,mBAAmB,CAC/C,YAAY,EACZ,iBAAiB,CAClB,CAAC;QACF,OAAO,qBAAqB,CAAC;IAC/B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAbD,0DAaC;AAED,MAAM,cAAc,GAAG,CAAO,IAAW,EAAgB,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AACnE,MAAM,6BAA6B,GAAG,CAC3C,aAAqC,EACrC,OAAiB,EACA,EAAE,CACnB,aAAa;KACV,GAAG,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,uBAAuB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;KACrE,MAAM,CAAC,cAAc,CAAC,CAAC;AANf,QAAA,6BAA6B,iCAMd","sourcesContent":["import {\n  TRIGGER_TYPES,\n  NOTIFICATION_API_TRIGGER_TYPES_SET,\n} from '../constants/notification-schema';\nimport type { FeatureAnnouncementRawNotification } from '../types/feature-announcement/feature-announcement';\nimport type { NormalisedAPINotification } from '../types/notification-api/notification-api';\nimport type {\n  INotification,\n  RawNotificationUnion,\n} from '../types/notification/notification';\nimport type { RawSnapNotification } from '../types/snaps';\nimport { processAPINotifications } from './process-api-notifications';\nimport {\n  isFeatureAnnouncementRead,\n  processFeatureAnnouncement,\n} from './process-feature-announcement';\nimport { processSnapNotification } from './process-snap-notifications';\n\nconst isOnChainNotification = (\n  notification: RawNotificationUnion,\n): notification is NormalisedAPINotification =>\n  NOTIFICATION_API_TRIGGER_TYPES_SET.has(notification.type);\n\nconst isFeatureAnnouncement = (\n  notification: RawNotificationUnion,\n): notification is FeatureAnnouncementRawNotification =>\n  notification.type === TRIGGER_TYPES.FEATURES_ANNOUNCEMENT;\n\nconst isSnapNotification = (\n  notification: RawNotificationUnion,\n): notification is RawSnapNotification =>\n  notification.type === TRIGGER_TYPES.SNAP;\n\n/**\n * Process feature announcement and wallet notifications into a shared/normalised notification shape.\n * We can still differentiate notifications by the `type` property\n *\n * @param notification - a feature announcement or on chain notification\n * @param readNotifications - all read notifications currently\n * @returns a processed notification\n */\nexport function processNotification(\n  notification: RawNotificationUnion,\n  readNotifications: string[] = [],\n): INotification {\n  const exhaustedAllCases = (_uncheckedCase: never): never => {\n    const type: string = notification?.type;\n    throw new Error(`No processor found for notification kind ${type}`);\n  };\n\n  if (isFeatureAnnouncement(notification)) {\n    const processedNotification = processFeatureAnnouncement(notification);\n    processedNotification.isRead = isFeatureAnnouncementRead(\n      processedNotification,\n      readNotifications,\n    );\n    return processedNotification;\n  }\n\n  if (isSnapNotification(notification)) {\n    return processSnapNotification(notification);\n  }\n\n  if (isOnChainNotification(notification)) {\n    return processAPINotifications(notification);\n  }\n\n  return exhaustedAllCases(notification);\n}\n\n/**\n * Safe version of processing a notification. Rather than throwing an error if failed to process, it will return the Notification or undefined\n *\n * @param notification - notification to processes\n * @param readNotifications - all read notifications currently\n * @returns a process notification or undefined if failed to process\n */\nexport function safeProcessNotification(\n  notification: RawNotificationUnion,\n  readNotifications: string[] = [],\n): INotification | undefined {\n  try {\n    const processedNotification = processNotification(\n      notification,\n      readNotifications,\n    );\n    return processedNotification;\n  } catch {\n    return undefined;\n  }\n}\n\nconst isNotUndefined = <Item>(item?: Item): item is Item => Boolean(item);\nexport const processAndFilterNotifications = (\n  notifications: RawNotificationUnion[],\n  readIds: string[],\n): INotification[] =>\n  notifications\n    .map((notification) => safeProcessNotification(notification, readIds))\n    .filter(isNotUndefined);\n"]}