(function(){const s=document.createElement("link").relList;if(s&&s.supports&&s.supports("modulepreload"))return;for(const t of document.querySelectorAll('link[rel="modulepreload"]'))c(t);new MutationObserver(t=>{for(const n of t)if(n.type==="childList")for(const d of n.addedNodes)d.tagName==="LINK"&&d.rel==="modulepreload"&&c(d)}).observe(document,{childList:!0,subtree:!0});function o(t){const n={};return t.integrity&&(n.integrity=t.integrity),t.referrerPolicy&&(n.referrerPolicy=t.referrerPolicy),t.crossOrigin==="use-credentials"?n.credentials="include":t.crossOrigin==="anonymous"?n.credentials="omit":n.credentials="same-origin",n}function c(t){if(t.ep)return;t.ep=!0;const n=o(t);fetch(t.href,n)}})();const m=15e6,P=new URLSearchParams(window.location.hash.split("?")[1]);document.body.dataset.theme=P.get("theme")??"light";let r=null,S=!1;const p=document.getElementById("connection-status"),g=document.getElementById("status-text"),y=document.getElementById("current-task"),l=document.getElementById("executed-code"),a=document.getElementById("copy-code-btn"),u=document.getElementById("connect-btn"),f=document.getElementById("disconnect-btn"),h=document.getElementById("version-warning"),E=document.getElementById("version-warning-text");function i(e,s){p&&(p.dataset.status=e),g&&(g.textContent=s);const o=e==="connected";u&&(u.hidden=o),f&&(f.hidden=!o),parent.postMessage({type:"update-connection-status",status:e},"*")}function C(e){y&&(y.textContent=e??"---"),e===null&&b(null)}function b(e){l&&(l.value=e??""),a&&(a.disabled=!e)}function w(e){if(r&&r.readyState===WebSocket.OPEN){let s=JSON.stringify(e);if(S&&s.length>m){const o=`Serialised response size (${s.length}) exceeds maximum of ${m}.`;console.warn(o+" [integrated remote MCP mode restriction]; sending error response instead; original response:",e),e={id:e.id,success:!1,error:o},s=JSON.stringify(e)}r.send(s),console.log("Sent response to MCP server:",e)}else console.error("WebSocket not connected, cannot send response")}function M(e,s){if(r?.readyState===WebSocket.OPEN){i("connected","Connected");return}try{let o=e||"/mcp/ws",c;s&&(o+=`?userToken=${encodeURIComponent(s)}`),r=new WebSocket(o),i("connecting","Connecting..."),r.onopen=()=>{setTimeout(()=>{r&&(console.log("Connected to MCP server"),i("connected","Connected"))},100)},r.onmessage=t=>{try{console.log("Received from MCP server:",t.data);const n=JSON.parse(t.data);n.task&&(C(n.task),b(n.params?.code??null)),parent.postMessage(n,"*")}catch(n){console.error("Failed to parse WebSocket message:",n)}},r.onclose=t=>{if(!c){console.log("Disconnected from MCP server");const n=t.reason?`Disconnected: ${t.reason}`:"Disconnected";i("disconnected",n),C(null)}r=null},r.onerror=t=>{console.error("WebSocket error:",t),c=t,i("error","Connection error")}}catch(o){console.error("Failed to connect to MCP server:",o);const c=o instanceof Error?o.message:void 0,t=c?`Connection failed: ${c}`:"Connection failed";i("error",t)}}a?.addEventListener("click",()=>{const e=l?.value;e&&navigator.clipboard.writeText(e).then(()=>{a.classList.add("copied"),setTimeout(()=>a.classList.remove("copied"),1500)})});u?.addEventListener("click",()=>{M()});f?.addEventListener("click",()=>{r?.close()});window.addEventListener("message",e=>{e.data.type==="mcp-mode"&&(S=e.data.integratedRemoteMcp),e.data.type==="start-server"&&M(e.data.url,e.data.token),e.data.type==="version-mismatch"&&h&&E&&(E.innerHTML=`<b>Version mismatch detected</b>: This version of the MCP server is intended for Penpot ${e.data.mcpVersion} while the current version is ${e.data.penpotVersion}. Executions may not work or produce suboptimal results.`,h.hidden=!1),e.data.type==="stop-server"?r?.close():e.data.source==="penpot"?document.body.dataset.theme=e.data.theme:e.data.type==="task-response"&&w(e.data.response)});parent.postMessage({type:"ui-initialized"},"*");
