import _ from 'lodash' ; import pluralize from 'pluralize' ; import { nanoid } from 'nanoid' ; import LogType from 'constants/logType' ; import { getMonthFromIndex } from 'utils/time' ; /**
 * Notification associated events.
 */ export enum NotificationEvent { AnonGiftPaidUpgrade , AnonSubGift , AnonSubMysteryGift , BitsBadgeTier , ExtendedSub , GiftPaidUpgrade , Host , PrimePaidUpgrade , Raid , ReSub , RewardGift , RitualNewChatter , SubGift , Subscription , SubMysteryGift } /**
 * Notification class.
 * A notification can be a cheer, a raid, a ritual, a resub, a sub, a subgift, etc.
 */ export default class Notification implements Serializable < SerializedNotification > { param tags return public static fromSubMysteryGift ( tags ) { return new Notification ( Notification . getMysteryGiftNotificationTitle ( tags , false ) , NotificationEvent . SubMysteryGift ) ; } param tags return public static fromAnonSubGift ( tags ) { const recipient = tags [ 'msg-param-recipient-display-name' ] || tags [ 'msg-param-recipient-user-name' ] ; return new Notification ( `An anonymous gifter just gifted a sub to ${ recipient }!` , NotificationEvent . AnonSubGift ) ; } param tags return public static fromAnonSubMysteryGift ( tags ) { return new Notification ( Notification . getMysteryGiftNotificationTitle ( tags , true ) , NotificationEvent . AnonSubMysteryGift ) ; } param tags return public static fromGiftPaidUpgrade ( tags ) { const username = tags [ 'display-name' ] || tags . login ; const gifter = tags [ 'msg-param-sender-name' ] || tags [ 'msg-param-sender-login' ] ; return new Notification ( `${ username } is continuing the gift sub they got from ${ gifter }!` , NotificationEvent . GiftPaidUpgrade ) ; } param tags return public static fromAnonGiftPaidUpgrade ( tags ) { const username = tags [ 'display-name' ] || tags . login ; return new Notification ( `${ username } is continuing the gift sub they got from an anonymous gifter!` , NotificationEvent . AnonGiftPaidUpgrade ) ; } param tags return public static fromPrimePaidUpgrade ( tags ) { const username = tags [ 'display-name' ] || tags . login ; const plan = _ . get ( tags , 'msg-param-sub-plan' , '' ) ; let tier = 1 ; if ( plan === '2000' ) { tier = 2 ; } else if ( plan === '3000' ) { tier = 3 ; } return new Notification ( `${ username } just converted their Prime Gaming sub to a tier ${ tier } sub!` , NotificationEvent . PrimePaidUpgrade ) ; } param tags return public static fromRewardGift ( tags ) { const total = tags [ 'msg-param-selected-count' ] ; return new Notification ( `A cheer shared rewards to ${ total } others in chat!` , NotificationEvent . AnonGiftPaidUpgrade ) ; } param tags return public static fromBitsBadgeTier ( tags ) { const username = tags [ 'display-name' ] || tags . login ; const badge = parseInt ( tags [ 'msg-param-threshold' ] , 10 ) ; return new Notification ( _ . isNaN ( badge ) ? `${ username } just earned a new Bits badge!` : `${ username } just earned a new ${ badge . toLocaleString ( ) } Bits badge!` , NotificationEvent . BitsBadgeTier ) ; } param tags return public static fromExtendedSub ( tags ) { const username = tags [ 'display-name' ] || tags . login ; const endMonthIndex = tags [ 'msg-param-sub-benefit-end-month' ] ; const endMonth = getMonthFromIndex ( parseInt ( endMonthIndex , 10 ) ) ; const plan = _ . get ( tags , 'msg-param-sub-plan' , '' ) ; let tier = 1 ; if ( plan === '2000' ) { tier = 2 ; } else if ( plan === '3000' ) { tier = 3 ; } return new Notification ( `${ username } extended their Tier ${ tier } subscription through ${ endMonth }!` , NotificationEvent . ExtendedSub ) ; } param tags param anonymous return private static getMysteryGiftNotificationTitle ( tags , anonymous ) { const username = anonymous ? 'An anonymous gifter' : tags [ 'display-name' ] || tags . login ; const total = parseInt ( tags [ 'msg-param-mass-gift-count' ] , 10 ) ; return ! _ . isNaN ( total ) ? `${ username } is giving out ${ total } sub ${ pluralize ( 'gift' , total ) }!` : `${ username } is giving out a sub gift!` ; } private id : string ; class param title param event param message constructor ( private title , private event , private message ? ) { this . id = nanoid ( ) ; } return public serialize ( ) { return { event : this . event , id : this . id , message : this . message , title : this . title , type : LogType . Notification , } ; } } /**
 * Serialized notification.
 */ export type SerializedNotification = { id : string ; event : NotificationEvent ; type : LogType ; title : string ; message ? : string ; } ;