import type { BlipClient, Ticket } from '../index.ts' import { PluginSender } from '../sender/plugin/pluginsender.ts' import type { Account, BlipLanguage } from '../types/account.ts' import { Namespace, type SendCommandOptions } from './namespace.ts' export class ContextNamespace extends Namespace { constructor(blipClient: BlipClient, defaultOptions?: SendCommandOptions) { super(blipClient, '', defaultOptions) } public async getCurrentTicket(): Promise { const sender = this.checkSenderRealm('desk') const { item } = await sender.channel.post<{ item: Ticket }>('currentTicket', null) return item } public getUser(): Promise { const sender = this.checkSenderRealm('portal') return sender.channel.post('getUserContext', { command: 'getCurrentUser' }) } public getLanguage(): Promise { const sender = this.checkSenderRealm() return sender.channel.post('getCurrentLanguage', null) } public toast(type: 'success' | 'warning' | 'danger', message: string): void { const sender = this.checkSenderRealm() const fixedType = this.isOnBlipDesk() && type === 'danger' ? 'error' : type sender.channel.post('toast', { type: fixedType, message }, { fireAndForget: true }) } public changeHeight(height: number): void { const sender = this.checkSenderRealm('portal') sender.channel.post('heightChange', height, { fireAndForget: true }) } public loading(show: boolean): void { const sender = this.checkSenderRealm() if (show) { sender.channel.post('startLoading', {}, { fireAndForget: true }) } else { sender.channel.post('stopLoading', {}, { fireAndForget: true }) } } public getUserToken(): Promise { const sender = this.checkSenderRealm('portal') return sender.channel.post('getToken', null) } public isOnBlipDesk(): boolean { return PluginSender.getRealm(this.blipClient) === 'desk' } public isOnPortal(): boolean { return PluginSender.getRealm(this.blipClient) === 'portal' } private checkSenderRealm(realm?: 'portal' | 'desk') { if (realm && PluginSender.getRealm(this.blipClient) !== realm) { throw new Error(`This command is only allowed for ${realm} realm`) } return PluginSender.check(this.blipClient) } }