// @bun
var{defineProperty:O,getOwnPropertyNames:b,getOwnPropertyDescriptor:I}=Object,k=Object.prototype.hasOwnProperty;function x(q){return this[q]}var f=(q)=>{var j=(Q??=new WeakMap).get(q),F;if(j)return j;if(j=O({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var z of b(q))if(!k.call(j,z))O(j,z,{get:x.bind(q,z),enumerable:!(F=I(q,z))||F.enumerable})}return Q.set(q,j),j},Q;var S=(q)=>q;function E(q,j){this[q]=S.bind(null,j)}var c=(q,j)=>{for(var F in j)O(q,F,{get:j[F],enumerable:!0,configurable:!0,set:E.bind(j,F)})};var M=(q,j)=>()=>(q&&(j=q(q=0)),j);var p=import.meta.require;function _(q){let j={prevent:!1,stop:!1,once:!1,capture:!1,self:!1,passive:!1,keys:[],systemKeys:[],mouse:null,debounce:null,throttle:null};for(let F of q){let z=F.toLowerCase();if(z==="prevent")j.prevent=!0;else if(z==="stop")j.stop=!0;else if(z==="once")j.once=!0;else if(z==="capture")j.capture=!0;else if(z==="self")j.self=!0;else if(z==="passive")j.passive=!0;else if(P.has(z))j.systemKeys.push(z);else if(z in N)j.mouse=z;else if(z in H||/^[a-z]$/.test(z))j.keys.push(H[z]||z.toUpperCase());else if(z.startsWith("debounce")){let J=z.match(/debounce(?:\.(\d+))?/);j.debounce=J?.[1]?Number.parseInt(J[1],10):300}else if(z.startsWith("throttle")){let J=z.match(/throttle(?:\.(\d+))?/);j.throttle=J?.[1]?Number.parseInt(J[1],10):300}}return j}function A(q){let j=[],F=0,z=/<([a-z][a-z0-9-]*)\s+([^>]*@[a-z][^>]*)>/gi,J;while((J=z.exec(q))!==null){let[G,$,X]=J,D=J.index,V=D+G.length,Z=`__stx_evt_${F++}`,K=[],L=/@([a-z][a-z0-9.]*)\s*=\s*"([^"]*)"/gi,B;while((B=L.exec(X))!==null){let[,C,y]=B,R=C.split("."),U=R[0];if(!W.has(U))continue;K.push({attribute:`@${C}`,event:U,handler:y,modifiers:_(R.slice(1)),elementId:Z})}let Y=/@([a-z][a-z0-9.]*)\s*=\s*'([^']*)'/gi;while((B=Y.exec(X))!==null){let[,C,y]=B,R=C.split("."),U=R[0];if(!W.has(U))continue;K.push({attribute:`@${C}`,event:U,handler:y,modifiers:_(R.slice(1)),elementId:Z})}if(K.length>0)j.push({originalTag:G,elementId:Z,events:K,startIndex:D,endIndex:V})}return j}function T(q){let j=[],{modifiers:F}=q;if(F.self)j.push("if ($event.target !== $el) return");for(let z of F.systemKeys)j.push(`if (!$event.${z}Key) return`);if(F.keys.length>0){let z=F.keys.map((J)=>`$event.key === '${J}'`).join(" || ");j.push(`if (!(${z})) return`)}if(F.mouse!==null){let z=N[F.mouse];j.push(`if ($event.button !== ${z}) return`)}if(F.prevent)j.push("$event.preventDefault()");if(F.stop)j.push("$event.stopPropagation()");return j.join("; ")}function g(q,j){let F=q;if(j.debounce!==null)F=`__stx_debounce(() => { ${F} }, ${j.debounce})`;else if(j.throttle!==null)F=`__stx_throttle(() => { ${F} }, ${j.throttle})`;return F}function v(q){let{modifiers:j}=q,F=q.elementId.replace(/\\/g,"\\\\").replace(/'/g,"\\'"),z=T(q),J=g(q.handler,j),G=[];if(j.once)G.push("once: true");if(j.capture)G.push("capture: true");if(j.passive)G.push("passive: true");let $=G.length>0?`, { ${G.join(", ")} }`:"",X=J.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t");if(q.event==="mounted")return`
  (function() {
    var $el = document.getElementById('${F}');
    if (!$el) return;
    // Run mounted handler immediately (DOM is ready)
    var scope = $el.closest('[data-stx-scope]');
    if (scope && scope.__stx_execute) {
      scope.__stx_execute('${X}', null, $el);
    }
else {
      __stx_runHandler('${X}', null, $el);
    }
  })();`;if(q.event==="unmounted")return`
  (function() {
    var $el = document.getElementById('${F}');
    if (!$el) return;
    // Set up MutationObserver to detect removal
    var observer = new MutationObserver(function(mutations) {
      for (var m of mutations) {
        for (var node of m.removedNodes) {
          if (node === $el || (node.contains && node.contains($el))) {
            observer.disconnect();
            var scope = $el.closest('[data-stx-scope]');
            if (scope && scope.__stx_execute) {
              scope.__stx_execute('${X}', null, $el);
            }
else {
              __stx_runHandler('${X}', null, $el);
            }
            return;
          }
        }
      }
    });
    observer.observe(document.body, { childList: true, subtree: true });
    // Store observer for cleanup
    $el.__stx_unmount_observer = observer;
  })();`;return`
  (function() {
    var $el = document.getElementById('${F}');
    if (!$el) return;
    // eslint-disable-next-line pickier/no-unused-vars
    $el.addEventListener('${q.event}', function($event) {
      ${z?z+";":""}
      // Find the closest reactive scope and execute within its context
      var scope = $el.closest('[data-stx-scope]');
      if (scope && scope.__stx_execute) {
        scope.__stx_execute('${X}', $event, $el);
      }
else {
        // Fallback for non-reactive context (entity-safe; decoded + run in memory)
        __stx_runHandler('${X}', $event, $el);
      }
    }${$});
  })();`}function h(q){if(q.length===0)return"";return`
<script data-stx-events>
(function() {
  'use strict';

  // Utility functions
  function __stx_debounce(fn, delay) {
    var timeout;
    return function() {
      var args = arguments;
      var ctx = this;
      clearTimeout(timeout);
      timeout = setTimeout(function() { fn.apply(ctx, args); }, delay);
    };
  }

  function __stx_throttle(fn, delay) {
    var last = 0;
    return function() {
      var now = Date.now();
      if (now - last >= delay) {
        last = now;
        fn.apply(this, arguments);
      }
    };
  }

  // Non-reactive fallback runner. The handler is embedded HTML-entity-encoded so
  // the surrounding <script> can never contain raw markup (e.g. </script>); we
  // decode it to JS only in memory here, then run it with $event/$el in scope.
  function __stx_runHandler(h, $event, $el) {
    var code = h
      .replace(/&lt;/g, '<')
      .replace(/&gt;/g, '>')
      .replace(/&quot;/g, '"')
      .replace(/&#39;/g, "'")
      .replace(/&#x27;/g, "'")
      .replace(/&amp;/g, '&');
    try {
      // eslint-disable-next-line no-new-func
      (new Function('$event', '$el', code)).call($el, $event, $el);
    }
    catch (e) {
      if (typeof console !== 'undefined') console.error('[stx] event handler error:', e);
    }
  }

  // Event listeners
  ${q.flatMap((F)=>F.events).map(v).join(`
`)}
})();
</script>`}function u(q){let j=A(q);if(j.length===0)return{template:q,bindings:[]};let F=q,z=[];for(let J=j.length-1;J>=0;J--){let G=j[J],$=G.originalTag;if(!$.includes(" id="))$=$.replace(/^<([a-z][a-z0-9-]*)/,`<$1 id="${G.elementId}"`);else{let X=$.match(/id=["']([^"']+)["']/);if(X)for(let D of G.events)D.elementId=X[1]}for(let X of G.events){let D=X.attribute.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),V=new RegExp(`\\s*${D}\\s*=\\s*"[^"]*"`,"g"),Z=new RegExp(`\\s*${D}\\s*=\\s*'[^']*'`,"g");$=$.replace(V,""),$=$.replace(Z,"")}F=F.slice(0,G.startIndex)+$+F.slice(G.endIndex),z.push(...G.events)}return{template:F,bindings:z}}function o(q,j,F){if(/\b(?:state|derived|effect)\s*(?:<[^<>()]*>)?\s*\(/.test(q)||/@(?:model|show|for|if)\s*=/.test(q)||/data-stx(?:-auto)?(?![-\w])/.test(q))return q;if(j.__stx_sfc_mode){if(/x-data\s*=\s*["']/.test(q))return q;let{template:D,bindings:V}=u(q),Z=j.__stx_event_bindings||[];return j.__stx_event_bindings=[...Z,...V],D}let J=A(q);if(J.length===0)return q;let G=q;for(let D=J.length-1;D>=0;D--){let V=J[D],Z=V.originalTag;if(!Z.includes(" id="))Z=Z.replace(/^<([a-z][a-z0-9-]*)/,`<$1 id="${V.elementId}"`);else{let K=Z.match(/id=["']([^"']+)["']/);if(K)for(let L of V.events)L.elementId=K[1]}for(let K of V.events){let L=K.attribute.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),B=new RegExp(`\\s*${L}\\s*=\\s*"[^"]*"`,"g"),Y=new RegExp(`\\s*${L}\\s*=\\s*'[^']*'`,"g");Z=Z.replace(B,""),Z=Z.replace(Y,"")}G=G.slice(0,V.startIndex)+Z+G.slice(V.endIndex)}let $=h(J),X=G.lastIndexOf("</body>");if(X!==-1)G=G.slice(0,X)+$+`
`+G.slice(X);else G+=$;return G}function m(q){return/@[a-z]+(?:\.[a-z]+)*\s*=\s*["'][^"']*["']/i.test(q)}var W,H,P,N;var w=M(()=>{W=new Set(["click","dblclick","mousedown","mouseup","mousemove","mouseenter","mouseleave","mouseover","mouseout","contextmenu","wheel","keydown","keyup","keypress","submit","reset","change","input","focus","blur","focusin","focusout","select","invalid","touchstart","touchend","touchmove","touchcancel","drag","dragstart","dragend","dragenter","dragleave","dragover","drop","scroll","resize","load","error","abort","animationstart","animationend","animationiteration","transitionstart","transitionend","transitionrun","transitioncancel","mounted","unmounted"]),H={enter:"Enter",tab:"Tab",delete:"Delete",backspace:"Backspace",esc:"Escape",escape:"Escape",space:" ",up:"ArrowUp",down:"ArrowDown",left:"ArrowLeft",right:"ArrowRight",home:"Home",end:"End",pageup:"PageUp",pagedown:"PageDown"},P=new Set(["ctrl","alt","shift","meta"]),N={left:0,middle:1,right:2}});w();export{o as processEventDirectives,m as hasEventDirectives,u as extractAndProcessEvents};
