export const speak = (text: string, lang = 'no-NO', rate = 0.9) => {
    if (typeof window === 'undefined') return

    // Cancel current speech to avoid queue buildup
    window.speechSynthesis.cancel()

    const utterance = new SpeechSynthesisUtterance(text)
    utterance.lang = lang
    utterance.rate = rate

    // Try to find a good voice
    const voices = window.speechSynthesis.getVoices()
    // Prioritize Norwegian voices
    const norwegianVoice = voices.find((v) => v.lang.includes('no') || v.lang.includes('nb') || v.lang.includes('nn'))
    if (norwegianVoice) utterance.voice = norwegianVoice

    window.speechSynthesis.speak(utterance)
}
