(function () {
  'use strict';

  if ('serviceWorker' in navigator) {
    window.addEventListener('load', function () {
      navigator.serviceWorker.register('/steps/public/sw.js').catch(function (err) {
        console.warn('Pyrk service worker registration failed:', err);
      });
    });
  }

  let deferredPrompt = null;
  window.addEventListener('beforeinstallprompt', function (event) {
    event.preventDefault();
    deferredPrompt = event;
    document.documentElement.classList.add('pyrk-can-install');
  });

  window.PyrkPwa = {
    canInstall: function () { return !!deferredPrompt; },
    install: async function () {
      if (!deferredPrompt) return false;
      deferredPrompt.prompt();
      const choice = await deferredPrompt.userChoice;
      deferredPrompt = null;
      document.documentElement.classList.remove('pyrk-can-install');
      return choice && choice.outcome === 'accepted';
    }
  };
})();
