// @bun
var{defineProperty:J,getOwnPropertyNames:T,getOwnPropertyDescriptor:U}=Object,W=Object.prototype.hasOwnProperty;function X(j){return this[j]}var B=(j)=>{var q=(K??=new WeakMap).get(j),A;if(q)return q;if(q=J({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var z of T(j))if(!W.call(q,z))J(q,z,{get:X.bind(j,z),enumerable:!(A=U(j,z))||A.enumerable})}return K.set(j,q),q},K;var Y=(j)=>j;function Z(j,q){this[j]=Y.bind(null,q)}var _=(j,q)=>{for(var A in q)J(j,A,{get:q[A],enumerable:!0,configurable:!0,set:Z.bind(q,A)})};var S=(j,q)=>()=>(j&&(q=j(j=0)),q);var g=import.meta.require;var R={};_(R,{withErrorBoundary:()=>L,processErrorBoundaryDirectives:()=>G,generateErrorBoundaryRuntime:()=>x,generateErrorBoundaryCSS:()=>v,createErrorBoundary:()=>V});function C(){return`eb-${++w}-${Date.now().toString(36)}`}function G(j,q={},A){let z=/@errorBoundary(?:\s*\(([^)]*)\))?\s*([\s\S]*?)@fallback\s*([\s\S]*?)@enderrorBoundary/gi;return j.replace(z,(D,F,M,N)=>{let P=C(),Q=H(F||"");return O(P,M.trim(),N.trim(),Q)})}function H(j){let q={},A=/(\w+)\s*:\s*(?:'([^']*)'|"([^"]*)"|(\w+))/g,z;while((z=A.exec(j))!==null){let D=z[1],F=z[2]||z[3]||z[4];switch(D){case"id":q.id=F;break;case"logErrors":q.logErrors=F==="true";break}}return q}function O(j,q,A,z){let D=z.logErrors!==!1;return`
<div class="stx-error-boundary" data-boundary-id="${j}" data-has-error="false">
  <div class="stx-error-boundary-content" data-boundary-content="${j}">
    ${q}
  </div>
  <div class="stx-error-boundary-fallback" data-boundary-fallback="${j}" style="display: none;">
    ${A}
  </div>
</div>
<script>
(function() {
  const boundaryId = '${j}';
  const logErrors = ${D};
  const boundary = document.querySelector('[data-boundary-id="' + boundaryId + '"]');
  const contentEl = document.querySelector('[data-boundary-content="' + boundaryId + '"]');
  const fallbackEl = document.querySelector('[data-boundary-fallback="' + boundaryId + '"]');

  if (!boundary || !contentEl || !fallbackEl) return;

  // Error state
  let hasError = false;
  let lastError = null;

  // Show error fallback
  function showFallback(error) {
    hasError = true;
    lastError = error;
    boundary.setAttribute('data-has-error', 'true');
    contentEl.style.display = 'none';
    fallbackEl.style.display = '';

    if (logErrors) {
      console.error('[stx:errorBoundary:' + boundaryId + ']', error);
    }

    // Dispatch error event
    boundary.dispatchEvent(new CustomEvent('stx:error', {
      detail: { error: error, boundaryId: boundaryId },
      bubbles: true
    }));
  }

  // Retry function (resets the boundary)
  function retry() {
    hasError = false;
    lastError = null;
    boundary.setAttribute('data-has-error', 'false');
    contentEl.style.display = '';
    fallbackEl.style.display = 'none';

    // Dispatch retry event
    boundary.dispatchEvent(new CustomEvent('stx:retry', {
      detail: { boundaryId: boundaryId },
      bubbles: true
    }));
  }

  // Expose retry function
  window['$stxRetry_' + boundaryId.replace(/-/g, '_')] = retry;

  // Make $retry available within the fallback
  fallbackEl.querySelectorAll('[onclick*="$retry"]').forEach(function(el) {
    const onclick = el.getAttribute('onclick');
    if (onclick) {
      el.setAttribute('onclick', onclick.replace(/\\$retry\\(\\)/g, "window['$stxRetry_" + boundaryId.replace(/-/g, '_') + "']()"));
    }
  });

  // Handle @click="$retry()" syntax
  fallbackEl.querySelectorAll('[\\\\@click*="$retry"]').forEach(function(el) {
    el.addEventListener('click', function(e) {
      e.preventDefault();
      retry();
    });
  });

  // Also check for data-onclick
  fallbackEl.querySelectorAll('[data-onclick*="$retry"]').forEach(function(el) {
    el.addEventListener('click', function(e) {
      e.preventDefault();
      retry();
    });
  });

  // Catch errors in content
  contentEl.addEventListener('error', function(e) {
    showFallback(e.error || new Error('Unknown error'));
  }, true);

  // Wrap global error handler for this boundary
  const originalOnError = window.onerror;
  window.onerror = function(message, source, lineno, colno, error) {
    // Check if error originated from within this boundary
    if (contentEl.contains(document.activeElement) || isErrorFromBoundary(error)) {
      showFallback(error || new Error(String(message)));
      return true; // Prevent default handling
    }
    if (originalOnError) {
      return originalOnError.apply(this, arguments);
    }
    return false;
  };

  // Helper to check if error is from this boundary (basic heuristic)
  function isErrorFromBoundary(error) {
    if (!error || !error.stack) return false;
    return error.stack.includes(boundaryId);
  }

  // Expose error info
  window.STX = window.STX || {};
  window.STX.errorBoundaries = window.STX.errorBoundaries || {};
  window.STX.errorBoundaries[boundaryId] = {
    hasError: function() { return hasError; },
    getError: function() { return lastError; },
    retry: retry,
    showFallback: showFallback
  };
})();
</script>
`}async function L(j,q,A){try{return await j()}catch(z){if(A)A(z instanceof Error?z:Error(String(z)));else console.error("[stx:errorBoundary] SSR Error:",z);return q}}function V(j,q={}){let A=q.onError?(z)=>q.onError(z,{timestamp:Date.now(),boundaryId:"ssr"}):void 0;return(z)=>L(z,j,A)}function v(){return`
/* STX Error Boundary Styles */
.stx-error-boundary {
  position: relative;
}

