import type { TextToSpeech } from "./types.js"; /** Creates a non-blocking browser speech-synthesis adapter when supported. */ export function createTextToSpeech(): TextToSpeech { const isSupported = "speechSynthesis" in window && "SpeechSynthesisUtterance" in window; return { isSupported, cancel() { if (isSupported) window.speechSynthesis.cancel(); }, speak(text: string) { if (!isSupported || text.trim().length === 0) return; // Cancel the previous utterance so repeated controls cannot build an // inaccessible queue of stale answers. window.speechSynthesis.cancel(); const utterance = new SpeechSynthesisUtterance(text); utterance.lang = document.documentElement.lang || "it-IT"; window.speechSynthesis.speak(utterance); }, }; }