import server from "web";
import type { alert, alertEventData } from "./types";

export async function sendAlertEvent(event: alertEventData) {
	server.publish("alerts", JSON.stringify(event));
}

export async function playAlert(alert: alert) {
	await sendAlertEvent({
		function: "playAlert",
		alert,
	});
}

type MSTTSOptions = {
	text: string;
	voice?: string;
	pitch?: number;
	speed?: number;
};

export async function playMSTTS(options: MSTTSOptions) {
	await sendAlertEvent({
		function: "playTTS",
		tts: {
			type: "microsoft",
			voice: options.voice ?? "Sam",
			pitch: options.pitch ? options.pitch.toString() : "100",
			speed: options.speed ? options.speed.toString() : "150",
			text: options.text,
		},
	});
}

export async function playDecTalk(input: string) {
	await sendAlertEvent({
		function: "playTTS",
		tts: {
			type: "dectalk",
			text: input,
		},
	});
}

export async function stopTTS() {
	await sendAlertEvent({
		function: "cancelTTS",
	});
}
