// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
export{};const o=document.getElementById("message-input"),g=o.form,f=document.getElementById("message-section"),a=document.getElementById("open-moji-attribution"),r=document.getElementById("connection-state"),l=document.getElementById("current-user");let s=100,u=0,i="";const v=e=>new Date(e+16510752e5).toLocaleString(),p=e=>{const t=document.createElement("div");if(a&&a.getAttribute("type")!=="font"){for(const n of e.author)t.append(d(n));t.innerHTML+=": ";for(const n of e.content)t.append(d(n))}else t.innerText="".concat(e.author.join(""),": ").concat(e.content.join(""));t.setAttribute("tooltip",v(e.timestamp)),f.append(t)},k=e=>{if(l.innerHTML="",a&&a.getAttribute("type")!=="font"){for(const t of e)l.append(d(t));return}l.innerText=e.join("")},d=e=>{const t=[...e].map(y=>y.codePointAt(0).toString(16).padStart(4,"0")).join("-").toUpperCase(),n=document.createElement("img");return n.src="/static/img/openmoji-svg-14.0/".concat(t,".svg"),n.classList.add("emoji"),n.alt=e,n},b=()=>{i&&!o.value&&(o.value=i,i="")},c=e=>{let t;if(r.onclick=()=>{},e==="connecting")t="Versuche mit WebSocket zu verbinden";else if(e==="connected")t="Mit WebSocket verbunden!";else if(e==="disconnected")t="Verbindung getrennt. Drücke hier um erneut zu versuchen.",r.onclick=()=>{u=0,s=500,r.onclick=()=>{},m()};else{console.error("invalid state",e);return}r.setAttribute("state",e),r.setAttribute("tooltip",t)},M=e=>{const t=JSON.parse(e.data);switch(t.type){case"messages":{f.innerText="";for(const n of t.messages)p(n);break}case"message":{p(t.message);break}case"init":{k(t.current_user),console.log("Connected as",t.current_user.join("")),c("connected"),s=100,u=0;break}case"users":{console.debug("Received users data",t.users);break}case"ratelimit":{b(),alert("Retry after ".concat(t.retry_after," seconds."));break}case"error":{b(),alert(t.error);break}default:console.error("Invalid type ".concat(t.type))}},m=()=>{c("connecting");const e=new WebSocket((window.location.protocol==="https:"?"wss:":"ws:")+"//".concat(window.location.host,"/websocket/emoji-chat")),t=setInterval(()=>{e.send("")},1e4);e.onclose=n=>{if(g.onsubmit=()=>{},n.wasClean){console.debug("Connection closed cleanly, code=".concat(n.code," reason=").concat(n.reason)),c("disconnected");return}if(console.debug("Connection closed, reconnecting in ".concat(s,"ms")),c("connecting"),clearInterval(t),u>20){c("disconnected");return}setTimeout(()=>{s=Math.max(500,Math.floor(Math.min(15e3,1.5*s-200))),u++,m()},s)},e.onopen=n=>{console.debug("Opened WebSocket",n)},e.onmessage=M,g.onsubmit=n=>{o.value!==""&&(i=o.value,e.send(JSON.stringify({type:"message",message:o.value})),o.value=""),n.preventDefault()}};m();// @license-end
//# sourceMappingURL=chat.js.map