.stx-error-boundary[data-has-error="true"] .stx-error-boundary-content {
  display: none;
}

.stx-error-boundary[data-has-error="true"] .stx-error-boundary-fallback {
  display: block !important;
}

/* Default fallback styling (can be overridden) */
.stx-error-boundary-fallback {
  padding: 1rem;
  border: 1px solid #ef4444;
  border-radius: 0.5rem;
  background-color: #fef2f2;
  color: #991b1b;
}

.stx-error-boundary-fallback button {
  margin-top: 0.5rem;
  padding: 0.5rem 1rem;
  border: 1px solid #ef4444;
  border-radius: 0.25rem;
  background-color: #ef4444;
  color: white;
  cursor: pointer;
}

.stx-error-boundary-fallback button:hover {
  background-color: #dc2626;
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  .stx-error-boundary-fallback {
    background-color: #450a0a;
    color: #fecaca;
  }
}
`}function x(){return`
// STX Error Boundary Runtime
(function() {
  if (typeof window === 'undefined') return;

  window.STX = window.STX || {};
  window.STX.errorBoundaries = window.STX.errorBoundaries || {};

  // Create an error boundary programmatically
  window.STX.createErrorBoundary = function(element, options) {
    options = options || {};
    const boundaryId = 'eb-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2);

    let hasError = false;
    let lastError = null;
    let fallbackContent = options.fallback || '<p>Something went wrong</p>';

    const contentWrapper = document.createElement('div');
    contentWrapper.className = 'stx-error-boundary-content';

    const fallbackWrapper = document.createElement('div');
    fallbackWrapper.className = 'stx-error-boundary-fallback';
    fallbackWrapper.style.display = 'none';
    fallbackWrapper.innerHTML = fallbackContent;

    // Move element's children to content wrapper
    while (element.firstChild) {
      contentWrapper.appendChild(element.firstChild);
    }

    element.appendChild(contentWrapper);
    element.appendChild(fallbackWrapper);
    element.classList.add('stx-error-boundary');
    element.setAttribute('data-boundary-id', boundaryId);

    function showFallback(error) {
      hasError = true;
      lastError = error;
      element.setAttribute('data-has-error', 'true');
      contentWrapper.style.display = 'none';
      fallbackWrapper.style.display = '';
      if (options.onError) options.onError(error);
    }

    function retry() {
      hasError = false;
      lastError = null;
      element.setAttribute('data-has-error', 'false');
      contentWrapper.style.display = '';
      fallbackWrapper.style.display = 'none';
      if (options.onRetry) options.onRetry();
    }

    window.STX.errorBoundaries[boundaryId] = {
      hasError: function() { return hasError; },
      getError: function() { return lastError; },
      retry: retry,
      showFallback: showFallback
    };

    return window.STX.errorBoundaries[boundaryId];
  };

  console.log('[stx] Error boundary runtime initialized');
})();
`}var w=0;export{L as withErrorBoundary,G as processErrorBoundaryDirectives,x as generateErrorBoundaryRuntime,v as generateErrorBoundaryCSS,V as createErrorBoundary};
