/* V249404-preview-home-members-recovery */
(function(){'use strict';
const MARK='V249404-preview-home-members-recovery',$=id=>document.getElementById(id),enc=encodeURIComponent;
const esc=s=>String(s??'').replace(/[&<>\"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','\"':'&quot;',"'":'&#39;'}[c]));
const mb=n=>{n=Number(n||0);return n>=1048576?(n/1048576).toFixed(1)+' MB':Math.max(1,Math.round(n/1024))+' KB'};
const S=()=>{try{return (window.ITH&&window.ITH.state)||{room:localStorage.getItem('ith_room')||'ithaki-phone-test',device:localStorage.getItem('ith_device')||'',name:localStorage.getItem('ith_name')||'Μέλος'}}catch(e){return {room:'ithaki-phone-test',device:'',name:'Μέλος'}}};
const base=()=>{try{return (window.ITH&&window.ITH.state&&window.ITH.state.api)||location.origin}catch(e){return location.origin}}, url=p=>String(base()).replace(/\/$/,'')+p;
function ensureCss(){if($('ithV249404Css'))return;const st=document.createElement('style');st.id='ithV249404Css';st.textContent=`#previewModal.show{position:fixed!important;inset:0!important;z-index:2147483647!important;display:flex!important;align-items:flex-start!important;justify-content:center!important;overflow:auto!important;background:rgba(1,4,9,.76)!important;padding:18px!important;pointer-events:auto!important}#previewModal.show .previewBox.previewSafeV249404{width:min(760px,96vw)!important;max-height:none!important;margin:auto!important;background:rgba(8,13,22,.96)!important;border:1px solid rgba(255,213,111,.28)!important;border-radius:24px!important;box-shadow:0 28px 90px rgba(0,0,0,.65)!important;padding:14px!important;overflow:visible!important}.previewSafeV249404 .v249404SafeStage{min-height:220px!important;display:flex!important;align-items:center!important;justify-content:center!important;border-radius:20px!important;background:linear-gradient(135deg,rgba(255,213,111,.13),rgba(255,255,255,.05))!important;border:1px solid rgba(255,213,111,.16)!important;text-align:center!important}.previewLiteV249404 .bigIcon{font-size:64px!important;margin-bottom:10px!important}.previewLiteV249404 b{display:block!important;color:#ffe49b!important;overflow-wrap:anywhere!important}.previewLiteV249404 small,.previewLiteV249404 .date{display:block!important;margin-top:8px!important;color:#d7dde8!important;opacity:.88!important}.homeStoriesV249404Fallback{border:1px solid rgba(255,255,255,.08)!important;background:rgba(255,255,255,.05)!important;border-radius:22px!important;margin:8px 0 12px!important;padding:8px!important;overflow:hidden!important}.homeStoriesV249404Fallback .storyScroll{display:flex!important;gap:10px!important;overflow-x:auto!important;padding:4px!important}.homeStoriesV249404Fallback .storyItem{min-width:72px!important;max-width:86px!important;background:transparent!important;border:0!important;color:#f5f7fa!important;text-align:center!important}.homeStoriesV249404Fallback .avatar{width:52px!important;height:52px!important;border-radius:50%!important;display:flex!important;align-items:center!important;justify-content:center!important;background:rgba(255,255,255,.1)!important;overflow:hidden!important;margin:auto!important}.homeStoriesV249404Fallback img{width:100%!important;height:100%!important;object-fit:cover!important}.homeStoriesV249404Fallback small{display:block!important;margin-top:5px!important;white-space:nowrap!important;overflow:hidden!important;text-overflow:ellipsis!important;font-size:11px!important}.homeMemberStatus404{font-size:12px!important;color:#ffe49b!important;padding:4px 8px!important}`;document.head.appendChild(st)}
function kindOf(file,kind){const t=String(file&&file.type||'').toLowerCase(),n=String(file&&file.name||'').toLowerCase(),k=String(kind||'').toLowerCase();if(k==='video'||t.startsWith('video/')||/\.(mp4|mov|m4v|webm|3gp|3gpp|avi|mkv)$/i.test(n))return 'video';if(k==='photo'||t.startsWith('image/'))return 'photo';if(k==='audio'||t.startsWith('audio/'))return 'audio';return 'file'}
function getOwnInput(){let inp=$('ithFileInputV249404');if(!inp){inp=document.createElement('input');inp.id='ithFileInputV249404';inp.type='file';inp.style.cssText='position:fixed;left:-10000px;top:-10000px;width:1px;height:1px;opacity:0;pointer-events:none;';document.body.appendChild(inp)}return inp}
function openPreview404(ctx,file,kind,target){ensureCss();if(!file)return false;const s=S();kind=kindOf(file,kind);try{if(s.preview&&s.preview.url)URL.revokeObjectURL(s.preview.url)}catch(e){}s.preview={ctx:ctx||'feed',file,kind,target:target||'',originalFile:file,studio:null,url:'',desktopSafeV249404:true};try{s.sending=false}catch(e){}const m=$('previewModal');if(!m){alert('Δεν βρέθηκε το previewModal.');return false}const icon=kind==='video'?'🎬':kind==='photo'?'🖼️':kind==='audio'?'🎙️':'📎',dest=(ctx==='dm')?'στα Μηνύματα':(ctx==='space')?'στην Κοινότητα':(ctx==='comment'||ctx==='comment_frame')?'στα σχόλια':'στη Ροή';m.classList.remove('callCompactV249218','voiceCallV249218','videoCallV249218');m.innerHTML='<div class="previewBox previewScrollable previewSafeV249404"><div class="row"><button class="icon" type="button" onclick="ITH.closePreview()">‹</button><b>Προεπισκόπηση '+esc(dest)+'</b><button class="btn" type="button" onclick="ITH.closePreview()">Άκυρο</button></div><div id="previewStage" class="previewStage v249404SafeStage"><div class="previewLiteV249404"><div class="bigIcon">'+icon+'</div><b>'+esc(file.name||'file')+'</b><small>'+esc(file.type||kind)+' · '+mb(file.size)+'</small><div class="date">V249404: καθαρή προεπισκόπηση χωρίς Studio/Canvas και χωρίς αλλαγή σε Αρχική.</div></div></div><div class="card previewFileCard"><b id="previewFileName">'+esc(file.name||'file')+'</b><div id="previewFileMeta" class="date">'+esc(file.type||kind)+' · '+mb(file.size)+'</div></div><div id="previewStudioMount"></div><textarea id="previewCaption" rows="2" style="width:100%;min-height:72px;resize:vertical" placeholder="Λεζάντα..."></textarea><input id="previewKeywords" class="previewKeywordsV249287" placeholder="Λέξεις κλειδιά"><div class="previewHintV249287">Το αρχείο θα ανέβει μόνο όταν πατήσεις Αποστολή.</div>'+(ctx==='feed'?'<label class="card row anonymousRow"><input id="previewAnonymous" type="checkbox"><span class="anonBadge">🕶️ Ανώνυμη δημοσίευση</span></label>':'')+'<div class="progress"><span id="progressBar"></span></div><div id="progressText" class="date">Έτοιμο · '+mb(file.size)+'</div><div class="row"><button id="previewSend" class="btn green" type="button" onclick="ITH.sendPreview()">Αποστολή / Δημοσίευση</button><button class="btn" type="button" onclick="ITH.closePreview()">Άκυρο</button></div></div>';m.classList.add('show');document.body.classList.add('ithPreviewOpenV249404');try{document.documentElement.setAttribute('data-ithaki-preview-v249404',MARK)}catch(e){}return false}
function closePreview404(){try{const s=S();if(s.preview&&s.preview.url)URL.revokeObjectURL(s.preview.url);s.preview={}}catch(e){}const m=$('previewModal');if(m){m.classList.remove('show');m.innerHTML=''}document.body.classList.remove('ithPreviewOpenV249404');return false}
function chooseFile404(ctx,kind,target){ensureCss();const inp=getOwnInput();inp.value='';inp.removeAttribute('capture');inp.accept=kind==='video'?'video/*':kind==='photo'?'image/*':'*/*';inp.onchange=function(ev){try{ev&&ev.stopPropagation&&ev.stopPropagation()}catch(e){}const f=inp.files&&inp.files[0];if(!f)return false;return openPreview404(ctx||'feed',f,kind,target||'')};setTimeout(()=>inp.click(),0);return false}
function captureMedia404(ctx,kind,target){ensureCss();const inp=getOwnInput();inp.value='';inp.accept=kind==='video'?'video/*':'image/*';inp.setAttribute('capture','environment');inp.onchange=function(ev){try{ev&&ev.stopPropagation&&ev.stopPropagation()}catch(e){}inp.removeAttribute('capture');const f=inp.files&&inp.files[0];if(!f)return false;return openPreview404(ctx||'feed',f,kind,target||'')};setTimeout(()=>inp.click(),0);return false}
async function fetchJson404(path){const r=await fetch(url(path),{cache:'no-store',credentials:'same-origin'});if(!r.ok)throw new Error('HTTP '+r.status);return await r.json()}
function normalizePerson(p){const id=String(p&&(p.device_id||p.id||p.sender_id)||'').trim();if(!id||id===String(S().device||''))return null;return {id,device_id:id,nickname:String(p.nickname||p.name||'Μέλος'),avatar:String(p.avatar||p.photo||''),online:!!p.online,last_seen:Number(p.last_seen||p.created_at||0)}}
function mergeIntoState(list){const s=S();try{s.people=s.people||new Map();(list||[]).forEach(x=>{const p=normalizePerson(x);if(!p)return;const old=s.people.get(p.id)||{};s.people.set(p.id,Object.assign({},old,p,{avatar:p.avatar||old.avatar||'',online:p.online||old.online}))})}catch(e){}}
function storyAvatar(p){return p.avatar?'<span class="avatar"><img src="'+esc(p.avatar)+'" onerror="this.style.display=\'none\'"></span>':'<span class="avatar">👤</span>'}
function renderHomeMembers404(list,status){ensureCss();const home=$('homePage');if(!home)return;let box=$('homeStoriesV249404');if(!box){box=document.createElement('section');box.id='homeStoriesV249404';box.className='homeStoriesV249404Fallback';home.insertBefore(box,home.firstChild)}const people=(list||[]).filter(p=>p&&p.id&&p.id!==String(S().device||'')).slice(0,80);const ith='<button class="storyItem on" type="button" onclick="return ITH.page&&ITH.page(\'feed\')"><span class="avatar"><img src="ok_ithaki_ship_logo.png" onerror="this.style.display=\'none\'"></span><small>ΙΘΑΚΗ</small></button>';box.innerHTML='<div class="homeMemberStatus404">Μέλη από server · '+esc(status||String(people.length)+' μέλη')+'</div><div class="storyScroll">'+ith+people.map(p=>'<button class="storyItem '+(p.online?'on':'')+'" type="button" onclick="return ITH.member&&ITH.member(\''+esc(p.id)+'\')">'+storyAvatar(p)+'<small>'+esc(p.nickname||'Μέλος')+'</small></button>').join('')+'</div>'}
async function loadHomeMembers404(){const s=S();let list=[];try{const k=await fetchJson404('/api/people/known?room_id='+enc(s.room||'ithaki-phone-test')+'&limit=800&_v='+Date.now());list=list.concat((k.people||[]).map(normalizePerson).filter(Boolean))}catch(e){}try{const o=await fetchJson404('/api/people/online?room_id='+enc(s.room||'ithaki-phone-test')+'&_v='+Date.now());list=list.concat((o.people||[]).map(x=>normalizePerson(Object.assign({},x,{online:true}))).filter(Boolean))}catch(e){}try{const f=await fetchJson404('/api/flow?room_id='+enc(s.room||'ithaki-phone-test')+'&visibility=public&limit=200&lite=1&viewer_id='+enc(s.device||'')+'&_v='+Date.now());list=list.concat((f.posts||[]).map(p=>normalizePerson({device_id:p.sender_id,nickname:p.nickname,avatar:p.avatar,last_seen:p.created_at})).filter(Boolean))}catch(e){}const map=new Map();list.forEach(p=>{if(!p||!p.id)return;map.set(p.id,Object.assign({},map.get(p.id)||{},p,{avatar:p.avatar||(map.get(p.id)||{}).avatar||'',online:p.online||(map.get(p.id)||{}).online||false}))});list=[...map.values()].sort((a,b)=>(b.online-a.online)||String(a.nickname||'').localeCompare(String(b.nickname||''),'el'));mergeIntoState(list);if((s.page||'home')==='home'||document.body.getAttribute('data-page')==='home')renderHomeMembers404(list,String(list.length)+' μέλη');try{document.documentElement.setAttribute('data-ithaki-home-members-v249404',String(list.length))}catch(e){}return list}
async function syncFriends404(){const s=S();if(!s.device)return [];try{const r=await fetchJson404('/api/social/member_lists/v249404?room_id='+enc(s.room||'ithaki-phone-test')+'&device_id='+enc(s.device)+'&_v='+Date.now());const friends=Array.isArray(r.friends)?r.friends:[];try{s.socialFriendsV249225=friends}catch(e){}try{localStorage.setItem('ith_contacts_server_only_v249404','1');localStorage.setItem('ith_contacts',JSON.stringify([...new Set(friends.map(p=>String(p.device_id||p.id||'')).filter(Boolean))]))}catch(e){}return friends}catch(e){try{s.socialFriendsV249225=[]}catch(_){}return []}}
function safeStudio404(){const m=$('previewStudioMount');if(m)m.innerHTML='<div class="card"><b>Ασφαλής λειτουργία V249404</b><div class="date">Το βαρύ Studio/Canvas είναι κλειστό για να μη κολλάει ο υπολογιστής.</div></div>';return false}
function install(){ensureCss();window.ITH=window.ITH||{};Object.assign(window.ITH,{openPreview:openPreview404,closePreview:closePreview404,chooseFile:chooseFile404,captureMedia:captureMedia404,openPreviewStudio:safeStudio404,loadHomeMembersV249404:loadHomeMembers404,refreshFriendsForMessagesV249246:syncFriends404});try{window.ITH.state=S()}catch(e){}try{document.documentElement.setAttribute('data-ithaki-v249404',MARK)}catch(e){}}
install();document.addEventListener('DOMContentLoaded',()=>{install();loadHomeMembers404();syncFriends404()},{once:true});setTimeout(()=>{install();loadHomeMembers404();syncFriends404()},250);setTimeout(loadHomeMembers404,1500);setInterval(loadHomeMembers404,25000);window.__ITHAKI_V249404_PREVIEW_HOME_MEMBERS_RECOVERY__={install,chooseFile404,openPreview404,loadHomeMembers404,syncFriends404};
})();
