{"version":3,"file":"notificationTarget.mjs","names":[],"sources":["../../../../src/components/Notifications/notificationTarget.ts"],"sourcesContent":["import type { Notification } from 'stream-chat';\n\nconst NOTIFICATION_TARGET_PANELS = [\n  'channel',\n  'thread',\n  'channel-list',\n  'thread-list',\n  'modal',\n] as const;\n\n/** Panel where a notification is consumed. Set it in `origin.context.panel` to route by panel. */\nexport type NotificationTargetPanel = (typeof NOTIFICATION_TARGET_PANELS)[number];\n\nexport const isNotificationTargetPanel = (\n  value: unknown,\n): value is NotificationTargetPanel =>\n  typeof value === 'string' &&\n  (NOTIFICATION_TARGET_PANELS as readonly string[]).includes(value);\n\n// Panel implied by the composer in `origin.context`, so a thread composer's upload failure shows\n// in the thread, not its channel. Only the `thread` and `channel` contexts map to a panel.\nconst getPanelFromComposerContext = (\n  notification: Notification,\n): NotificationTargetPanel | undefined => {\n  const composer = notification.origin.context?.composer as\n    | { contextType?: unknown }\n    | undefined;\n\n  if (composer?.contextType === 'thread') return 'thread';\n  if (composer?.contextType === 'channel') return 'channel';\n  return undefined;\n};\n\nexport const getNotificationTargetPanel = (\n  notification: Notification,\n): NotificationTargetPanel | undefined => {\n  const targetTag = notification.tags?.find((tag) => tag.startsWith('target:'));\n  if (targetTag) {\n    const candidate = targetTag.slice('target:'.length);\n    if (isNotificationTargetPanel(candidate)) return candidate;\n  }\n  const panel = notification.origin.context?.panel;\n  if (isNotificationTargetPanel(panel)) return panel;\n  return getPanelFromComposerContext(notification);\n};\n\nexport const getNotificationTargetPanels = (\n  notification: Notification,\n): NotificationTargetPanel[] => {\n  const targetPanels = (notification.tags ?? [])\n    .filter((tag) => tag.startsWith('target:'))\n    .map((tag) => tag.slice('target:'.length))\n    .filter((value): value is NotificationTargetPanel =>\n      isNotificationTargetPanel(value),\n    );\n\n  if (targetPanels.length > 0) {\n    return Array.from(new Set(targetPanels));\n  }\n\n  const panel = notification.origin.context?.panel;\n  if (isNotificationTargetPanel(panel)) return [panel];\n\n  const composerPanel = getPanelFromComposerContext(notification);\n  return composerPanel ? [composerPanel] : [];\n};\n\nexport const getNotificationTargetTag = (panel: NotificationTargetPanel) =>\n  `target:${panel}` as const;\n\nexport const addNotificationTargetTag = (\n  panel: NotificationTargetPanel | undefined,\n  tags?: string[],\n) => {\n  if (!panel) return tags ?? [];\n  return Array.from(new Set([getNotificationTargetTag(panel), ...(tags ?? [])]));\n};\n\nexport const isNotificationForPanel = (\n  notification: Notification,\n  panel: NotificationTargetPanel,\n  options?: { fallbackPanel?: NotificationTargetPanel },\n) => {\n  const explicitTargetPanels = getNotificationTargetPanels(notification);\n  if (explicitTargetPanels.length > 0) {\n    return explicitTargetPanels.includes(panel);\n  }\n\n  const resolvedPanel = options?.fallbackPanel ?? 'channel';\n  return resolvedPanel === panel;\n};\n"],"mappings":";AAEA,IAAM,6BAA6B;CACjC;CACA;CACA;CACA;CACA;AACF;AAKA,IAAa,6BACX,UAEA,OAAO,UAAU,YAChB,2BAAiD,SAAS,KAAK;AAIlE,IAAM,+BACJ,iBACwC;CACxC,MAAM,WAAW,aAAa,OAAO,SAAS;CAI9C,IAAI,UAAU,gBAAgB,UAAU,OAAO;CAC/C,IAAI,UAAU,gBAAgB,WAAW,OAAO;AAElD;AAEA,IAAa,8BACX,iBACwC;CACxC,MAAM,YAAY,aAAa,MAAM,MAAM,QAAQ,IAAI,WAAW,SAAS,CAAC;CAC5E,IAAI,WAAW;EACb,MAAM,YAAY,UAAU,MAAM,CAAgB;EAClD,IAAI,0BAA0B,SAAS,GAAG,OAAO;CACnD;CACA,MAAM,QAAQ,aAAa,OAAO,SAAS;CAC3C,IAAI,0BAA0B,KAAK,GAAG,OAAO;CAC7C,OAAO,4BAA4B,YAAY;AACjD;AAEA,IAAa,+BACX,iBAC8B;CAC9B,MAAM,gBAAgB,aAAa,QAAQ,CAAC,EAAA,CACzC,QAAQ,QAAQ,IAAI,WAAW,SAAS,CAAC,CAAC,CAC1C,KAAK,QAAQ,IAAI,MAAM,CAAgB,CAAC,CAAC,CACzC,QA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