class TonicRaw{constructor(e){this.isTonicRaw=!0,this.rawText=e}valueOf(){return this.rawText}toString(){return this.rawText}}class Tonic extends window.HTMLElement{constructor(){super();const e=Tonic._states[this.id];delete Tonic._states[this.id],this.isTonicComponent=!0,this.state=e||{},this.props={},this.elements=[...this.children],this.elements.__children__=!0,this.nodes=[...this.childNodes],this.nodes.__children__=!0,this._events()}static _createId(){return`tonic${Tonic._index++}`}static _maybePromise(e){e&&"function"==typeof e.then&&"function"==typeof e.catch&&e.catch(e=>setTimeout(()=>{throw e},0))}static _splitName(e){return e.match(/[A-Z][a-z]*/g).join("-")}static _normalizeAttrs(e,t={}){return[...e].forEach(e=>t[e.name]=e.value),t}_events(){const e=Object.getOwnPropertyNames(window.HTMLElement.prototype);for(const t of this._props)-1!==e.indexOf("on"+t)&&this.addEventListener(t,this)}_prop(e){const t=this._id,s=`__${t}__${Tonic._createId()}__`;return Tonic._data[t]=Tonic._data[t]||{},Tonic._data[t][s]=e,s}_placehold(e){const t=this._id,s=`placehold:${t}:${Tonic._createId()}__`;return Tonic._children[t]=Tonic._children[t]||{},Tonic._children[t][s]=e,s}static match(e,t){return e.matches||(e=e.parentElement),e.matches(t)?e:e.closest(t)}static getPropertyNames(e){const t=[];for(;e&&e!==Tonic.prototype;)t.push(...Object.getOwnPropertyNames(e)),e=Object.getPrototypeOf(e);return t}static add(e,t){if(e.prototype._props=Tonic.getPropertyNames(e.prototype),!(t||e.name&&e.name.length>1))throw Error("Mangling. https://bit.ly/2TkJ6zP");t||(t=Tonic._splitName(e.name).toLowerCase()),window.customElements.get(t)||(Tonic._reg[t]=e,Tonic._tags=Object.keys(Tonic._reg).join(),window.customElements.define(t,e),e.stylesheet&&Tonic.registerStyles(e.stylesheet))}static registerStyles(e){if(Tonic._stylesheetRegistry.includes(e))return;Tonic._stylesheetRegistry.push(e);const t=document.createElement("style");t.appendChild(document.createTextNode(e())),document.head&&document.head.appendChild(t)}static escape(e){return e.replace(Tonic.ESC,e=>Tonic.MAP[e])}static raw(e){return new TonicRaw(e)}html([e,...t],...s){const n=s.map(e=>{if(e&&e.__children__)return this._placehold(e);if(e&&e.isTonicRaw)return e.rawText;switch(Object.prototype.toString.call(e)){case"[object HTMLCollection]":case"[object NodeList]":return this._placehold([...e]);case"[object Array]":case"[object Object]":case"[object Function]":return this._prop(e);case"[object NamedNodeMap]":return this._prop(Tonic._normalizeAttrs(e));case"[object Number]":return`${e}__float`;case"[object String]":return Tonic.escape(e);case"[object Boolean]":return`${e}__boolean`;case"[object Null]":return`${e}__null`;case"[object HTMLElement]":return this._placehold([e])}return"object"==typeof e&&e&&1===e.nodeType&&"function"==typeof e.cloneNode?this._placehold([e]):e}).reduce((e,s)=>e.concat(s,t.shift()),[e]).join("");return Tonic.raw(n)}setState(e){this.state="function"==typeof e?e(this.state):e}getState(){return this.state}scheduleReRender(e){return this.pendingReRender?this.pendingReRender:(this.pendingReRender=new Promise(t=>{window.requestAnimationFrame(()=>{const s=this._set(this.root,this.render);this.pendingReRender=null,s&&s.then?Tonic._maybePromise(s.then(()=>{this.updated&&this.updated(e),t()})):(this.updated&&this.updated(e),t())})}),this.pendingReRender)}reRender(e=this.props){const t={...this.props};return this.props="function"==typeof e?e(t):e,this.scheduleReRender(t)}getProps(){return this.props}handleEvent(e){Tonic._maybePromise(this[e.type](e))}_drainIterator(e,t){return t.next().then(s=>{if(this._set(e,null,s.value),!s.done)return this._drainIterator(e,t)})}_set(e,t,s=""){for(const t of e.querySelectorAll(Tonic._tags))t.isTonicComponent&&t.id&&Tonic._refIds.includes(t.id)&&(Tonic._states[t.id]=t.getState());if(t instanceof Tonic.AsyncFunction){return(t.call(this)||"").then(t=>this._apply(e,t))}if(t instanceof Tonic.AsyncFunctionGenerator){const s=t.call(this);return this._drainIterator(e,s)}return t instanceof Function?(s=t.call(this)||"",this._apply(e,s)):this._apply(e,s)}_apply(e,t){if(t&&t.isTonicRaw&&(t=t.rawText),"string"==typeof t){if(t=t.replace(Tonic.SPREAD,(e,t)=>{const s=Tonic._data[t.split("__")[1]][t];return Object.entries(s).map(([e,t])=>{const s=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase();return!0===t?s:t?`${s}="${Tonic.escape(String(t))}"`:""}).filter(Boolean).join(" ")}),this.stylesheet&&(t=`${t}`),e.innerHTML=t,this.styles){const t=this.styles();for(const s of e.querySelectorAll("[styles]"))for(const e of s.getAttribute("styles").split(/\s+/))Object.assign(s.style,t[e.trim()])}const s=Tonic._children[this._id]||{},n=(e,t)=>{if(3===e.nodeType){const n=e.textContent.trim();s[n]&&t(e,s[n])}const i=e.childNodes;if(i)for(let e=0;e{for(const s of t)e.parentNode.insertBefore(s,e);delete Tonic._children[this._id][e.id],e.parentNode.removeChild(e)})}else e.innerHTML="",e.appendChild(t.cloneNode(!0))}connectedCallback(){this.root=this.shadowRoot||this,this.wrap&&(this.wrapped=this.render,this.render=this.wrap),this.id&&!Tonic._refIds.includes(this.id)&&Tonic._refIds.push(this.id);const e=e=>e.replace(/-(.)/g,(e,t)=>t.toUpperCase());for(const{name:t,value:s}of this.attributes){const n=e(t),i=this.props[n]=s;if(/__\w+__\w+__/.test(i)){const{1:e}=i.split("__");this.props[n]=Tonic._data[e][i]}else if(/\d+__float/.test(i))this.props[n]=parseFloat(i,10);else if("null__null"===i)this.props[n]=null;else if(/\w+__boolean/.test(i))this.props[n]=i.includes("true");else if(/placehold:\w+:\w+__/.test(i)){const{1:e}=i.split(":");this.props[n]=Tonic._children[e][i][0]}}this.props=Object.assign(this.defaults?this.defaults():{},this.props),this._source?this.innerHTML=this._source:this._source=this.innerHTML,this._id=this._id||Tonic._createId(),this.willConnect&&this.willConnect(),Tonic._maybePromise(this._set(this.root,this.render)),Tonic._maybePromise(this.connected&&this.connected())}disconnectedCallback(){Tonic._maybePromise(this.disconnected&&this.disconnected()),this.elements.length=0,this.nodes.length=0,delete Tonic._data[this._id],delete Tonic._children[this._id]}}Object.assign(Tonic,{_tags:"",_refIds:[],_data:{},_states:{},_children:{},_reg:{},_stylesheetRegistry:[],_index:0,version:"undefined"!=typeof require?require("./package").version:null,SPREAD:/\.\.\.\s?(__\w+__\w+__)/g,ESC:/["&'<>`]/g,AsyncFunctionGenerator:async function*(){}.constructor,AsyncFunction:async function(){}.constructor,MAP:{'"':""","&":"&","'":"'","<":"<",">":">","`":"`"}}),"object"==typeof module&&(module.exports=Tonic);