/**
 * Copyright 2020 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 *
 */
import"../dynamic-import-registry/dynamic-import-registry.js";globalThis.WCAutoload=globalThis.WCAutoload||{},globalThis.WCAutoloadRegistry=globalThis.WCAutoloadRegistry||{},globalThis.WCAutoload.requestAvailability=()=>(globalThis.WCAutoload.instance||(globalThis.WCAutoload.instance=globalThis.document.createElement("wc-autoload"),globalThis.document.body.appendChild(globalThis.WCAutoload.instance)),globalThis.WCAutoload.instance);const fetch_retry=async(t,e,o)=>{for(let a=0;a<o;a++)try{return await fetch(t,e)}catch(t){if(a+1===o)throw t}};globalThis.WCAutoload.process=t=>new Promise(((t,e)=>{var o=globalThis.WCAutoload.requestAvailability();o.loaded=!0;var a={};if(globalThis.WCAutoloadRegistryFileProcessed){let e=document;o.target&&(e=o.target,o.processNewElement(e)),e.querySelectorAll("*").forEach((t=>{t.tagName&&!a[t.tagName]&&(o.processNewElement(t),a[t.tagName]=t.tagName)})),t("autoloader already processed")}else setTimeout((async()=>{if(globalThis.WCAutoloadBasePath?o.registry.basePath=globalThis.WCAutoloadBasePath:globalThis.WCGlobalBasePath&&(o.registry.basePath=globalThis.WCGlobalBasePath),globalThis.WCAutoloadRegistryFile&&!globalThis.WCAutoloadRegistryFileProcessed){"string"==typeof globalThis.WCAutoloadRegistryFile&&(globalThis.WCAutoloadRegistryFile=[globalThis.WCAutoloadRegistryFile]);for(var e=0;e<globalThis.WCAutoloadRegistryFile.length;e++)await fetch_retry(globalThis.WCAutoloadRegistryFile[e],{priority:"high"},3).then((function(t){return t.json()})).then((function(t){globalThis.WCAutoloadRegistryFileProcessed=!0,globalThis.WCAutoloadRegistry={...globalThis.WCAutoloadRegistry,...t}}))}if(globalThis.WCAutoloadRegistry)for(var e in globalThis.WCAutoloadRegistry)o.registry.register({tag:e,path:globalThis.WCAutoloadRegistry[e]});let l=document;o.target&&(l=o.target,o.processNewElement(l)),l.querySelectorAll("*").forEach((t=>{t.tagName&&!a[t.tagName]&&(o.processNewElement(t),a[t.tagName]=t.tagName)})),t("autoloader processed on the fly")}),0)})),globalThis.addEventListener("load",globalThis.WCAutoload.process),globalThis.WCAutoload.postLoaded=t=>{setTimeout((()=>{let e=globalThis.WCAutoload.requestAvailability();e.loaded&&globalThis.document.querySelectorAll(t.detail.tag).length>0&&e.registry.loadDefinition(t.detail.tag)}),0)},globalThis.addEventListener("dynamic-import-registry--new-registration",globalThis.WCAutoload.postLoaded);class WcRegistry extends HTMLElement{static get tag(){return"wc-registry"}constructor(){super(),this.loader=globalThis.WCAutoload.requestAvailability()}connectedCallback(){setTimeout((()=>{if(this.children.length>0&&"TEMPLATE"==this.children[0].tagName)try{let e=JSON.parse(this.children[0].content.textContent);for(var t in e)this.loader.registry.register({tag:t,path:e[t]})}catch(t){console.warn(t)}}),0)}}customElements.define(WcRegistry.tag,WcRegistry);class WcAutoload extends HTMLElement{static get tag(){return"wc-autoload"}constructor(){super(),this.loaded=!1,this.registry=globalThis.DynamicImportRegistry.requestAvailability(),this.options={childList:!0,subtree:!0}}connectedCallback(){this._mutationObserver=new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{this.processNewElement(t)}))}))})),globalThis.WCAutoloadOptions&&(this.options=globalThis.WCAutoloadOptions),setTimeout((()=>{globalThis.WCAutoloadTarget?this.target=globalThis.WCAutoloadTarget:this.target=globalThis.document.body,this._mutationObserver.observe(this.target,this.options)}),0)}disconnectedCallback(){this._mutationObserver.disconnect()}processNewElement(t){t.tagName&&t.tagName.includes("-")&&"DYNAMIC-IMPORT-REGISTRY"!=t.tagName&&"WC-REGISTRY"!=t.tagName&&"WC-AUTOLOAD"!=t.tagName&&this.registry.loadDefinition(t.tagName)}}customElements.define(WcAutoload.tag,WcAutoload);export{WcAutoload};