System.register([], function (_export, _context) {
  "use strict";

  var resolveLoaded, whenLoaded, mybase, externalSite, root, serviceworkerReady, onReady;
  return {
    setters: [],
    execute: function () {
      _recorder_._src_client_load_swx_js = _recorder_._src_client_load_swx_js || {};
      resolveLoaded = _recorder_._src_client_load_swx_js.resolveLoaded;
      _recorder_._src_client_load_swx_js.resolveLoaded = resolveLoaded;

      _export("whenLoaded", whenLoaded = new Promise(resolve => {
        _recorder_._src_client_load_swx_js.resolveLoaded = resolve;
      }));

      _export("whenLoaded", whenLoaded);

      _recorder_._src_client_load_swx_js.whenLoaded = whenLoaded;
      mybase = window.location.host + window.location.pathname.replace(/\/[^/]+$/, "");
      _recorder_._src_client_load_swx_js.mybase = mybase;
      externalSite = !lively4url.match(_recorder_._src_client_load_swx_js.mybase);
      _recorder_._src_client_load_swx_js.externalSite = externalSite;
      // I am not somewhere below lively4url

      if (document.location.search.match("noserviceworker")) {
        _recorder_._src_client_load_swx_js.externalSite = true;
      }

      // service worker, only works on localhost or HTTPS
      if (window.location.protocol == "http:" && window.location.hostname != "localhost") {
        _recorder_._src_client_load_swx_js.externalSite = true;
      }

      if ('serviceWorker' in navigator || window.lively4chrome || _recorder_._src_client_load_swx_js.externalSite) {
        console.log("LOAD Lively4: boot lively4 service worker");
        root = "" + lively4url + "/";
        _recorder_._src_client_load_swx_js.root = root;
        serviceworkerReady = false;
        _recorder_._src_client_load_swx_js.serviceworkerReady = serviceworkerReady;

        onReady = function () {
          _recorder_._src_client_load_swx_js.serviceworkerReady = true;
          // Lively has all the dependencies


          // Workaround because only one function can listen to serviceworker messages
          window.serviceWorkerMessageHandlers = {};

          if (window.navigator.serviceWorker) {
            window.navigator.serviceWorker.onmessage = function (event) {
              for (let key in window.serviceWorkerMessageHandlers) {
                window.serviceWorkerMessageHandlers[key](event);
              }
            };

            // Add listener for serviceWorker messages
            window.serviceWorkerMessageHandlers['networkNotifications'] = event => {
              const message = event.data;

              // Only handle notifications here
              if (message.type != 'notification') return;

              let messageColors = {
                'info': '',
                'warning': 'yellow',
                'error': 'red'
              };

              if ('lively' in window) {
                lively.notify('ServiceWorker', message.data, 5, null, messageColors[message.command]);
              }
            };

            // Add listener for offline/online events
            // This is currently only used in the ServiceWorker, but the ServiceWorker does not get these events
            // So we register here and forward the events
            window.addEventListener('online', () => {
              navigator.serviceWorker.controller.postMessage({
                type: 'network',
                command: 'online'
              });
            });

            window.addEventListener('offline', () => {
              navigator.serviceWorker.controller.postMessage({
                type: 'network',
                command: 'offline'
              });
            });
          }

          _recorder_._src_client_load_swx_js.resolveLoaded();
        };

        _recorder_._src_client_load_swx_js.onReady = onReady;


        if ('serviceWorker' in navigator && navigator.serviceWorker.controller || window.lively4chrome || _recorder_._src_client_load_swx_js.externalSite) {

          console.log("NO Service worker during migration!!!");
          if (window.lively4chrome) {
            console.log("[Livley4] running without service worker");
          } else if (_recorder_._src_client_load_swx_js.externalSite) {
            console.log("[Lively4] load from external site");
          } else {
            console.log("[Lively4] Use existing service worker");
          }
          // we don't have to do anything here... the service worker is already there
          _recorder_._src_client_load_swx_js.onReady();
        } else {
          // the scope of the serviceworker can only be refined to something below it's root... so we have to install it as a file at the content's side. 
          navigator.serviceWorker.register(new URL('swx-loader.js', window.location)).then(function (registration) {
            console.log("SWX registration", registration);
            window.lively4swxregistration = registration; // for debugging
            // Registration was successful
            console.log('ServiceWorker registration successful with scope: ', registration.scope);

            // so now we have to reload!
            console.log("ok... lets wait for the service worker.");
            // console.log("Lively4 ServiceWorker installed! Reboot needed! ;-)")
            // window.location = window.location
          }).catch(function (err) {
            // registration failed
            console.log('ServiceWorker registration failed: ', err);
          });
          navigator.serviceWorker.ready.then(_recorder_._src_client_load_swx_js.onReady);
        }
      }

      document.addEventListener('DOMContentLoaded', function () {
        if (Notification.permission !== "granted") Notification.requestPermission();
      });
    }
  };
});