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