import type { SerializableTypedMessage , SerializableTypedMessages , NonSerializableTypedMessage , TypedMessage , } from './base' ; import { isSerializableTypedMessage } from './utils' ; import { composeEvery , composeSome , createIsType } from './utils/internal' ; type Meta = TypedMessage [ 'meta' ] ; export type WellKnownCoreTypedMessages = TypedMessageText | TypedMessageImage | TypedMessageTuple | TypedMessageTupleSerializable | TypedMessageUnknown | TypedMessageEmpty | TypedMessagePromise ; export type TypedMessageText = TypedMessageTextV1 ; /** A text message */ export interface TypedMessageTextV1 extends SerializableTypedMessage < 1 > { readonly type : 'text' ; readonly content : string ; } export const isTypedMessageTextV1 = createIsType < TypedMessageTextV1 > ( 'text' , 1 ) ; export const isTypedMessageText = composeSome ( isTypedMessageTextV1 ) as ( x : TypedMessage ) => x is TypedMessageText ; export function makeTypedMessageText ( text , meta ? ) { return { type : 'text' , version : 1 , serializable : true , content : text , meta } ; } export type TypedMessageImage = TypedMessageImageV1 ; /**
 * A single image
 * TODO: it should be serializable but still not decided how to do that yet.
 */ export interface TypedMessageImageV1 extends NonSerializableTypedMessage { readonly type : 'image' ; readonly image : string | Blob ; readonly width ? : number ; readonly height ? : number ; } export const isTypedMessageImageV1 = createIsType < TypedMessageImageV1 > ( 'image' ) ; export const isTypedMessageImage = composeSome ( isTypedMessageImageV1 ) as ( x : TypedMessage ) => x is TypedMessageImage ; export function makeTypedMessageImage ( image , size ? , meta ? ) { return { type : 'image' , serializable : false , image , width : size ?. width , height : size ?. height , meta } ; } /** Multiple TypedMessages (ordered) */ export interface TypedMessageTuple < T extends readonly TypedMessage [ ] = readonly TypedMessage [ ] > extends TypedMessage { readonly type : 'tuple' ; readonly items : T ; } export interface TypedMessageTupleSerializable < T extends readonly SerializableTypedMessages [ ] = readonly SerializableTypedMessages [ ] > extends SerializableTypedMessage < 1 > , TypedMessageTuple < T > { readonly type : 'tuple' ; } export const isTypedMessageTuple = ( x ) : x is TypedMessageTuple | TypedMessageTupleSerializable => x . type === 'tuple' ; export const isTypedMessageTupleSerializable = composeEvery ( isTypedMessageTuple , isSerializableTypedMessage ) as any ; export function makeTypedMessageTuple < T extends readonly TypedMessage [ ] = readonly TypedMessage [ ] > ( items , meta ? ) { return { type : 'tuple' , items , meta } ; } export function makeTypedMessageTupleFromList < T extends readonly TypedMessage [ ] = readonly TypedMessage [ ] > ( ... args ) { return { type : 'tuple' , items : args } ; } export function makeTypedMessageTupleSerializable < T extends readonly SerializableTypedMessages [ ] = readonly SerializableTypedMessages [ ] > ( items , meta ? ) { return { type : 'tuple' , version : 1 , serializable : true , items , meta } ; } export function makeTypedMessageSerializableTupleFromList < T extends readonly SerializableTypedMessages [ ] = readonly SerializableTypedMessages [ ] > ( ... args ) { return { type : 'tuple' , version : 1 , items : args , serializable : true } ; } export interface TypedMessageUnknown extends NonSerializableTypedMessage { readonly type : 'unknown' ; /** The unrecognized data */ readonly raw ? : unknown ; } export const isTypedMessageUnknown = createIsType < TypedMessageUnknown > ( 'unknown' ) ; export function makeTypedMessageUnknown ( raw ? , meta ? ) { return { type : 'unknown' , serializable : false , meta , raw } ; } export interface TypedMessageEmpty extends SerializableTypedMessage < 1 > { readonly type : 'empty' ; } export const isTypedMessageEmpty = createIsType < TypedMessageEmpty > ( 'empty' ) ; export function makeTypedMessageEmpty ( meta ? ) { return { type : 'empty' , serializable : true , version : 1 , meta } ; } export interface TypedMessagePromise < T extends TypedMessage = TypedMessage > extends NonSerializableTypedMessage { readonly type : 'promise' ; readonly promise : Promise < T > ; readonly value ? : T ; } export const isTypedMessagePromise = createIsType < TypedMessagePromise > ( 'promise' ) ; export function makeTypedMessagePromise < T extends TypedMessage = TypedMessage > ( promise ) { const x = { type : 'promise' , serializable : false , promise } ; promise . then ( ( y ) => ( ( x as any ) . value = y ) ) ; return x ; } export const isWellKnownCoreTypedMessages = composeSome ( isTypedMessageText , isTypedMessageImage , isTypedMessageTuple , isTypedMessageUnknown , isTypedMessagePromise ) as ( x : TypedMessage ) => x is WellKnownCoreTypedMessages ;