import _ from 'lodash' ; import { nanoid } from 'nanoid' ; import { SerializedChatter } from 'libs/Chatter' ; /**
 * RegExp used to identify a valid action text.
 */ const TextRegExp = /^[\w\W]+$/ ; /**
 * RegExp used to identify a valid action name.
 */ const NameRegExp = /^[\w\- ]+$/ ; /**
 * RegExp used to identify a valid action recipient.
 */ const RecipientRegExp = /^[\w]{3,}$/ ; /**
 * Types of action available.
 */ export enum ActionType { Say = 'Say' , Whisper = 'Whisper' , Prepare = 'Prepare' , Open = 'Open URL' } /**
 * Action class.
 */ export default class Action implements Serializable < SerializedAction > { param editing param type param name param text param recipient return public static validate ( editing , type , text , name , recipient ? ) { const isWhisperAction = type === ActionType . Whisper ; const textValid = Action . validateValue ( editing , text , TextRegExp ) ; const nameValid = Action . validateValue ( editing , name , NameRegExp ) ; const recipientValid = isWhisperAction && ! _ . isNil ( recipient ) ? Action . validateValue ( editing , recipient , RecipientRegExp ) : true ; const textAndNameValid = textValid && nameValid ; const valid = isWhisperAction ? textAndNameValid && recipientValid : textAndNameValid ; return { name : nameValid , recipient : recipientValid , text : textValid , valid } ; } param action param placeholders return public static parse ( action , replacements ) { const compiledTemplate = _ . template ( action . text ) ; return compiledTemplate ( replacements ) ; } param editing param value param regexp return private static validateValue ( editing , value , regexp ) { const valid = regexp . test ( value ) ; return editing ? value . length > 0 && valid : valid ; } class param type param name param text param recipient param id constructor ( private type , private name , private text , private recipient ? , private id = nanoid ( ) ) { } return public serialize ( ) { return { id : this . id , name : this . name , recipient : this . type === ActionType . Whisper ? this . recipient : undefined , text : this . text , type : this . type , } ; } return public validate ( ) { return Action . validate ( false , this . type , this . text , this . name , this . recipient ) ; } } /**
 * Action placeholders.
 */ export enum ActionPlaceholder { Username = 'username' , Channel = 'channel' } /**
 * Action placeholders replacement values..
 */ type ActionReplacement = Record < ActionPlaceholder , string > ; /**
 * Serialized action.
 */ export type SerializedAction = { id : string ; name : string ; text : string ; type : ActionType ; recipient ? : string ; } ; /**
 * Action handler.
 */ export type ActionHandler = ( action : SerializedAction , chatter : Optional < SerializedChatter > ) => void ;