(function(){"use strict";var e,t;if(typeof window==="undefined"){e=require("jsdom").JSDOM;t=new e(``).window}else{t=window}var n=t.document;function r(e,t,i,s,l=t,a=e){Object.defineProperty(t,"view",{enumerable:false,configurable:true,writable:true,value:l});Object.assign(i,{$source:e,$target:t,$template:a.children?a.children.length>1?a.children:a.children[0]:a,$view:l.children.length>1?l.children:l.children[0]});if(typeof e==="string"){const n=d(e,i,s)+"",o=(new DOMParser).parseFromString(n,"text/html"),c=E(o.body.childNodes);if(c.some(e=>e.nodeType===1)){r(C(o.body),o.body,i,s,l,a);let e;if(t.nodeType===3){e=t.parentElement;e.removeChild(t)}else if(t.nodeType===1){e=t;while(e.lastChild)e.removeChild(e.lastChild)}E(o.body.childNodes).forEach(t=>e.appendChild(t))}else if(t.data!==n){t.data=n}return l}t.view.template=t.view.innerHTML;if(e.tagName!==t.tagName){if(t.parentNode){const r=n.createElement(e.tagName);t.parentNode.replaceChild(r,t);t=r}}E(t.attributes).forEach(n=>e.attributes[n.name]!==null||t.removeAttribute(n.name));let c;Object.keys(e.attributes).forEach(f=>{let u=e.attributes[f],p=typeof u,h=o;o=t;const m=d(f.split(":")[0],i,s),b=x[m]||L.directives[m];u=d(u,i,s);o=h;p=typeof u;if(u==null){t.removeAttribute(f);delete t[f]}else if(["boolean","number","string"].includes(p)){if(u!==t.getAttribute(f)){try{t.setAttribute(f,u);if(f==="value")t.value=u}catch(e){}}}else{const e=j(f);if(u!==t[e])t[e]=u}if(b){while(t.lastChild)t.removeChild(t.lastChild);const o=(i,o)=>{e.children.forEach(e=>{if(typeof e==="string")t.appendChild(new Text(e));else t.appendChild(n.createElement(e.tagName));r(e,t.lastChild,i,o,l,a)});return t};const p=b(u,i,s,o,{element:t,raw:f,resolved:f.indexOf("${")>=0?d(f,i,s):f}),h=typeof p;if(p==null){t.parentElement.removeChild(t)}else if(h==="string"){c=true;t.innerHTML=p}else if(h==="object"&&p instanceof HTMLElement){c=true;if(p!==t)t.parentElement.replaceChild(p,t)}}});if(!c){let o=t.childNodes.length-e.children.length;while(o-- >0)!t.lastChild||t.removeChild(t.lastChild);let c=E(t.childNodes,0,e.children.length);if(c.length===0){const n=t.shadowRoot;if(n)c=E(n.childNodes,0,e.children.length)}e.children.forEach((e,o)=>{if(o<c.length){r(e,c[o],i,s,l,a);return}if(typeof e==="string")t.appendChild(new Text);else t.appendChild(n.createElement(e.tagName));r(e,t.lastChild,i,s,l,a)})}return l}function i(){}i.prototype=new Function;let o,s;const l=new Map,a=(e,t,n={})=>{const r=Object.values(n).reduce((e,[t,n])=>e+=` ${t}="${n}"`,"");return`<${t}${r}>${e}</${t}>`},c=e=>{if(e.message.indexOf("not defined")>=0){const t=e.message.split(" ")[0];try{return Function(`return ${t}`)()}catch(e){return t}}},f=(e,...t)=>t,u=e=>{try{const t=new URL(e);if(t.pathname)return true;return false}catch(e){return false}},d=(e,t,n)=>{if(typeof e==="string"&&e.indexOf("${")>=0){const r={},i=e.lastIndexOf("${")===0&&e[e.length-1]==="}";while(true){try{e=Function("scope","actions","extras","__interpolate","with(scope.__t_model||{}) { with(scope) { with(actions) { with(extras) { return "+(i?"__interpolate":"")+"`"+e+"`}}}}")(t||{},n||{},r,f);if(i)e=e[0];break}catch(e){const n=c(e);if(!n)throw e;t.__t_model[n];r[n]=""}}}return e},p=(e,t)=>{t=Object.assign(e,t);Object.keys(t).forEach(n=>{if(t[n]===undefined){delete e[n];delete t[n]}});return e},h=(e={},t={})=>{const n=Object.assign(Array.isArray(e)?[]:{},e);Object.keys(n).forEach(e=>{if(n[e]&&typeof n[e]==="object")n[e]=h(n[e],t[e])});const r=new Proxy(n,{get(e,t){if(t==="__isReactor__")return true;if(o){let n=l.get(e);if(!n){n={};l.set(e,n)}if(!n[t])n[t]=new Set;n[t].add(o)}return e[t]},set(e,n,i){const o=t[n]||t["*"],s=e[n];if(s===i)return true;if(o){(Array.isArray(o)?o:[o]).forEach(e=>t[n](s,i,n,r))}if(i&&typeof i==="object"&&!i.__isReactor__){i=h(i)}e[n]=i;const a=l.get(e);if(a){const e=s===undefined?Object.values(a):[a[n]],t=new Set;if(e){e.forEach(e=>{(e||[]).forEach(e=>{const r=e.render||(e.view?e.view.render:null);if(!t.has(e)&&e.parentElement&&r){r();t.add(e)}else if(s!==undefined){a[n].delete(e)}})})}}return true},deleteProperty(e,t){delete e[t];const n=l.get(e);if(n&&n[t]){n[t].forEach(e=>{const r=e.render||(e.view?e.view.render:null);if(e.parentElement&&r)r();else n[t].delete(e)})}return true}});return r},m=e=>{return t=>{const n=e[t.type]||e["*"];if(n)n(t)}},b=(e,t={})=>{const r=[].slice=n.querySelectorAll(e);r.forEach(e=>T(e,t))},g=(e,t={})=>{let{template:r,customElement:o,model:s,attributes:l,actions:a,controller:c,linkModel:f,reactive:u,lifecycle:d={},protect:m}=t;if(r&&o)throw new Error("Component can't take both template and customElement");if(!r&&!o)throw new Error("Component must have template or customElement");t=Object.assign({lifecycle:{}},t);if(!o){const t=e.split("-").map(e=>`${e[0].toUpperCase()+e.substring(1)}`).join("");o=Function("template",`return class ${t} extends HTMLElement { constructor() { super(); const shadow = this.attachShadow({mode: 'open'}); shadow.innerHTML = template.innerHTML||template; }}`)(r)}o.defaults={model:s,attributes:l,actions:a,controller:c,linkModel:f,reactive:u,lifecycle:d,protect:m};customElements.define(e,o,t.extends?{extends:t.extends}:undefined);const b=new i,g=function(t={}){const r=n.createElement(e),i=Object.assign({controller:c,linkModel:f,lifecycle:d,protect:m,reactive:u},t);i.model=p(JSON.parse(JSON.stringify(s||{})),t.model);if(i.reactive)i.model=h(i.model);i.attributes=p(Object.assign({},l),t.attributes);i.actions=p(Object.assign({},a),t.actions);Object.keys(i.attributes).forEach(e=>{const t=i.attributes[e];if(t==null)r.removeAttribute(O(e));else if(["boolean","number","string"].includes(typeof t))r.setAttribute(O(e),t);else r[j(e)]=t});if(d.beforeCreate)d.beforeCreate.call(r);L.view(r,i);if(d.created)d.created.call(r);return r};g.constructor=i;Object.setPrototypeOf(g,b);return g},y=e=>{const t=w.states.pop();if(t)t()},w=(e,t)=>{w.states.push(e);history.pushState({},"",t)},v=e=>{w.states||(w.states=[]);window.addEventListener("popstate",y,false);function t(t){Object.defineProperty(t,"stopRoute",{enumerable:false,configurable:true,writable:true,value:function(e){if(e)w(this.target.view.render,n);this.routeStopped=true}});const n=t.target.href,r=e=>t.stopRoute(e);if(n){const n=t.target;let i=n.pathname;if(n.protocol==="file:")i=i.substring(i.indexOf(":")+1);for(let o in e){let s=e[o];try{o=Function("return "+o)()}catch(e){}const l=typeof o;let a={};Object.defineProperty(a,"raw",{enumerable:false,value:[]});Object.defineProperty(a,"resolved",{enumerable:false,value:[]});if(l==="string"){const e=o.split("/"),t=i.split("/");if(e.length!==t.length)continue;let r;if(e.every((e,n)=>{if(e[0]===":"){const i=M(t[n],Object.assign({},M.options,{eval:true}));a.raw.push(e);a.resolved.push(i);a[e.substring(1)]=i;r="param";return true}if(e!==t[n])return false;if(e){if(a.raw.length===0||r==="param"){a.raw.push("/"+e);a.resolved.push("/"+e)}else{a.raw[a.raw.length-1]+="/"+e;a.resolved[a.resolved.length-1]+="/"+e}}return true})){if(n.search){const e=M(n.search).substring(1);a.raw.push(e);const t=e.split("&");if(t.length>0){const e=t.reduce((e,t)=>{const n=t.split("=");e[n[0]]=M(n[1],Object.assign({},M.options,{eval:true}));return e},{});a.query=e;a.resolved.push(e)}}}else{continue}}if(l==="string"||l==="function"&&(a=o(new URL(n.href)))||o instanceof RegExp&&o.test(i)&&(a=new URL(n.href))){s.call(t,a,r);t.preventDefault();if(t.routeStopped)break}}}}Object.defineProperty(t,"add",{enumerable:false,configurable:true,writable:true,value:(t,n)=>e[t]=n});e=Object.assign({},e);return t},E=e=>[].slice.call(e),j=e=>{const t=e.split("-");let n=t.shift(),r=t.shift();while(r){n+=r[0].toUpperCase()+r.substring(1);r=t.shift()}return n},O=e=>{let t="";for(let n=0;n<e.length;n++){const r=e[n];if(n>0&&r===r.toUpperCase())t+="-";t+=r}return t},C=(e,t,n=e.tagName)=>{if(e.nodeName==="#text")return e.textContent;const r={tagName:n,attributes:Object.assign({},t),children:[]};E(e.attributes).forEach(e=>r.attributes[e.name]=e.value);for(const t in e){if(t[0]==="o"&&t[1]==="n"){let n=e[t];const i=e.getAttribute(t);if(!n&&i){n=new Function("event",i)}if(n)r.attributes[t]=n}}E(e.childNodes).forEach(e=>r.children.push(C(e)));return r},T=function(e,i=e.constructor.defaults||{}){let{template:l,model:a={},attributes:c={},actions:f={},controller:d,linkModel:h=L.linkModel,lifecycle:m={},protect:b=s}=i;if(e.length&&e[0]!=null&&!e.options){if(typeof e==="string"){const t=[].slice.call(n.querySelectorAll(e));t.forEach(e=>T(e,i))}else{[].slice.call(e).forEach(e=>T(e,i))}return e}if(l){const t=typeof l;if(t==="object"){if(l.tagName==="TEMPLATE")l.innerText=l.innerHTML;else l=l.shadowRoot?l.shadowRoot:l.firstChild}else{const t=n.createElement(e.tagName),r=u(l),o=new Text(r?`loading ${l}...`:l);if(o[0]!=="#"){t.appendChild(o)}i=Object.assign({},i);delete i.template;if(r){fetch(l).then(e=>{if(e.ok)return e.text();throw new Error(`Error: ${o.data}`)}).then(t=>{e.innerHTML=t;T(e,i);return}).catch(t=>{e.innerHTML=t.message;return})}else if(l[0]==="#"){const t=n.getElementById(l.substring(0));e.innerHTML=t;return T(e,i)}l=t}}let g;const y={__t_model:a},w=l&&(l.tagName==="TEMPLATE"||l.tagName==="SCRIPT")?e.tagName:undefined,v=C(l||e,c,w),j=(e,...t)=>{return r=>{const i=e.split(".");let o=i.pop(),s,l=y.__t_model;while(s=i.shift()){l=l[s];if(!l)l[s]={}}l[o]=r.target.value;t.forEach(e=>n.querySelectorAll(e).forEach(e=>!e.render||e.render()))}},O=(t=y,n)=>{if(L.off||!e.parentElement)return;const i=o,s=t===y?y:{__t_model:t};if(n)p(a,t);o=e;if(g&&m.beforeUpdate)m.beforeUpdate.call(e);r(v,e,s,f);o=i;if(g&&m.updated)m.updated.call(e);return e};if(m.beforeMount)m.beforeMount.call(e);if(e.hasAttribute("defer"))e.removeAttribute("defer");else O(y);g=true;if(m.mounted)m.mounted.call(e);if(d){let{handleEvent:t,events:n,options:r=false}=d;if(typeof d==="function")t=d;if(n)n.forEach(n=>e.addEventListener(n,t,r));else{for(const t in e){if(t[0]==="o"&&t[1]==="n")e.addEventListener(t.substring(2),d,r)}}}else{Object.keys(i).forEach(t=>{if(t.startsWith("on")&&typeof i[t]==="function"){e.addEventListener(t.substring(2),i[t])}})}const A=e instanceof HTMLInputElement?[e]:E(e.querySelectorAll("input, textarea, select"));if([t.HTMLInputElement,t.HTMLSelectElement,t.HTMLTextAreaElement].some(t=>e instanceof t))A.push(e);A.forEach(e=>{if(b||e.hasAttribute("protect")){const t=e.setAttribute;let n=e.getAttribute("value");if(n==null||n[0]==="$"&&n[1]==="{"&&n[n.length-1]==="}")n="";e.setAttribute=function(r,i){if((b||this.hasAttribute("protect"))&&r==="value"&&i){if(i&&M(i)===undefined){i=n;e.setCustomValidity("Invalid input")}else{e.setCustomValidity("");n=i}}t.call(this,r,i)};e.addEventListener("input",t=>{if(t.target.value&&M(t.target.value)===undefined){e.setCustomValidity("Invalid input");e.value=n;t.preventDefault();return false}if(n!==t.target.value){e.setCustomValidity("");n=t.target.value}})}});if(h)A.forEach(t=>t.addEventListener(typeof h==="string"&&e.tagName!=="SELECT"?h:"change",e=>!t.validity.valid||j(t.name)(e),false));Object.entries({render:O,linkModel:j,model:a}).forEach(([t,n])=>Object.defineProperty(e,t,{enumerable:false,configurable:true,writable:true,value:n}));return e},M=(e,t=M.options)=>{e=t.coerce.reduce((e,t)=>t(e),e);if(t.accept.some(t=>t(e)))return e;if(t.reject.some(t=>t(e)))return;if(Array.isArray(e)){e.forEach((t,n)=>e[n]=M(e));return e}if(e&&typeof e==="object"){for(let t in e){const n=M(e[t]);if(n===undefined)delete e[t];else e[t]=n}return e}if(typeof e==="string"){e=t.escape.reduce((e,t)=>t(e),e);const n=this||typeof globalThis!=="undefined"?globalThis:typeof global!=="undefined"?global:{};if(t.eval&&!n[e.trim()]){try{return M(Function("return "+e).call(null))}catch(t){return e}}}return e},A=(e=window,t=(()=>""))=>{if(typeof window!=="undefined"&&window.prompt&&e===window){const e=window.prompt.bind(window);window.prompt=function(t){const n=e(t),r=M(n);if(r===undefined)window.alert("Invalid input: "+n);else return r};s=true;return}return e},x={"t-content":(e,t,n,r,{raw:i,resolved:o,element:s}={})=>{if(e!=null){s.removeAttribute("t-content");s.innerHTML=L.escape(e)}else{s.outerHTML=s.view.template}return s},"t-for":(e,t,n,r,{raw:i,resolved:o,element:s}={})=>{let[l,a,c]=o.split(":"),f=0;if(!c)c=e[Symbol.iterator]?"of":"in";if(c==="in"){for(let n in e){r(Object.assign({},t,{[a]:n,index:f}));f++}}else if(c==="of"){for(let n of e){r(Object.assign({},t,{[a]:n,index:f}));f++}}else throw new TypeError(`loop type must be 'in' or 'of' for ${i}`);return s},"t-foreach":(e,t,n,r,{element:i}={})=>{if(e[Symbol.iterator]){let i=0;const o={index:i++,iterable:e};if(Array.isArray(e))o.array=e;t=Object.assign({},t,o);for(const i of e){t.value=i;r(t,n)}}return i},"t-forvalues":(e,t,n,r,{element:i}={})=>{if(e&&typeof e==="object")(e.entries?e.entries():Object.entries(e)).forEach(([i,o],s)=>r(Object.assign({},t,{value:o,key:i,object:e}),n));return i},"t-if":(e,t,n,r,{element:i}={})=>e?r(t,n):""};M.options={coerce:[],accept:[e=>!e||["number","boolean"].includes(typeof e)],reject:[e=>typeof e==="function",e=>typeof e==="string"&&e.match(/<\s*\?\s*.*\s*/),e=>typeof e==="string"&&e.match(/(eval|alert|prompt|dialog|void|clean)\s*\(+/),e=>typeof e==="string"&&e.match(/url\s*\(/),e=>typeof e==="string"&&e.match(/console\.\s*.*\s*\(/),e=>typeof e==="string"&&e.match(/javascript:/),e=>typeof e==="string"&&e.match(/\(\s*.*\s*\)\s*.*\s*=>/),e=>typeof e==="string"&&e.match(/[Ff]unction\s*.*\s*\(\s*.*\s*\)\s*.*\s*\{\s*.*\s*\}\s*.*\s*\)\s*.*\s*\(\s*.*\s*\)/),e=>typeof e==="string"&&e.match(/Function\s*.*\s*\(\s*.*\s*\)\s*/)],escape:[e=>{if(typeof e==="string"&&e[0]==="?"){const t=e.substring(1).split("&");let n=t.length;return t.reduce((e,t,r)=>{const[i,o]=decodeURIComponent(t).split("=");let s=o!==undefined?M(o):M(i);if(s&&typeof s==="object")s=JSON.stringify(s);if(s!==undefined)e+=(o!==undefined?`${i}=${s}`:s)+(r<n-1?"&":"");else n--;return e},"?")}return e},e=>{if(typeof e==="string"&&e[0]!=="?"&&typeof n!=="undefined"){const t=n.createElement("div");t.appendChild(n.createTextNode(e));return t.innerHTML}return e}],eval:true};const L={component:g,reactor:h,view:T,define:b,router:v,handlers:m,escape:M,protect:A,resolve:d,JSDOM:e,directives:{},el:a};if(typeof module!=="undefined")module.exports=L;if(typeof window!=="undefined")window.tlx=L}).call(this);