// @bun
var{defineProperty:P,getOwnPropertyNames:Y,getOwnPropertyDescriptor:Z}=Object,_=Object.prototype.hasOwnProperty;function $(j){return this[j]}var H=(j)=>{var q=(Q??=new WeakMap).get(j),w;if(q)return q;if(q=P({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var F of Y(j))if(!_.call(q,F))P(q,F,{get:$.bind(j,F),enumerable:!(w=Z(j,F))||w.enumerable})}return Q.set(j,q),q},Q;var v=(j)=>j;function x(j,q){this[j]=v.bind(null,q)}var I=(j,q)=>{for(var w in q)P(j,w,{get:q[w],enumerable:!0,configurable:!0,set:x.bind(q,w)})};var m=(j,q)=>()=>(j&&(q=j(j=0)),q);var s=import.meta.require;var D={};I(D,{processDeferDirectives:()=>T});function R(){return`stx-defer-${++U}`}function S(j){let q=j.match(/on:\s*['"]?(\w+)(?:\((\d+)\))?['"]?/);if(q)return{trigger:q[1],value:q[2]};return{trigger:"immediate"}}function V(j,q,w){let{trigger:F,value:E}=S(q),z=w,J,G,A,K=w.match(/@placeholder\s*([\s\S]*?)(?=@loading|@error|$)/);if(K)J=K[1].trim(),z=z.replace(/@placeholder[\s\S]*?(?=@loading|@error|$)/,"");let N=w.match(/@loading\s*([\s\S]*?)(?=@error|$)/);if(N)G=N[1].trim(),z=z.replace(/@loading[\s\S]*?(?=@error|$)/,"");let O=w.match(/@error(?:\s*\(\s*(\w+)\s*\))?\s*([\s\S]*)$/);if(O)A=O[2].trim(),z=z.replace(/@error(?:\s*\(\s*\w+\s*\))?[\s\S]*$/,"");return z=z.trim(),{trigger:F,triggerValue:E,content:z,placeholder:J,loading:G,error:A}}function B(j,q){let{trigger:w,triggerValue:F}=q,E;switch(w){case"visible":E=`
        const observer = new IntersectionObserver((entries) => {
          if (entries[0].isIntersecting) {
            observer.disconnect();
            loadContent();
          }
        }, { threshold: 0.1 });
        observer.observe(container);
      `;break;case"idle":E=`
        if ('requestIdleCallback' in window) {
          requestIdleCallback(() => loadContent(), { timeout: 3000 });
        }
else {
          setTimeout(loadContent, 200);
        }
      `;break;case"interaction":E=`
        const events = ['click', 'focus', 'touchstart'];
        const handler = () => {
          events.forEach(e => container.removeEventListener(e, handler));
          loadContent();
        };
        events.forEach(e => container.addEventListener(e, handler, { once: true, passive: true }));
      `;break;case"hover":E=`
        const handler = () => {
          container.removeEventListener('mouseenter', handler);
          container.removeEventListener('focusin', handler);
          loadContent();
        };
        container.addEventListener('mouseenter', handler, { once: true });
        container.addEventListener('focusin', handler, { once: true });
      `;break;case"timer":E=`
        setTimeout(loadContent, ${F||"1000"});
      `;break;case"immediate":default:E=`
        Promise.resolve().then(loadContent);
      `;break}return`
    <script>
    (function() {
      const container = document.getElementById('${j}');
      if (!container) return;

      const contentEl = container.querySelector('[data-defer-content]');
      const placeholderEl = container.querySelector('[data-defer-placeholder]');
      const loadingEl = container.querySelector('[data-defer-loading]');
      const errorEl = container.querySelector('[data-defer-error]');

      function showElement(el) {
        if (el) el.style.display = '';
      }
      function hideElement(el) {
        if (el) el.style.display = 'none';
      }

      function loadContent() {
        hideElement(placeholderEl);
        showElement(loadingEl);

        // Simulate async load (content is already rendered but hidden)
        Promise.resolve().then(() => {
          hideElement(loadingEl);
          showElement(contentEl);
          container.dispatchEvent(new CustomEvent('defer:loaded'));
        }).catch((err) => {
          hideElement(loadingEl);
          if (errorEl) {
            showElement(errorEl);
          }
          console.error('Defer load error:', err);
        });
      }

      ${E}
    })();
    </script>
  `}function T(j,q,w){U=0;let F=/@defer\s*\(([^)]*)\)\s*([\s\S]*?)@enddefer/g;return j.replace(F,(E,z,J)=>{let G=R(),A=V(E,z,J),K=A.placeholder?`<div data-defer-placeholder>${A.placeholder}</div>`:"",N=A.loading?`<div data-defer-loading style="display: none;">${A.loading}</div>`:'<div data-defer-loading style="display: none;"></div>',O=A.error?`<div data-defer-error style="display: none;">${A.error}</div>`:"",W=`<div data-defer-content style="display: none;">${A.content}</div>`,X=B(G,A);return`
      <div id="${G}" class="stx-defer" data-defer-trigger="${A.trigger}">
        ${K}
        ${N}
        ${W}
        ${O}
      </div>
      ${X}
    `})}var U=0;export{T as processDeferDirectives};
