(function(){const o=document.createElement("link").relList;if(o&&o.supports&&o.supports("modulepreload"))return;for(const t of document.querySelectorAll('link[rel="modulepreload"]'))a(t);new MutationObserver(t=>{for(const r of t)if(r.type==="childList")for(const i of r.addedNodes)i.tagName==="LINK"&&i.rel==="modulepreload"&&a(i)}).observe(document,{childList:!0,subtree:!0});function n(t){const r={};return t.integrity&&(r.integrity=t.integrity),t.referrerPolicy&&(r.referrerPolicy=t.referrerPolicy),t.crossOrigin==="use-credentials"?r.credentials="include":t.crossOrigin==="anonymous"?r.credentials="omit":r.credentials="same-origin",r}function a(t){if(t.ep)return;t.ep=!0;const r=n(t);fetch(t.href,r)}})();const d=new URLSearchParams(window.location.search);document.body.dataset.theme=d.get("theme")??"light";const u=d.get("multiUser")==="true";console.log("Penpot MCP multi-user mode:",u);let s=null;const l=document.getElementById("connection-status");function c(e,o,n){if(l){const a=n?`${e}: ${n}`:e;l.textContent=a,l.style.color=o?"var(--accent-primary)":"var(--error-700)"}}function f(e){s&&s.readyState===WebSocket.OPEN?(s.send(JSON.stringify(e)),console.log("Sent response to MCP server:",e)):console.error("WebSocket not connected, cannot send response")}function m(){if(s?.readyState===WebSocket.OPEN){c("Already connected",!0);return}try{let e="/mcp/ws";u&&(e+=`?userToken=${encodeURIComponent("dummyToken")}`),s=new WebSocket(e),c("Connecting...",!1),s.onopen=()=>{console.log("Connected to MCP server"),c("Connected to MCP server",!0)},s.onmessage=o=>{console.log("Received from MCP server:",o.data);try{const n=JSON.parse(o.data);parent.postMessage(n,"*")}catch(n){console.error("Failed to parse WebSocket message:",n)}},s.onclose=o=>{console.log("Disconnected from MCP server");const n=o.reason||void 0;c("Disconnected",!1,n),s=null},s.onerror=o=>{console.error("WebSocket error:",o),c("Connection error",!1)}}catch(e){console.error("Failed to connect to MCP server:",e);const o=e instanceof Error?e.message:void 0;c("Connection failed",!1,o)}}document.querySelector("[data-handler='connect-mcp']")?.addEventListener("click",()=>{m()});window.addEventListener("message",e=>{e.data.source==="penpot"?document.body.dataset.theme=e.data.theme:e.data.type==="task-response"&&f(e.data.response)});
