// @bun
var{defineProperty:F,getOwnPropertyNames:L,getOwnPropertyDescriptor:M}=Object,N=Object.prototype.hasOwnProperty;function Q(j){return this[j]}var O=(j)=>{var k=(G??=new WeakMap).get(j),q;if(k)return k;if(k=F({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var z of L(j))if(!N.call(k,z))F(k,z,{get:Q.bind(j,z),enumerable:!(q=M(j,z))||q.enumerable})}return G.set(j,k),k},G;var T=(j)=>j;function V(j,k){this[j]=T.bind(null,k)}var R=(j,k)=>{for(var q in k)F(j,q,{get:k[q],enumerable:!0,configurable:!0,set:V.bind(k,q)})};var X=(j,k)=>()=>(j&&(k=j(j=0)),k);var U=import.meta.require;function Y(j,k){let q=k.pwa?.manifest?.themeColor||"#4f46e5";return`<div class="stx-pwa-install" style="display:none;">
  ${j}
</div>
<script>
(function() {
  let deferredPrompt;
  const installContainer = document.querySelector('.stx-pwa-install');
  const installBtn = installContainer?.querySelector('button, [data-pwa-install]') || installContainer;

  window.addEventListener('beforeinstallprompt', (e) => {
    e.preventDefault();
    deferredPrompt = e;
    if (installContainer) {
      installContainer.style.display = 'block';
    }
  });

  if (installBtn) {
    installBtn.addEventListener('click', async () => {
      if (!deferredPrompt) return;
      deferredPrompt.prompt();
      const { outcome } = await deferredPrompt.userChoice;
      console.log('[PWA] Install prompt outcome:', outcome);
      deferredPrompt = null;
      if (installContainer) {
        installContainer.style.display = 'none';
      }
    });
  }

  window.addEventListener('appinstalled', () => {
    console.log('[PWA] App installed');
    deferredPrompt = null;
    if (installContainer) {
      installContainer.style.display = 'none';
    }
  });

  // Check if already installed
  if (window.matchMedia('(display-mode: standalone)').matches) {
    if (installContainer) {
      installContainer.style.display = 'none';
    }
  }
})();
</script>`}function Z(j,k){let q=k.pwa?.updates,z=q?.message||"A new version is available. Reload to update?",E=q?.autoReload||!1,H=j.trim().length>0,J=`
<div class="stx-pwa-update-banner" style="
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
  z-index: 99999;
  transform: translateY(100%);
  transition: transform 0.3s ease;
">
  <span style="font-size: 14px;">${z}</span>
  <div style="display: flex; gap: 12px;">
    <button data-pwa-dismiss style="
      background: transparent;
      border: 1px solid rgba(255,255,255,0.5);
      color: white;
      padding: 8px 16px;
      border-radius: 6px;
      cursor: pointer;
      font-size: 14px;
    ">Later</button>
    <button data-pwa-reload style="
      background: white;
      border: none;
      color: #667eea;
      padding: 8px 16px;
      border-radius: 6px;
      cursor: pointer;
      font-weight: 600;
      font-size: 14px;
    ">Update Now</button>
  </div>
</div>`;return`${H?`<div class="stx-pwa-update" style="display:none;">${j}</div>`:J}
<script>
(function() {
  const updateContainer = document.querySelector('.stx-pwa-update, .stx-pwa-update-banner');
  const dismissBtn = updateContainer?.querySelector('[data-pwa-dismiss]');
  const reloadBtn = updateContainer?.querySelector('[data-pwa-reload], button');
  const autoReload = ${E};
  let newWorker = null;

  function showUpdate() {
    if (updateContainer) {
      if (updateContainer.classList.contains('stx-pwa-update-banner')) {
        updateContainer.style.transform = 'translateY(0)';
      }
else {
        updateContainer.style.display = 'block';
      }
    }
    if (autoReload) {
      setTimeout(() => window.location.reload(), 1000);
    }
  }

  function hideUpdate() {
    if (updateContainer) {
      if (updateContainer.classList.contains('stx-pwa-update-banner')) {
        updateContainer.style.transform = 'translateY(100%)';
      }
else {
        updateContainer.style.display = 'none';
      }
    }
  }

  if (dismissBtn) {
    dismissBtn.addEventListener('click', hideUpdate);
  }

  if (reloadBtn) {
    reloadBtn.addEventListener('click', () => {
      if (newWorker) {
        newWorker.postMessage({ type: 'SKIP_WAITING' });
      }
      window.location.reload();
    });
  }

  // Listen for service worker updates
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.ready.then((registration) => {
      registration.addEventListener('updatefound', () => {
        newWorker = registration.installing;
        if (newWorker) {
          newWorker.addEventListener('statechange', () => {
            if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
              showUpdate();
            }
          });
        }
      });
    });

    // Handle controller change (after skipWaiting)
    navigator.serviceWorker.addEventListener('controllerchange', () => {
      window.location.reload();
    });
  }

  // Also listen for custom event
  window.addEventListener('pwa-update-available', showUpdate);
})();
</script>`}function _(j,k){let q=j.trim().length>0,z=`
<div class="stx-offline-indicator" style="
  position: fixed;
  top: 16px;
  right: 16px;
  background: #ef4444;
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
  z-index: 99999;
  display: none;
  align-items: center;
  gap: 8px;
">
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
    <path d="M1 1l22 22M16.72 11.06A10.94 10.94 0 0 1 19 12.55M5 12.55a10.94 10.94 0 0 1 5.17-2.39M10.71 5.05A16 16 0 0 1 22.58 9M1.42 9a15.91 15.91 0 0 1 4.7-2.88M8.53 16.11a6 6 0 0 1 6.95 0"/>
    <circle cx="12" cy="20" r="1"/>
  </svg>
  <span>You are offline</span>
</div>`;return`${q?`<div class="stx-offline-indicator" style="display:none;">${j}</div>`:`
<div class="stx-offline-indicator" style="
  position: fixed;
  top: 16px;
  right: 16px;
  background: #ef4444;
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
  z-index: 99999;
  display: none;
  align-items: center;
  gap: 8px;
">
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
    <path d="M1 1l22 22M16.72 11.06A10.94 10.94 0 0 1 19 12.55M5 12.55a10.94 10.94 0 0 1 5.17-2.39M10.71 5.05A16 16 0 0 1 22.58 9M1.42 9a15.91 15.91 0 0 1 4.7-2.88M8.53 16.11a6 6 0 0 1 6.95 0"/>
    <circle cx="12" cy="20" r="1"/>
  </svg>
  <span>You are offline</span>
</div>`}
<script>
(function() {
  const indicator = document.querySelector('.stx-offline-indicator');

  function updateStatus() {
    if (indicator) {
      indicator.style.display = navigator.onLine ? 'none' : 'flex';
    }
  }

  window.addEventListener('online', updateStatus);
  window.addEventListener('offline', updateStatus);
  updateStatus();
})();
</script>`}function $(j,k){let q=k.pwa?.push,z=q?.vapidPublicKey||"",E=q?.subscriptionEndpoint||"/api/push/subscribe";return`<div class="stx-pwa-push" style="display:none;">
  ${j}
</div>
<script>
(function() {
  const pushContainer = document.querySelector('.stx-pwa-push');
  const pushBtn = pushContainer?.querySelector('button, [data-pwa-push]') || pushContainer;
  const vapidKey = '${z}';
  const endpoint = '${E}';

  async function checkPushSupport() {
    if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
      console.log('[PWA] Push notifications not supported');
      return false;
    }
    return true;
  }

  async function subscribeToPush() {
    try {
      const registration = await navigator.serviceWorker.ready;

      // Check existing subscription
      let subscription = await registration.pushManager.getSubscription();

      if (!subscription && vapidKey) {
        // Convert VAPID key
        const urlBase64ToUint8Array = (base64String) => {
          const padding = '='.repeat((4 - base64String.length % 4) % 4);
          const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
          const rawData = window.atob(base64);
          const outputArray = new Uint8Array(rawData.length);
          for (let i = 0; i < rawData.length; ++i) {
            outputArray[i] = rawData.charCodeAt(i);
          }
          return outputArray;
        };

        subscription = await registration.pushManager.subscribe({
          userVisibleOnly: true,
          applicationServerKey: urlBase64ToUint8Array(vapidKey)
        });

        // Send subscription to server
        if (endpoint) {
          await fetch(endpoint, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(subscription)
          });
        }

        console.log('[PWA] Push subscription created');
        return subscription;
      }

      return subscription;
    }
catch (error) {
      console.error('[PWA] Push subscription failed:', error);
      return null;
    }
  }

  async function init() {
    if (await checkPushSupport()) {
      const permission = await Notification.requestPermission();
      if (permission === 'granted' && pushContainer) {
        pushContainer.style.display = 'block';
      }
    }
  }

  if (pushBtn) {
    pushBtn.addEventListener('click', subscribeToPush);
  }

  // Check support on load
  if (pushContainer) {
    init();
  }
})();
</script>`}function P(j){return[...j,...S]}var y,A,B,K,S;var W=X(()=>{y={name:"pwa.installButton",handler:(j,k,q,z)=>{let E=q.__stx_options||{};return Y(j,E)},hasEndTag:!0,description:"Renders a PWA install prompt button that appears when the app can be installed"},A={name:"pwa.updatePrompt",handler:(j,k,q,z)=>{let E=q.__stx_options||{};return Z(j,E)},hasEndTag:!0,description:"Renders a PWA update notification UI when a new version is available"},B={name:"pwa.offlineIndicator",handler:(j,k,q,z)=>{let E=q.__stx_options||{};return _(j,E)},hasEndTag:!0,description:"Renders an offline status indicator that shows when the user loses connection"},K={name:"pwa.pushSubscribe",handler:(j,k,q,z)=>{let E=q.__stx_options||{};return $(j,E)},hasEndTag:!0,description:"Renders a push notification subscription button"},S=[y,A,B,K]});W();export{P as registerPwaDirectives,A as pwaUpdateDirective,K as pwaPushDirective,B as pwaOfflineDirective,y as pwaInstallDirective,S as pwaDirectives};
