(function(){const e=document.createElement("link").relList;if(e&&e.supports&&e.supports("modulepreload"))return;for(const t of document.querySelectorAll('link[rel="modulepreload"]'))l(t);new MutationObserver(t=>{for(const s of t)if(s.type==="childList")for(const r of s.addedNodes)r.tagName==="LINK"&&r.rel==="modulepreload"&&l(r)}).observe(document,{childList:!0,subtree:!0});function n(t){const s={};return t.integrity&&(s.integrity=t.integrity),t.referrerPolicy&&(s.referrerPolicy=t.referrerPolicy),t.crossOrigin==="use-credentials"?s.credentials="include":t.crossOrigin==="anonymous"?s.credentials="omit":s.credentials="same-origin",s}function l(t){if(t.ep)return;t.ep=!0;const s=n(t);fetch(t.href,s)}})();class m{constructor(){this.statusDiv=document.getElementById("status"),this.setupEventListeners(),this.setupMessageListeners(),this.log("App initialized")}getAppAPI(){return window.getAppAPI()}setupEventListeners(){document.getElementById("ping-btn")?.addEventListener("click",()=>{this.pingBackend()}),document.getElementById("status-btn")?.addEventListener("click",()=>{this.getBackendStatus()}),document.getElementById("hello-btn")?.addEventListener("click",()=>{this.sayHello()})}setupMessageListeners(){const e=this.getAppAPI();e.on("backend-ready",n=>{this.log(`✓ ${n.message}`)}),e.on("heartbeat",n=>{console.log("Heartbeat from backend:",n)})}async pingBackend(){this.log("Sending ping to backend...");try{const e=await this.getAppAPI().request("ping",{});this.log(`Pong received! Message count: ${e.messageCount}`)}catch(e){this.log(`Error: ${e.message}`)}}async getBackendStatus(){this.log("Requesting backend status...");try{const e=await this.getAppAPI().request("get-status",{});this.log(`Status: ${e.status}`),this.log(`Uptime: ${e.uptime.toFixed(2)}s`),this.log(`Messages: ${e.messageCount}`)}catch(e){this.log(`Error: ${e.message}`)}}async sayHello(){const e=await this.showInputDialog("Say Hello","What would you like to say?","Hello from frontend!");if(e){this.log(`Sending: "${e}"`);try{const n=await this.getAppAPI().request("hello",{message:e});this.log(`Backend says: ${n.message}`)}catch(n){this.log(`Error: ${n.message}`)}}}showInputDialog(e,n="Enter value...",l=""){return new Promise(t=>{const s=document.getElementById("input-dialog"),r=document.getElementById("input-dialog-title"),o=document.getElementById("input-dialog-input"),d=document.getElementById("input-dialog-close"),u=document.getElementById("input-dialog-cancel"),g=document.getElementById("input-dialog-confirm");if(!s||!r||!o||!d||!u||!g){t(null);return}r.textContent=e,o.placeholder=n,o.value=l,s.style.display="flex",setTimeout(()=>o.focus(),100);const p=i=>{s.style.display="none",o.value="",t(i)},h=()=>{const i=o.value.trim();p(i||null)},a=()=>{p(null)};g.onclick=h,u.onclick=a,d.onclick=a;const c=i=>{i.key==="Enter"?(h(),o.removeEventListener("keydown",c)):i.key==="Escape"&&(a(),o.removeEventListener("keydown",c))};o.addEventListener("keydown",c),s.onclick=i=>{i.target===s&&a()}})}log(e){const n=document.createElement("p");if(n.textContent=`[${new Date().toLocaleTimeString()}] ${e}`,this.statusDiv)for(this.statusDiv.appendChild(n),this.statusDiv.scrollTop=this.statusDiv.scrollHeight;this.statusDiv.children.length>20;)this.statusDiv.removeChild(this.statusDiv.firstChild)}}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>new m):new m;
