import { Translator } from "../i18n/api"; export type Speaker = { id: string, language : string, } export class Message { public translation? : string; constructor (public text : string, public speaker : Speaker) {} public async translate(translator : Translator, language? : string) { this.translation = await translator.translate(this.text, language); } } export class Conversation extends Array { constructor ( private translator : Translator, public s1 : Speaker, public s2 : Speaker, ) { super(); } public addMessage(speaker : Speaker, text : string) { this.push(new Message(text, speaker)); } public async translateMessage(i : number) { if (!this[i]) throw new Error(`${i} is not a valid message number`); const otherSpeaker = this[i].speaker.id === this.s1.id ? this.s2 : this.s1 await this[i].translate(this.translator, otherSpeaker.language); } public async translateLast() { return await this.translateMessage(this.length-1); } }