/* OK ITHAKI V249520 - FREE COMMUNITY CREATE ACTIONS / COMPACT HOME / GOLD WISDOM */
(function(){
'use strict';
if(window.__ITHAKI_V249520_FREE_COMMUNITY_CREATE__)return;
window.__ITHAKI_V249520_FREE_COMMUNITY_CREATE__={version:'V249520',freeSystemCommunityMemberCreate:true,compactHome25:true,goldWisdom:true,visibleAnnouncementEventPlus:true,at:Date.now()};
const I=()=>window.ITH||{},S=()=>window.state||I().state||{},$=id=>document.getElementById(id);
const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
function room(){return String(S().room||localStorage.getItem('ith_room')||'ithaki-phone-test')}
function sid(){return String(S().currentSpace||String(S().currentSpaceChannel||'').replace(/^space:/,''))}
function device(){return String(S().device||localStorage.getItem('ith_device')||'')}
function nickname(){return String(S().name||localStorage.getItem('ith_name')||localStorage.getItem('ith_nickname')||'Μέλος')}
function base(){return location.origin.replace(/\/$/,'')}
function headers(json=false){const h={};if(json)h['Content-Type']='application/json';try{const t=localStorage.getItem('ith_public_token')||localStorage.getItem('ith_token')||'';if(t)h['X-ITHAKI-TOKEN']=t}catch(e){}return h}
async function api(path,opt={}){const r=await fetch(base()+path,Object.assign({cache:'no-store'},opt,{headers:Object.assign(headers(!!opt.body),opt.headers||{})}));const txt=await r.text();let d={};try{d=txt?JSON.parse(txt):{}}catch(e){d={detail:txt}}if(!r.ok)throw Error(typeof d.detail==='string'?d.detail:(d.message||('HTTP '+r.status)));return d}
function toast(m,ms=6500){try{if(I().toast)return I().toast(m,ms)}catch(e){}const t=$('toast');if(t){t.textContent=m;t.classList.add('show');setTimeout(()=>t.classList.remove('show'),ms)}else alert(m)}
const details={};
function currentSpace(){return details[sid()]||(S().spaces||[]).find(x=>String(x.subroom_id||'')===sid())||{subroom_id:sid(),role:'member',access_type:'public',title:String($('spaceTitle')?.textContent||'Κοινότητα')}}
async function resolvedSpace(){const id=sid();if(!id)return currentSpace();try{const d=await api('/api/rooms/detail_v249516?room_id='+encodeURIComponent(room())+'&subroom_id='+encodeURIComponent(id)+'&device_id='+encodeURIComponent(device()));if(d?.room){details[id]=Object.assign({},currentSpace(),d.room);const list=S().spaces||[];const item=list.find(x=>String(x.subroom_id||'')===id);if(item)Object.assign(item,d.room);return details[id]}}catch(e){}return currentSpace()}
function role(x=currentSpace()){return String(x.role||((x.is_owner||String(x.owner_id||'')===device())?'creator':'member'))}
function isFreeSystem(x=currentSpace()){const id=String(x.subroom_id||sid()),owner=String(x.owner_id||'').toLowerCase(),access=String(x.access_type||'public').toLowerCase();return access==='public'&&(owner==='system'||owner==='ithaki-system'||id.startsWith('default_')||id.startsWith('default_v249513_'))}
function canCreate(x=currentSpace()){const r=role(x);return isFreeSystem(x)?r!=='visitor':['creator','moderator'].includes(r)}
function staffCode(x){const r=role(x),key=r==='moderator'?'ith_space_moderator_code_v249516_'+sid():'ith_space_creator_code_v249510_'+sid();let code='';try{code=localStorage.getItem(key)||''}catch(e){}if(!code&&!isFreeSystem(x)&&['creator','moderator'].includes(r)){code=prompt(r==='moderator'?'Κωδικός συντονιστή:':'Κωδικός δημιουργού:')||'';if(code)try{localStorage.setItem(key,code)}catch(e){}}return code}
function addCss(){if($('ith520Css'))return;const st=document.createElement('style');st.id='ith520Css';st.textContent=`
.ith520PlusMark{position:absolute;right:6px;top:5px;width:20px;height:20px;border-radius:50%;display:grid;place-items:center;background:#54f0a0;color:#03140d;border:2px solid #07131f;font-size:15px;font-weight:1000;line-height:1;box-shadow:0 4px 12px rgba(0,0,0,.28)}
.ith520Policy{padding:9px 11px;margin-bottom:9px;border-radius:15px;background:rgba(54,240,160,.08);border:1px solid rgba(54,240,160,.24);color:#dfffee;font-size:12px;line-height:1.4}
.ith520Create{width:100%;min-height:55px;margin-bottom:10px;border-radius:17px;border:1px solid rgba(54,240,160,.52);background:linear-gradient(135deg,rgba(54,240,160,.18),rgba(255,217,110,.10));color:#fff;font-size:15px;font-weight:1000;touch-action:manipulation}
.ith520EventCard{padding:12px;border-radius:17px;background:rgba(255,255,255,.055);border:1px solid rgba(255,255,255,.11);margin-bottom:9px;color:#fff}.ith520EventCard small{display:block;margin-top:7px;color:rgba(230,243,255,.72)}.ith520EventActions{display:flex;gap:7px;margin-top:9px}.ith520EventActions button{flex:1;min-height:42px;border-radius:14px;border:1px solid rgba(255,255,255,.14);background:rgba(255,255,255,.07);color:#fff}
body[data-page="home"] .ithWisdomV249461 b,body[data-page="home"] .ithWisdomV249461 p,.hourlyWisdomCard.greekWisdomCard .proPanelText b,.hourlyWisdomCard.greekWisdomCard .proPanelText p,.wisdomBoard b,.wisdomBoard p,.feedGoldWisdomCard .feedGoldQuote,.feedGoldWisdomCard .feedGoldQuote small{color:#ffd86e!important;-webkit-text-fill-color:#ffd86e!important;text-shadow:0 3px 16px rgba(0,0,0,.45)!important}
@media(max-width:760px){body[data-page="home"] .ithActionsV249461{grid-auto-rows:minmax(84px,auto)!important;min-height:auto!important;gap:7px!important;padding:1px 1px calc(82px + env(safe-area-inset-bottom))!important}body[data-page="home"] .ithActionV249461{min-height:84px!important;padding:6px 4px!important;gap:4px!important;border-radius:15px!important}body[data-page="home"] .ithActionIconV249461{width:44px!important;height:44px!important;border-radius:14px!important;font-size:23px!important}body[data-page="home"] .ithActionV249461 b{font-size:10px!important;line-height:1.08!important}body[data-page="home"] .ithActionV249461 small{font-size:7.5px!important;line-height:1.08!important;max-width:105px!important}body[data-page="home"] .ithHomeV249461{gap:6px!important}body[data-page="home"] .ithWisdomV249461{padding:8px 10px!important;border-radius:15px!important}body[data-page="home"] .ithWisdomV249461 p{font-size:12px!important;line-height:1.3!important}}
`;document.head.appendChild(st)}
function panel(title,html){$('ith520Panel')?.remove();const d=document.createElement('div');d.id='ith520Panel';d.className='ith519Panel';d.innerHTML=`<div class="ith519PanelHead"><button data-ith520-close>‹</button><b>${esc(title)}</b><button data-ith520-close>×</button></div><div class="ith519PanelBody">${html}</div>`;document.body.appendChild(d);d.querySelectorAll('[data-ith520-close]').forEach(b=>b.onclick=()=>d.remove());return d}
function policyHtml(x){return `<div class="ith520Policy">${isFreeSystem(x)?'Ελεύθερη κοινότητα: κάθε μέλος μπορεί να δημιουργεί ψηφοφορία, ανακοίνωση και εκδήλωση. Δεν χρειάζεται συντονιστής.':'Νέα κοινότητα μέλους: ανακοινώσεις και εκδηλώσεις δημιουργούνται από τον δημιουργό ή τον συντονιστή. Οι ψηφοφορίες παραμένουν διαθέσιμες σε όλα τα μέλη.'}</div>`}
function parseBody(text){try{const o=JSON.parse(String(text||''));return String(o.body??o.text??text)}catch(e){return String(text||'')}}
async function openAnnouncements(){const x=await resolvedSpace();let d;try{d=await api('/api/rooms/pins?room_id='+encodeURIComponent(room())+'&subroom_id='+encodeURIComponent(sid()))}catch(e){return toast('Δεν φορτώθηκαν οι ανακοινώσεις: '+e.message)}let html=policyHtml(x);if(canCreate(x))html+='<button class="ith520Create" id="ith520NewAnnouncement">＋ ΝΕΑ ΑΝΑΚΟΙΝΩΣΗ</button>';html+=(d.pins||[]).map(p=>`<div class="ith519Card announcement"><b>📣 ${esc(p.note||'Ανακοίνωση')}</b><div style="white-space:pre-wrap;margin-top:7px">${esc(parseBody(p.text||''))}</div><small>${esc(p.nickname||'')} · ${new Date(Number(p.message_created_at||p.created_at||0)*1000).toLocaleString('el-GR')}</small></div>`).join('')||'<div class="ith519Card">Δεν υπάρχουν ακόμη ανακοινώσεις.</div>';const p=panel('📣 Ανακοινώσεις κοινότητας',html);p.querySelector('#ith520NewAnnouncement')?.addEventListener('click',()=>createAnnouncement(x))}
async function createAnnouncement(x){if(!canCreate(x))return toast('Στη νέα κοινότητα η ανακοίνωση γίνεται από δημιουργό ή συντονιστή.');const title=prompt('Τίτλος ανακοίνωσης:')||'';if(!title)return;const body=prompt('Κείμενο ανακοίνωσης:')||'';if(!body)return;const code=isFreeSystem(x)?'':staffCode(x);if(!isFreeSystem(x)&&!code)return;try{await api('/api/rooms/announcements/create_v249520',{method:'POST',body:JSON.stringify({room_id:room(),subroom_id:sid(),device_id:device(),nickname:nickname(),staff_code:code,title,body})});toast('Η ανακοίνωση δημοσιεύτηκε και καρφιτσώθηκε.');$('ith520Panel')?.remove();const f=I().loadSpaceMessagesV249518||I().loadSpaceMessages;if(typeof f==='function')await f();openAnnouncements()}catch(e){toast('Δεν δημοσιεύτηκε: '+e.message,8000)}}
async function openEvents(){const x=await resolvedSpace();let d;try{d=await api('/api/rooms/events_v249516?room_id='+encodeURIComponent(room())+'&subroom_id='+encodeURIComponent(sid())+'&device_id='+encodeURIComponent(device()))}catch(e){return toast('Δεν φορτώθηκαν οι εκδηλώσεις: '+e.message)}let html=policyHtml(x);if(canCreate(x))html+='<button class="ith520Create" id="ith520NewEvent">＋ ΝΕΑ ΕΚΔΗΛΩΣΗ</button>';html+=(d.events||[]).map(ev=>`<div class="ith520EventCard"><b>📅 ${esc(ev.title)}</b><div style="white-space:pre-wrap;margin-top:6px">${esc(ev.description||'')}</div><small>${new Date(Number(ev.starts_at||0)*1000).toLocaleString('el-GR')} · ${esc(ev.location||'')} · 👥 ${Number(ev.going_count||0)}</small><div class="ith520EventActions"><button data-ith520-event="${esc(ev.event_id)}" data-rsvp="going">Θα πάω${ev.my_response==='going'?' ✓':''}</button><button data-ith520-event="${esc(ev.event_id)}" data-rsvp="interested">Ενδιαφέρομαι${ev.my_response==='interested'?' ✓':''}</button></div></div>`).join('')||'<div class="ith519Card">Δεν υπάρχουν ακόμη εκδηλώσεις.</div>';const p=panel('📅 Εκδηλώσεις κοινότητας',html);p.querySelector('#ith520NewEvent')?.addEventListener('click',()=>createEvent(x));p.querySelectorAll('[data-ith520-event][data-rsvp]').forEach(b=>b.onclick=async()=>{try{await api('/api/rooms/events/rsvp_v249516',{method:'POST',body:JSON.stringify({room_id:room(),subroom_id:sid(),device_id:device(),nickname:nickname(),event_id:b.dataset.ith520Event,response:b.dataset.rsvp})});openEvents()}catch(e){toast(e.message)}})}
async function createEvent(x){if(!canCreate(x))return toast('Στη νέα κοινότητα η εκδήλωση γίνεται από δημιουργό ή συντονιστή.');const title=prompt('Τίτλος εκδήλωσης:')||'';if(!title)return;const when=prompt('Ημερομηνία και ώρα, π.χ. 2026-08-15 20:00:')||'';const ms=Date.parse(when.replace(' ','T'));if(!Number.isFinite(ms)||ms<=Date.now())return toast('Βάλε σωστή μελλοντική ημερομηνία και ώρα.');const locationText=prompt('Τοποθεσία ή σύνδεσμος:')||'';const description=prompt('Περιγραφή:')||'';const code=isFreeSystem(x)?'':staffCode(x);if(!isFreeSystem(x)&&!code)return;try{await api('/api/rooms/events/create_v249520',{method:'POST',body:JSON.stringify({room_id:room(),subroom_id:sid(),device_id:device(),nickname:nickname(),staff_code:code,title,description,location:locationText,starts_at:ms/1000})});toast('Η εκδήλωση δημιουργήθηκε.');openEvents()}catch(e){toast('Δεν δημιουργήθηκε: '+e.message,8000)}}
function decorateToolbar(){const bar=$('ith519CommunityCommandBar');if(!bar)return;const x=currentSpace();[['polls',true],['announcements',canCreate(x)],['events',canCreate(x)]].forEach(([cmd,show])=>{const b=bar.querySelector('[data-ith519-cmd="'+cmd+'"]');if(!b)return;let m=b.querySelector('.ith520PlusMark');if(show&&!m){m=document.createElement('i');m.className='ith520PlusMark';m.textContent='+';b.appendChild(m)}else if(!show&&m)m.remove()});if(isFreeSystem(x))bar.querySelector('[data-ith519-cmd="manage"]')?.remove()}
function capture(e){if(window.__ITHAKI_V249521_SPECIAL_OWNER__)return;const b=e.target?.closest?.('#ith519CommunityCommandBar [data-ith519-cmd]');if(!b)return;const c=b.dataset.ith519Cmd;if(c!=='announcements'&&c!=='events')return;e.preventDefault();e.stopPropagation();e.stopImmediatePropagation();if(c==='announcements')openAnnouncements();else openEvents()}
function refresh(){addCss();decorateToolbar()}
function install(){addCss();try{const u=new URL(location.href);if(u.searchParams.get('v')!=='249524'){u.searchParams.set('v',(window.ITHAKI_ACTIVE_VERSION||'249526r10f37f1f2'));history.replaceState(null,'',u.pathname+'?'+u.searchParams.toString()+u.hash)}}catch(e){}document.addEventListener('click',capture,true);Object.assign(I(),{openCommunityAnnouncementsV249520:openAnnouncements,openCommunityEventsV249520:openEvents,openCommunityEventsV249516:openEvents,refreshCommunityFreePolicyV249520:refresh});new MutationObserver(()=>{clearTimeout(install._m);install._m=setTimeout(refresh,60)}).observe(document.body,{subtree:true,childList:true});setInterval(refresh,1200);refresh();try{localStorage.setItem('ith_frontend_build','V249520-free-community-create-compact-home-gold-wisdom');document.documentElement.setAttribute('data-ithaki-v249520','free-community-create-compact-home-gold-wisdom')}catch(e){}}
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',install,{once:true});else install();
})();
