"use strict";(()=>{function x(t){let e=document.createElement("div");e.id="eden-app-frame-overlay";let n=t.mode==="both",l=t.showTitle!==!1,p=n?'<button class="eden-app-frame-button toggle-mode" id="eden-toggle-mode-btn" title="Toggle Window Mode">\u229E</button>':"",c=l?'<div id="eden-app-frame-title">App</div>':"";return e.innerHTML=`
    ${c}
    <div id="eden-app-frame-controls">
      ${p}
      <button class="eden-app-frame-button minimize" id="eden-minimize-btn" title="Minimize">\u2212</button>
      <button class="eden-app-frame-button close" id="eden-close-btn" title="Close">\xD7</button>
    </div>
  `,e}function z(t,e){let n=()=>{console.log("[Eden Frame] injectOverlay called, body exists:",!!document.body),document.body?(document.body.insertBefore(t,document.body.firstChild),document.body.classList.add("eden-framed"),console.log("[Eden Frame] Overlay injected into body"),e&&e()):(console.log("[Eden Frame] Body not ready, retrying..."),setTimeout(n,10))};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",n):n()}function I(t){let e=window.matchMedia("(prefers-color-scheme: dark)"),n=l=>{l.matches?t.classList.add("dark"):t.classList.remove("dark")};n(e),e.addEventListener("change",n)}function y(t){let e=document.getElementById("eden-app-frame-title");e&&(e.textContent=t)}function M(t){let e=t.split("."),n=e[e.length-1]||t;return n.charAt(0).toUpperCase()+n.slice(1)}function _(){let t=document.getElementById("eden-close-btn");t&&t.addEventListener("click",()=>{let e=()=>{let n=window.edenFrame?._internal.appId;console.log("[Eden Frame] Stopping app:",n),n?window.edenAPI.shellCommand("process/stop",{appId:n}).catch(console.error):setTimeout(e,100)};e()})}function T(){let t=document.getElementById("eden-minimize-btn");t&&t.addEventListener("click",()=>{let e=()=>{let n=window.edenFrame?._internal.appId;n?window.edenAPI.shellCommand("view/set-view-visibility",{appId:n,visible:!1}).catch(console.error):setTimeout(e,100)};e()})}function L(){let t=document.getElementById("eden-toggle-mode-btn");t&&t.addEventListener("click",()=>{let e=()=>{let n=window.edenFrame?._internal.appId;n?window.edenAPI.shellCommand("view/toggle-view-mode",{appId:n}).then(()=>{console.log("[Eden Frame] View mode toggled")}).catch(console.error):setTimeout(e,100)};e()})}function b(t){if("screenX"in t&&t.screenX!==void 0&&t.screenY!==void 0)return{x:t.screenX,y:t.screenY};if("touches"in t&&t.touches&&t.touches[0]){let e=t.touches[0];if(e.screenX!==void 0&&e.screenY!==void 0)return{x:e.screenX,y:e.screenY};let n=window.edenFrame?._internal.bounds;return n?{x:n.x+e.clientX,y:n.y+e.clientY}:{x:e.clientX+(window.screenX||0),y:e.clientY+(window.screenY||0)}}return{x:0,y:0}}function A(t,e){let n=!1,l=0,p=0,c=null,o=!1,r=null,d=null,u=()=>{let s=window.edenFrame?._internal.appId;d&&s&&(window.edenAPI.shellCommand("view/update-view-bounds",{appId:s,bounds:d}).catch(console.error),d=null),n&&(r=requestAnimationFrame(u))},g=s=>{if(s.target.closest(".eden-app-frame-button"))return;console.log("[Eden Frame] startDrag called, event type:",s.type),console.log("[Eden Frame] currentBounds before refresh:",e.current);let i=window.edenFrame?._internal.bounds;if(i&&i.x!==void 0)e.current={...i},console.log("[Eden Frame] Refreshed currentBounds from edenFrame._internal.bounds:",e.current);else if(!e.current){console.warn("[Eden Frame] Cannot start drag - currentBounds not initialized!");return}n=!0,o=s.type.startsWith("touch");let a=b(s);l=a.x,p=a.y,c={...e.current},console.log("[Eden Frame] Drag started at:",a,"isTouch:",o),s.preventDefault(),s.stopPropagation(),s.stopImmediatePropagation(),o&&(r=requestAnimationFrame(u));let m=window.edenFrame?._internal.appId;!o&&m&&window.edenAPI.shellCommand("view/focus-app",{appId:m}).catch(console.error),!o&&m&&window.edenAPI.shellCommand("view/start-drag",{appId:m,startX:a.x,startY:a.y}).catch(console.error),o||window.addEventListener("mouseup",h)},E=s=>{if(s.preventDefault(),s.stopPropagation(),!n||!c)return;let i=b(s),a=i.x-l,m=i.y-p,w={x:c.x+a,y:c.y+m,width:c.width,height:c.height};e.current=w,d=w},h=s=>{if(s.type.startsWith("touch")&&s.touches&&s.touches.length>0||!n)return;console.log("[Eden Frame] Drag ended, final currentBounds:",e.current),n=!1,c=null,window.removeEventListener("mouseup",h);let i=window.edenFrame?._internal.appId;r&&(cancelAnimationFrame(r),r=null,d&&i&&(window.edenAPI.shellCommand("view/update-view-bounds",{appId:i,bounds:d}).catch(console.error),window.edenFrame._internal.bounds={...d},d=null)),o&&e.current&&(window.edenFrame._internal.bounds={...e.current},console.log("[Eden Frame] Updated edenFrame._internal.bounds after touch drag:",window.edenFrame?._internal.bounds)),!o&&i&&window.edenAPI.shellCommand("view/end-drag",{appId:i}).catch(console.error)};t.addEventListener("mousedown",g),t.addEventListener("touchstart",g,{passive:!1}),document.addEventListener("touchmove",E,{passive:!1,capture:!0}),document.addEventListener("touchend",h,{passive:!1}),document.addEventListener("touchcancel",h,{passive:!1}),console.log("[Eden Frame] Drag event listeners registered")}function B(t,e){let n=document.createElement("div");n.id="eden-resize-handle",n.style.cssText=`
    position: fixed;
    bottom: 0;
    right: 0;
    width: 20px;
    height: 20px;
    cursor: nwse-resize;
    z-index: 2147483647;
    -webkit-app-region: no-drag;
    touch-action: none;
  `,document.body.appendChild(n);let l=!1,p=0,c=0,o=null,r=!1,d=null,u=null,g=()=>{let i=window.edenFrame?._internal.appId;u&&i&&(window.edenAPI.shellCommand("view/update-view-bounds",{appId:i,bounds:u}).catch(console.error),u=null),l&&(d=requestAnimationFrame(g))},E=i=>{if(console.log("[Eden Frame] startResize called, event type:",i.type),!e.current){let w=window.edenFrame?._internal.bounds;if(w&&w.x!==void 0)e.current={...w},console.log("[Eden Frame] Initialized currentBounds from edenFrame._internal.bounds:",e.current);else{console.warn("[Eden Frame] Cannot start resize - currentBounds not initialized!");return}}l=!0,r=i.type.startsWith("touch");let a=b(i);p=a.x,c=a.y,o={...e.current},console.log("[Eden Frame] Resize started at:",a,"isTouch:",r),i.preventDefault(),i.stopPropagation(),r&&(d=requestAnimationFrame(g));let m=window.edenFrame?._internal.appId;!r&&m&&window.edenAPI.shellCommand("view/focus-app",{appId:m}).catch(console.error),!r&&m&&window.edenAPI.shellCommand("view/start-resize",{appId:m,startX:a.x,startY:a.y}).catch(console.error),r||window.addEventListener("mouseup",s)},h=i=>{if(console.log("[Eden Frame] moveResize called, isResizing:",l,"event type:",i.type),i.preventDefault(),i.stopPropagation(),!l||!o){console.log("[Eden Frame] moveResize returning early - isResizing:",l,"resizeStartBounds:",o);return}let a=b(i),m=a.x-p,w=a.y-c;console.log("[Eden Frame] moveResize coords:",a,"delta:",{deltaX:m,deltaY:w});let f=o.width+m,v=o.height+w;t.minSize?(f=Math.max(f,t.minSize.width||200),v=Math.max(v,t.minSize.height||200)):(f=Math.max(f,200),v=Math.max(v,200)),t.maxSize&&(f=Math.min(f,t.maxSize.width||2e3),v=Math.min(v,t.maxSize.height||2e3));let F={x:o.x,y:o.y,width:Math.round(f),height:Math.round(v)};console.log("[Eden Frame] moveResize newBounds:",F),e.current=F,u=F},s=i=>{if(!l)return;console.log("[Eden Frame] Resize ended, final currentBounds:",e.current),l=!1,o=null,window.removeEventListener("mouseup",s);let a=window.edenFrame?._internal.appId;d&&(cancelAnimationFrame(d),d=null,u&&a&&(window.edenAPI.shellCommand("view/update-view-bounds",{appId:a,bounds:u}).catch(console.error),window.edenFrame._internal.bounds={...u},u=null)),r&&e.current&&(window.edenFrame._internal.bounds={...e.current},console.log("[Eden Frame] Updated edenFrame._internal.bounds after touch resize:",window.edenFrame?._internal.bounds)),!r&&a&&window.edenAPI.shellCommand("view/end-resize",{appId:a}).catch(console.error)};n.addEventListener("mousedown",E),n.addEventListener("touchstart",E,{passive:!1}),document.addEventListener("touchmove",h,{passive:!1,capture:!0}),document.addEventListener("touchend",s,{passive:!1}),document.addEventListener("touchcancel",s,{passive:!1}),console.log("[Eden Frame] Resize event listeners registered")}(function(){if(console.log("[Eden Frame] Injection script started"),window.edenFrame||(window.edenFrame={setTitle:o=>{},_internal:{appId:"",injected:!1,config:{},currentMode:"tiled",bounds:{x:0,y:0,width:0,height:0}}}),window.edenFrame._internal.injected){console.log("[Eden Frame] Already injected, skipping");return}window.edenFrame._internal.injected=!0;let t=null,e="App",n={current:null},l=x(window.edenFrame._internal.config),p=()=>{window.edenFrame&&window.edenFrame._internal.appId&&(t=window.edenFrame._internal.appId,e=M(t),y(e)),_(),T(),L(),c(),window.edenAPI.subscribe("view/mode-changed",o=>{let{mode:r,bounds:d}=o;console.log("[Eden Frame] View mode changed to:",r,"with bounds:",d),window.edenFrame._internal.currentMode=r,d&&d.x!==void 0&&(window.edenFrame._internal.bounds=d,n.current={...d}),c()}),console.log("[Eden Frame] Subscribed to view/mode-changed")},c=()=>{let o=window.edenFrame._internal.currentMode,r=window.edenFrame._internal.config,d=window.edenFrame._internal.bounds;if(console.log("[Eden Frame] Window mode:",o,"Config:",r,"Initial bounds:",d),o!=="floating")return;if(d&&d.x!==void 0)n.current={...d},console.log("[Eden Frame] Initialized bounds from backend:",n.current);else if(r.defaultSize){let s=r.defaultPosition?.x||0,i=r.defaultPosition?.y||0;n.current={x:0+s,y:0+i,width:r.defaultSize.width||800,height:r.defaultSize.height||600},console.log("[Eden Frame] Initialized bounds from config:",n.current)}let u=r.movable!==!1,g=r.resizable!==!1;console.log("[Eden Frame] Movable:",u,"Resizable:",g),u&&A(l,n),g&&B(r,n)};window.edenAPI.subscribe("view/bounds-updated",o=>{n.current={...o},window.edenFrame._internal.bounds={...o}}),console.log("[Eden Frame] Subscribed to view/bounds-updated"),document.addEventListener("touchstart",o=>{console.log("[Eden Frame] Global touchstart on:",o.target)},{passive:!1,capture:!0}),document.addEventListener("touchcancel",o=>{console.log("[Eden Frame] Global touchcancel on:",o.target),console.log("[Eden Frame] Stack trace:",new Error().stack)},{capture:!0}),z(l,()=>{I(l),p()}),window.edenFrame.setTitle=o=>{y(o)}})();})();
//# sourceMappingURL=frame-injector.js.map
