/* OK ITHAKI V249526R10F37 — BIL SPEAK EACH MESSAGE ONCE */
(function(){
'use strict';
if(window.__ITHAKI_R10F40_NOTIFICATION_EXCLUSIVE__) return;
if(window.__ITHAKI_R10F37_BIL_ONCE__) return;
window.__ITHAKI_R10F37_BIL_ONCE__=1;
var STORE='ithaki_bil_spoken_message_ids_r10f37';
var MAX=500;
var memory=new Set();
function load(){try{var a=JSON.parse(localStorage.getItem(STORE)||'[]');if(Array.isArray(a))a.forEach(function(x){memory.add(String(x))})}catch(e){}}
function save(){try{localStorage.setItem(STORE,JSON.stringify(Array.from(memory).slice(-MAX)))}catch(e){}}
function norm(v){return String(v==null?'':v).trim()}
function idOf(m){if(!m||typeof m!=='object')return '';var id=m.message_id||m.messageId||m.id||m.uuid||m.client_message_id||m.server_id;if(id)return norm(id);var who=m.sender_id||m.from_device||m.from||m.sender||'';var at=m.created_at||m.timestamp||m.time||m.sent_at||'';var text=m.text||m.message||m.body||'';if(!text)return '';return 'fallback:'+norm(who)+'|'+norm(at)+'|'+norm(text).slice(0,180)}
function isIncoming(m){var st=window.state||(window.ITH&&window.ITH.state)||{};var me=norm(st.deviceId||st.device||localStorage.getItem('ith_device_id')||localStorage.getItem('device_id'));var from=norm(m&&(m.sender_id||m.from_device||m.device_id||m.from));return !me||!from||from!==me}
function claim(m){var id=idOf(m);if(!id||!isIncoming(m)||memory.has(id))return false;memory.add(id);if(memory.size>MAX)memory=new Set(Array.from(memory).slice(-MAX));save();return true}
function prime(list){if(!Array.isArray(list))return;list.forEach(function(m){var id=idOf(m);if(id)memory.add(id)});if(memory.size>MAX)memory=new Set(Array.from(memory).slice(-MAX));save()}
function speakMessageOnce(m,text){if(!claim(m))return false;var t=norm(text||(m&&(m.text||m.message||m.body)));if(!t)return false;try{if(window.ITH&&typeof window.ITH.showBil==='function'){window.ITH.showBil(t);return true}if(typeof window.showBil==='function'){window.showBil(t);return true}if('speechSynthesis' in window&&typeof SpeechSynthesisUtterance!=='undefined'){var u=new SpeechSynthesisUtterance(t);u.lang=(document.documentElement.lang||'el').toLowerCase().startsWith('en')?'en-US':'el-GR';u.rate=.94;window.speechSynthesis.speak(u);return true}}catch(e){}return false}
load();
window.ITHAKI_BIL_ONCE={claim:claim,prime:prime,speakMessageOnce:speakMessageOnce,idOf:idOf,store:STORE};
['handleIncomingMessage','onIncomingMessage','receiveMessage','processIncomingMessage','notifyIncomingMessage'].forEach(function(n){var f=window[n];if(typeof f==='function'&&!f.__bilOnce){var w=function(m){var r=f.apply(this,arguments);try{speakMessageOnce(m)}catch(e){}return r};w.__bilOnce=1;window[n]=w}});
['ithaki:new-message','ithaki:message','new-message','message-received'].forEach(function(n){document.addEventListener(n,function(e){try{var m=e.detail&&((e.detail.message)||e.detail);speakMessageOnce(m)}catch(_){}},true)});
console.log('ITHAKI_R10F37_BIL_SPEAK_ONCE_READY');
})();
