import{t as e}from"./preload-helper.BbTAn5NJ.js";var t,n=!1,r=!1,i=`* `,a=800,o=e=>new Promise(t=>setTimeout(t,e)),s={reloaded:null,removeServiceWorker:null,installServiceWorker:null},c=navigator.userAgent.indexOf(`Safari`)>-1&&navigator.userAgent.indexOf(`Chrome`)===-1;function l(e){var t;if(e&&c)return!1;if(performance&&performance.getEntriesByType&&typeof performance.getEntriesByType==`function`){let e=performance.getEntriesByType(`navigation`);if(e&&e[0]&&e[0].type===`reload`)return!0}return!((t=window.performance)==null||(t=t.navigation)==null)&&t.type?window.performance.navigation.type===1:!1}s.reloaded=l;var u=0;async function d(e){if(n&&r){e(0,0);return}console.debug(!n&&!r?`•-• service worker: wait for service worker cache open and start...`:n?`•-• service worker: wait for service worker cache open...`:`•-• service worker: wait for service worker to start...`),e(await m(`cache-open`),u)}function f(e,t){`caches`in window?caches.keys().then(function(n){if(console.debug(`•-• clearing cache keys`,n),n.length===0){e?g(`•-• `,t,!1):t&&t();return}let r=0;for(let i of n)caches.delete(i).then(function(a){r++,console.log(`  •-• cache deleted`,i,`result`,a),r===n.length&&(console.log(`%c•-• all caches cleared`,`font-weight: bold`),e?g(`•-• `,t,!1):t&&t())})}).catch(function(n){console.warn(`•-• cache keys retrieval failed`,n),e?g(`•-• `,t,!1):t&&t()}):e?g(`•-• `,t,!1):t&&t()}function p(e,t){if(n=!1,r=!1,`serviceWorker`in navigator){let n=0,r=0;navigator.serviceWorker.getRegistrations().then(function(i){if(r=i.length,r<1)return f(e,t);for(let a of i)console.debug(`•-• uninstall service worker`,a),a.unregister().then(function(i){console.log(`  •-• unregister service worker done, result`,i),n++,n===r&&f(e,t)}).catch(function(i){console.warn(`•-• unregister service worker FAILED`,i),n++,n===r&&f(e,t)})}).catch(function(e){console.debug(`service worker registration failed: `,e),t&&t()})}else{let e=`* we are sorry, but service worker is not supported, you must use secure https:// on the server machine to use this page`;console.error(e),alert(e),t&&t()}}s.removeServiceWorker=p;async function m(e){let t=performance&&performance.now&&performance.now()/1e3||-1;for(navigator.serviceWorker.controller||await navigator.serviceWorker.ready;!(n&&r)&&u<a;)u++,!r&&(u===1||u%250==0)&&navigator.serviceWorker.controller&&(console.log(i+`service worker, post message: %c'${e}'`,`font-weight: bold`),navigator.serviceWorker.controller.postMessage({type:e})),await o(0);return t=performance.now()/1e3-t,t}function h(){`serviceWorker`in navigator&&navigator.serviceWorker.addEventListener(`message`,function(n){if(n.data.type===`cache-open`)r=n.data.result===!0,console.debug(`•-• service worker: cache-open message result: ${n.data.result}, loop: ${u}`);else if(n.data.type===`debug`)console.debug.apply(null,n.data.args);else if(n.data.type===`warn`)console.warn.apply(null,n.data.args);else if(n.data.type===`activate`)typeof n.data.result==`string`?console.debug(`•-• service worker install result: ${n.data.result}`):console.debug(`•-• service worker install result: ${JSON.stringify(n.data.result)}`);else{console.error(`•-• service worker install error: ${JSON.stringify(n.data.result)}`);async function r(){t==null&&(t=await e(()=>import(`./nc-reload.B2f1BAXz.js`),[])),t.onServiceWorkerMessage(n)}r()}})}h();function g(e,t,a){if(n){t&&t();return}if(`serviceWorker`in navigator){i=e||`•-• `,e&&console.debug(a===!1?i+`service worker was not installed, install service worker...`:i+`install service worker...`);let o=``;o.startsWith(`XX-base-path-`)&&(o=``),navigator.serviceWorker.register(o+`/sw.js`).then(async o=>{await navigator.serviceWorker.ready,n=!0,localStorage.getItem(`nc-reloaded`)?(localStorage.removeItem(`nc-reloaded`),console.debug(i+`service worker has been reloaded, scope`,o.scope)):((a||c)&&(r=!0),e&&(a?console.debug(i+`service worker was already installed, scope`,o.scope):r?console.log(`%c`+i+`service worker and cache have been installed, scope:`,`font-weight: bold`,o.scope):(console.log(`%c`+i+`service worker has been installed, scope:`,`font-weight: bold`,o.scope),r=!0))),r||await m(`cache-open`),t&&t()}).catch(function(e){let r=i+`service worker registration FAILED, error: ${e}`;console.error(r),alert(r),n=!0,t&&t()})}else{let e=`Service worker is not supported, you must use secure ${`https://`+window.location.host} or http://localhost on the server machine to use this page.`;console.error(e),alert(e),n=!0,t&&t()}}s.installServiceWorker=g;export{s as _,g as installServiceWorker,l as reloaded,p as removeServiceWorker,d as waitServiceWorker};