/* OK ITHAKI V249395 — unified emoji, stickers and attachments for comments/messages. */
(function(){
'use strict';
const BUILD='V249395_COMMENT_MESSAGE_TOOLS';
const EMOJI=['😀','😂','😊','😍','🥰','😢','😮','😡','👍','👎','👏','🙏','❤️','💛','💙','🔥','🌹','🎉','✅','🤝'];
const STICKERS=['🌟 Μπράβο','❤️ Με αγάπη','👏 Συγχαρητήρια','🙏 Ευχαριστώ','🎉 Χαρά','🌹 Για σένα','🔥 Δυνατό','🤝 Μαζί','⛵ ΙΘΑΚΗ'];
const COMM_IDS=new Set(['dmInput','spaceInput','commentInput','oneTimeMsgV249288','bcastTextV249280','forwardTextV249280','bottleTextV249274','bottleReplyTextV249274','bottleThreadTextV249274','zeroSupportTextV249270','hostMsg','emergencyDirectTextV249283','wallText357']);
let palette=null;
function byId(id){return document.getElementById(id)}
function note(msg){try{if(window.toast)window.toast(msg,4200);else if(window.ITH&&ITH.toast)ITH.toast(msg,4200)}catch(e){}}
function injectCss(){
  if(byId('ithComposerToolsCss395'))return;
  const st=document.createElement('style');st.id='ithComposerToolsCss395';st.textContent=`
  .ithComposerTools395{display:flex;align-items:center;gap:6px;flex:0 0 100%;width:100%;padding:3px 2px 5px;order:-1;overflow-x:auto;scrollbar-width:none}
  .ithComposerTools395::-webkit-scrollbar{display:none}.ithComposerTools395 button{width:34px;height:34px;min-width:34px;border-radius:50%;border:1px solid rgba(255,221,135,.30);background:rgba(255,255,255,.07);color:#fff;display:grid;place-items:center;font-size:17px;padding:0;cursor:pointer}
  .composer.ithToolsReady395,.commentComposer.ithToolsReady395{flex-wrap:wrap!important}.commentComposer.ithToolsReady395 textarea,.composer.ithToolsReady395 textarea{min-width:0;flex:1 1 180px}
  .ithPalette395{position:fixed;z-index:2147483000;left:50%;bottom:max(82px,calc(env(safe-area-inset-bottom,0px) + 74px));transform:translateX(-50%);width:min(520px,calc(100vw - 18px));max-height:44vh;overflow:auto;padding:10px;border-radius:20px;border:1px solid rgba(255,220,128,.35);background:rgba(4,12,22,.98);box-shadow:0 24px 80px rgba(0,0,0,.72);display:grid;grid-template-columns:repeat(auto-fit,minmax(44px,1fr));gap:7px}
  .ithPalette395 button{min-height:43px;border-radius:13px;border:1px solid rgba(255,255,255,.12);background:rgba(255,255,255,.07);color:#fff;font-size:20px;padding:6px;cursor:pointer}.ithPalette395.stickers button{grid-column:span 2;font-size:13px;font-weight:850;white-space:normal}
  .ithPalette395 .ithClosePalette395{position:sticky;top:0;grid-column:1/-1;height:34px!important;min-height:34px!important;border-color:rgba(255,220,128,.35)!important;color:#ffe3a0!important}
  `;document.head.appendChild(st)
}
function closePalette(){if(palette){try{palette.remove()}catch(e){}palette=null}}
function insertAtCursor(el,text){if(!el)return;const start=Number.isFinite(el.selectionStart)?el.selectionStart:el.value.length;const end=Number.isFinite(el.selectionEnd)?el.selectionEnd:start;const before=String(el.value||'').slice(0,start),after=String(el.value||'').slice(end);const spacer=(before&&!/\s$/.test(before)&&!/^\s/.test(text))?' ':'';el.value=before+spacer+text+after;const pos=(before+spacer+text).length;try{el.focus();el.setSelectionRange(pos,pos);el.dispatchEvent(new Event('input',{bubbles:true}))}catch(e){}}
function openPalette(textareaId,kind){
  injectCss();closePalette();const el=byId(textareaId);if(!el)return false;
  palette=document.createElement('div');palette.className='ithPalette395 '+(kind==='stickers'?'stickers':'');
  const values=kind==='stickers'?STICKERS:EMOJI;
  palette.innerHTML='<button type="button" class="ithClosePalette395">‹ Κλείσιμο</button>'+values.map(v=>`<button type="button" data-value="${String(v).replace(/&/g,'&amp;').replace(/"/g,'&quot;')}">${v}</button>`).join('');
  palette.querySelector('.ithClosePalette395').onclick=closePalette;
  palette.querySelectorAll('[data-value]').forEach(b=>b.onclick=()=>{insertAtCursor(el,b.dataset.value||'');closePalette()});
  document.body.appendChild(palette);return false
}
function fileKind(f){const t=String(f&&f.type||'').toLowerCase();if(t.startsWith('image/'))return'photo';if(t.startsWith('video/'))return'video';if(t.startsWith('audio/'))return'audio';return'file'}
function pickCommentFile(postId,frameMode){
  postId=String(postId||'');if(!postId){note('Δεν βρέθηκε η δημοσίευση.');return false}
  const inp=document.createElement('input');inp.type='file';inp.accept='*/*';inp.style.display='none';document.body.appendChild(inp);
  inp.onchange=()=>{const f=inp.files&&inp.files[0];try{inp.remove()}catch(e){}if(!f)return;try{const opener=window.openPreview||(window.ITH&&ITH.openPreview);if(typeof opener!=='function')throw new Error('Η προεπισκόπηση δεν είναι διαθέσιμη.');opener(frameMode?'comment_frame':'comment',f,fileKind(f),postId)}catch(e){note(e.message||String(e))}};
  inp.click();return false
}
function fileAction(id){
  try{
    if(id==='commentInput')return pickCommentFile((typeof state!=='undefined'&&state.commentPostId)||'',false);
    if(id==='dmInput'){
      const channel=(typeof state!=='undefined'&&state.activeChannel)||'';if(!channel){note('Επίλεξε πρώτα φίλο.');return false}
      return window.ITH&&ITH.chooseFile?ITH.chooseFile('dm','file',channel):false;
    }
    if(id==='spaceInput'){
      const channel=(typeof state!=='undefined'&&state.currentSpaceChannel)||'';if(!channel){note('Επίλεξε πρώτα χώρο.');return false}
      return window.ITH&&ITH.chooseFile?ITH.chooseFile('space','file',channel):false;
    }
  }catch(e){note(e.message||String(e))}
  return false
}
function qualifies(el){if(!el||el.tagName!=='TEXTAREA')return false;if(COMM_IDS.has(el.id))return true;const p=String(el.getAttribute('placeholder')||'').toLowerCase();return /(μήνυμα|μηνυμα|message|σχόλιο|σχολιο|comment)/.test(p)}
function decorate(el){
  if(!qualifies(el)||el.dataset.ithTools395==='1')return;el.dataset.ithTools395='1';injectCss();
  const parent=el.parentElement;if(!parent)return;parent.classList.add('ithToolsReady395');
  const tools=document.createElement('div');tools.className='ithComposerTools395';tools.dataset.for=el.id||'';
  const mk=(txt,title,fn)=>{const b=document.createElement('button');b.type='button';b.textContent=txt;b.title=title;b.setAttribute('aria-label',title);b.onclick=fn;return b};
  tools.appendChild(mk('😊','Emoji',()=>openPalette(el.id,'emoji')));
  tools.appendChild(mk('🧩','Σχεδιάκια / stickers',()=>openPalette(el.id,'stickers')));
  const supportsFile=['commentInput','spaceInput'].includes(el.id)||(el.id==='dmInput'&&![...parent.querySelectorAll('button')].some(b=>String(b.textContent||'').includes('📎')));
  if(supportsFile)tools.appendChild(mk('📎','Εισαγωγή αρχείου',()=>fileAction(el.id)));
  parent.insertBefore(tools,el);
}
function scan(root=document){try{if(root.matches&&root.matches('textarea'))decorate(root);(root.querySelectorAll?root.querySelectorAll('textarea'):[]).forEach(decorate)}catch(e){}}
window.addEventListener('message',e=>{const d=e.data||{};if(d.type==='ithaki-v249395-comment-file')pickCommentFile(String(d.postId||''),true)});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&palette){e.stopPropagation();closePalette()}},true);
document.addEventListener('pointerdown',e=>{if(palette&&!palette.contains(e.target)&&!e.target.closest('.ithComposerTools395'))closePalette()},{capture:true});
window.ITH=window.ITH||{};Object.assign(window.ITH,{openComposerPaletteV249395:openPalette,pickCommentFileV249395:pickCommentFile,decorateCommunicationComposersV249395:scan});
const start=()=>{scan();try{new MutationObserver(ms=>ms.forEach(m=>m.addedNodes.forEach(n=>{if(n.nodeType===1)scan(n)}))).observe(document.body,{childList:true,subtree:true})}catch(e){}};
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',start,{once:true});else start();
})();
