var Assistant = {}
Assistant.speak = function(text, chunkSize = 200) {
    if ('speechSynthesis' in window) {
      const speechSynthesis = window.speechSynthesis;
      const chunks = [];
      for (let i = 0; i < text.length; i += chunkSize) {
        chunks.push(text.slice(i, i + chunkSize));
      }
  
      function speakNextChunk(index) {
        if (index < chunks.length) {
            const utterance = new SpeechSynthesisUtterance(chunks[index]);
            utterance.onend = () => speakNextChunk(index + 1); // Speak the next chunk when the current one finishes.
            window.speechSynthesis.cancel()
            speechSynthesis.speak(utterance);
        }
      }
  
      speakNextChunk(0); // Start with the first chunk.
    } else {
      console.error('Speech synthesis is not supported in this browser.');
    }
  }
  

// var Assistant = function (){
//     this._message = null
//     this._message = new SpeechSynthesisUtterance()
//     // this._voices = window.speechSynthesis.getVoices();
//     // var r = []
//     // this._voices.forEach(function(_item){
//     //     if(_item.lang.match(/us|br|gb/i)){
//     //         r.push(_item)
//     //     }
//     // })
//     // this._voices = r
//     this.speak = function(text,times){
//         var _message = null;
//         if (window.speechSynthesis) {
//             var _message = new SpeechSynthesisUtterance()
//             var voices = window.speechSynthesis.getVoices();
//             var r = []
//             voices.forEach(function(_item){
//                 if(_item.lang.match(/gb/i)){
//                     r.push(_item)
//                 }
//             })
//             voices = r
//         }
//         if ( _message != null) {
            
//             _message.text = text
//             // _message.voice  = voices[0]
//             _message.volume = 1
//             _message.pitch = 1
//             window.speechSynthesis.speak(_message)     
//         }   
//     }
//     this._speak = function (text,times){
//         // this._message = new SpeechSynthesisUtterance()
//         // this._message.text = text
//         // this._message.voice = this._voices[0]
//         var _voices = [ ]
//         window.speechSynthesis.getVoices().forEach(_item=>{
//             if(_item.lang.match(/au|gb/i)){
//                 _voices.push(_item)
//             }
//         })
//         var _message = new SpeechSynthesisUtterance()
//         _message.text = text
//         _message.voiceURI = 'native'
//         // alert(_message.voiceURI)
//         // _message.volume = 1
//         window.speechSynthesis.cancel()
//         if (times != null){
//             times = parseInt(times)
//             // var _message = this._message
//             var _index = 0
//             var _iHandler = setInterval(function(){
//                 window.speechSynthesis.speak(_message)
//                 _index += 1
//                 if (_index == times){
//                     clearInterval(_iHandler)
//                 }
//             },2500,times)
//         }else{
//             window.speechSynthesis.speak(_message)
//         }

//     }

// }