import{LitElement as e,html as t,css as o}from"../../../lit/index.js";import{HAXElement as s,HAXWiring as r}from"../../hax-body-behaviors/hax-body-behaviors.js";import{varGet as i}from"../../utils/utils.js";import{HAXStore as a}from"./hax-store.js";class HaxAutoloader extends(s(e)){static get styles(){return[o`
        :host {
          display: none;
        }
      `]}render(){return t`<slot></slot>`}static get tag(){return"hax-autoloader"}static get properties(){return{...super.properties,processedList:{type:Object}}}constructor(){super(),this.processedList={}}firstUpdated(e){this.dispatchEvent(new CustomEvent("hax-register-core-piece",{bubbles:!0,cancelable:!0,composed:!0,detail:{piece:"haxAutoloader",object:this}}))}connectedCallback(){super.connectedCallback(),this.observer=new MutationObserver((e=>{e.forEach((e=>{e.addedNodes.forEach((e=>{this.processNewElements(e)}))}))})),this.observer.observe(this,{childList:!0})}disconnectedCallback(){this.observer.disconnect(),super.disconnectedCallback()}processNewElements(e){let t=this.childNodes;for(let e=0;e<t.length;e++){if(void 0!==t[e].tagName&&void 0===this.processedList[t[e].tagName])try{let o=t[e].tagName.toLowerCase();if("function"==typeof t[e].getHaxProperties){const s=new CustomEvent("hax-register-properties",{bubbles:!0,composed:!0,cancelable:!0,detail:{tag:o,properties:t[e].getHaxProperties(),polymer:!0}});context.dispatchEvent(s)}else if("function"==typeof t[e].HAXWiring){const s=new CustomEvent("hax-register-properties",{bubbles:!0,cancelable:!0,composed:!0,detail:{tag:o,properties:t[e].HAXWiring.getHaxProperties(),polymer:!1}});context.dispatchEvent(s)}else if(globalThis.customElements.get(o)){let e=globalThis.customElements.get(o);e?"function"==typeof e.getHaxProperties?this.setHaxProperties(e.getHaxProperties(),o):"function"==typeof e.HAXWiring?this.setHaxProperties(e.HAXWiring.getHaxProperties(),o):e.haxProperties?this.setHaxProperties(e.haxProperties,o):(console.warn(`${o} didn't have hax wiring so HAX guessed as best it can. See https://haxtheweb.org/documentation-1/hax-development/hax-schema for documentation on adding custom wiring for better UX.`),this.guessHaxWiring(o)):console.error(`${o} was not a valid custom element yet a load was attempted`)}else{let e;if(globalThis.WCAutoload&&globalThis.WCAutoload.requestAvailability()&&globalThis.WCAutoload.requestAvailability().registry.getPathToTag(o))e=globalThis.WCAutoload.requestAvailability().registry.getPathToTag(o);else{let t=i(a,`__appStoreData.autoloader.${o}.import`,i(a,`__appStoreData.autoloader.${o}`,`@haxtheweb/${o}/${o}.js`));e=`${new URL("./hax-autoloader.js",import.meta.url).href+"/../../../../"}${t}`}import(e).then((e=>{let t=globalThis.customElements.get(o);t?"function"==typeof t.getHaxProperties?this.setHaxProperties(t.getHaxProperties(),o):"function"==typeof t.HAXWiring?this.setHaxProperties(t.HAXWiring.getHaxProperties(),o):t.haxProperties?this.setHaxProperties(t.haxProperties,o):i(a,`__appStoreData.autoloader.${o}.haxProperties`,!1)?this.setHaxProperties(i(a,`__appStoreData.autoloader.${o}.haxProperties`,!1),o):(console.warn(`${o} didn't have hax wiring so HAX guessed as best it can. See https://haxtheweb.org/documentation-1/hax-development/hax-schema for documentation on adding custom wiring for better UX.`),this.guessHaxWiring(o)):console.error(`${o} was not a valid custom element yet a load was attempted`)})).catch((e=>{console.warn(e)}))}this.processedList[o]=o}catch(e){}t[e].remove()}}guessHaxWiring(e){try{let t=new r,o=t.prototypeHaxProperties();o.gizmo.title=e.replace("-"," "),o.gizmo.tags=["Other","undefined",e.replace("-"," "),e],o.gizmo.handles=[],o.settings.configure=[],o.settings.advanced=[],o.settings.developer=[],o=t.standardAdvancedProps(o,e),o.saveOptions={},o.demoSchema=[];let s={};if(customElements.get(e)&&(s=customElements.get(e).properties),s){for(let e in s)if(s[e].type&&s[e].type.name)switch(s[e].type.name){case"String":o.settings.configure.push({property:e,title:e,description:"",inputMethod:"textfield"});break;case"Number":o.settings.configure.push({property:e,title:e,description:"",inputMethod:"number"});break;case"Boolean":o.settings.configure.push({property:e,title:e,description:"",inputMethod:"boolean"})}}else{let t=globalThis.document.createElement(e).getAttributeNames();for(let e=0;e<t.length;e++)o.settings.configure.push({attribute:t[e],title:t[e],description:"",inputMethod:"textfield"})}t.readyToFireHAXSchema(e,o,this)}catch(e){console.warn("HAX failed to create wiring that worked")}}}customElements.define(HaxAutoloader.tag,HaxAutoloader);export{HaxAutoloader};