!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).lata={})}(this,(function(t){"use strict";class e{constructor(t,e=4){this.prefix=t,this.level=e}out(t,e="log",...o){if(t>this.level)return;if(!["log","warn","error"].includes(e))throw new Error(`Invalid type ('${e}')`);let n=[`[${(new Date).toISOString()}]`];this.prefix&&n.push(`[${this.prefix}]`),"error"===e&&n.push("ERROR"),n=[...n,...o],console[e](...n)}log(t,...e){this.out(t,"log",...e)}warn(t,...e){this.out(t,"warn",...e)}error(t,...e){this.out(t,"error",...e)}}class o{constructor(t="textContent"){this.target=t,this.isAttr=t.startsWith("@"),this.attrName=this.isAttr?t.substr(1):void 0}}function n(t){return t.replace(/\.([\d+])/g,"[$1]")}function r(t,e){if(""===e)return;let o,r=`return ${n(`obj.${e}`)}`;try{o=new Function("obj",r)(t)}catch(s){throw s}return o}function s(t,e,o){let i=function(t=""){if("string"!=typeof t)throw new Error("path must be a string");return""===t?null:t.split(".").slice(0,-1).join(".")}(e);null!==i&&function(t,e){if(!e.includes("."))return{};const o=e.split(".");let n="";const i={};for(let a=0,l=o.length-1;a1){const e=n.slice(0,n.length-1).join(".");r=h._get(t,e)}if(void 0===r)return!1;return n[n.length-1]in r},h.set=function(t,e,o,{silent:n=!1,force:r=!1}={}){if(!(t.__Mole instanceof h))throw new TypeError("Not a mole");r&&h.createParentsForPath(t,e);let s=`${h.pathToArraySyntax(`mole.${e}`)} = value;`;try{new Function("mole,value",s)(t,o)}catch(i){if(!n&&i instanceof TypeError)throw i}},h._set=function(t,e,o,{silent:n=!1,force:r=!1}={}){const s=h.onSetDisabled(t,!0);h.set(t,e,o,{silent:n,force:r}),h.onSetDisabled(t,s)},h.createParentsForPath=function(t,e){f(t),_(e);const o=e.split(".");let n,r=o.shift();for(;n=o.shift();)h.pathExists(t,r)||(n.match(/^\d+$/)?h.set(t,r,[]):h.set(t,r,{})),r=r+"."+n},h.pathToArraySyntax=function(t){const e=h.pathToDotSyntax(t).split("."),o=e.slice(1).map((t=>t.match(/^\d+$/)?`[${t}]`:"[`"+t+"`]"));return`${e[0]}${o.join("")}`},h._normalisePath=function(t){return t.replace(/\[['"`](.*?)['"`]\]/g,"[`$1`]")},h.pathToDotSyntax=function(t){return h._normalisePath(t).replace(/\[`?(.*?)`?\]/g,".$1")};class d{constructor(t){let e;if("string"==typeof t)e=this.parseSelector(t);else{if("object"!=typeof t)throw new Error("Not a string or an object");e=t}this.css=e.css,this.target=e.target,this.attrName=e.attrName}parseSelector(t){let e,[o,n]=t.split("/");return n||(n="textContent"),n&&n.startsWith("@")&&(e=n.substr(1)),{css:o,target:n,attrName:e}}toString(){return`${this.css}/${this.target}`}isAttr(){return this.target.startsWith("@")}attr(){return this.target.startsWith("@")?this.target.substr(1):null}}const b='\n//console.log(\'in EXPRESSION_FUNCTION_BODY()\');\n//console.log("arguments: ", arguments);\n//console.log("EXECFUNC this: ", this);\n//console.log("EXECFUNC __lata: ", __lata);\n//console.log("EXECFUNC __expr: ", __expr);\n//console.log("EXECFUNC window: ", window);\n//console.log("EXECFUNC console: ", console);\n//console.log("EXECFUNC __debug: ", __debug);\n//console.log("EXECFUNC $lata: ", $lata);\n//console.log("EXECFUNC Lata: ", Lata);\n//__debug.warn(4, "OOOOOOOOOOOOOOOOO");\n __expr = __expr.trim();\n if (__expr.length === 0) {\n __debug.warn(4, "Empty expression, skipping: \'" + __expr + "\'");\n return;\n }\n\n const hasDebug = (\'options\' in __lata) && (\'debugLevel\' in __lata.options);\n\n let res = null;\n const evalStr = \'return (\' + __expr + \')\';\n try {\n res = (new Function(\'__expr, __lata\', evalStr)).bind(this)(__expr, __lata);\n\n __debug.warn(4, "expression: ", __expr, " => ", res);\n } catch (e) {\n __debug.warn(4, "(3) Error when evaluating lata expression \'" + __expr + "\': " + e.message);\n }\n\n return res;\n',g=b;function y(t,e=b){const o=`return (new Function("${["__lata","__expr","__debug"].join(", ")}", fn)).bind(lata.proxy)`;return new Function("lata,fn",o)(t,e)}const w=/\bthis\.[a-z_$][a-z0-9._$]*/gi,x=/\.(\.|$)/;function E(t){const e=t.match(w);if(null===e)return[];let o=[...new Set(e.map((t=>t.substr(5))))];return o=o.filter((t=>!t.match(x))),o}const m=new e("lata",0),L=new e("exec",0);function A(t,e={},o={},{push:n=!0,pull:r=!1,auto:s=!1,parentLata:i,rootLata:a,internalPath:l=""}={}){const c={push:n,pull:r,auto:s};if(!this)return new A(t,e,o,c).proxy;m.warn(1,"Creating new Lata... *** el: ",t,", data: ",e,", tiesOrSelectors: ",o,", options: ",c),this._processConstructorArgs(t,e,o,{push:n,pull:r,auto:s}),this.expressions={},Object.defineProperty(e,"__Lata",{enumerable:!1,configurable:!1,writable:!1,value:this}),this.parentLata=this.options.parentLata,this.rootLata=this.options.rootLata??this,this.internalPath=this.options.internalPath??"",this.options.auto&&this._processLataDashPropAttributes(),this.options.pull&&this.pull(),this.options.push&&this.push(),this.proxy=h(e,{onSet:this.onMoleSet.bind(this)}),this._processLataDashInitAttributes(),this._processLataDashExprAttributes(),this._processLataDashOnclickAttributes(),this._processLataDashShowAttributes()}A.prototype._processConstructorArgs=function(t,e,o,{push:n=!0,pull:r=!1,auto:s=!1,parentLata:i,rootLata:a}={}){if(this.options={push:n,pull:r,auto:s,parentLata:i,rootLata:a},this.options.auto&&(this.options.pull=!0,this.options.push=!0),"string"==typeof t)this.$el=document.querySelector(t);else{if(!(t instanceof Element)){const t="No element supplied/found";throw m.error(0,t),new Error(t)}this.$el=t}if("object"!=typeof e){const t="data must be an object";throw m.error(0,t),new Error(t)}if("object"!=typeof o){const t="tiesOrSelectors must be an object";throw m.error(0,t),new Error(t)}this.data=e,this.ties={};for(const l in o)if(o[l]instanceof c)this.ties[l]=o[l];else{const t=new d(o[l]),e=this.$el.querySelector(t.css),n=new c(this.data,l,e,t.target);this.ties[l]=n}},A.prototype.push=function(t){const e=void 0===t?Object.keys(this.ties):[t];for(const o of e){if(!(o in this.ties))throw new Error(`No tie at path '${o}'`);this.ties[o].push()}},A.prototype.pull=function(t){const e=void 0===t?Object.keys(this.ties):[t];for(const o of e){this.ties[o].pull()}},A.prototype.pathFromRoot=function(t=""){var e;let o=(null==(e=this.parentLata)?void 0:e.internalPath)??"";return o+=this.internalPath+"."+t,o=o.replace(/^\./,""),o},A.prototype.onMoleSet=function(t,e,o,n,r){const s=function(t){return function(t){return t.__Lata instanceof A}(t)&&""===t.__Lata.internalPath}(e);if(s){let s=this.pathFromRoot(t);return e.__Lata.internalPath=s,e.__Lata.rootLata=this.rootLata,e.__Lata.parentLata=this,void A.prototype.onMoleSet.bind(e.__Lata.rootLata)(s,e,o,n,r)}m.warn(4,"property was set : { path, value, oldValue }: ",{path:t,value:e,oldValue:o});const i=this.ties[t];i&&(m.warn(4,"- pushing -"),i.push());let a=!1;return a=this._getLataExpressionVars().includes(t),a&&(this._processLataDashExprAttributes(),this._processLataDashShowAttributes()),!0},A.prototype._updateExpressions=function(){this._processLataDashExprAttributes(),this._processLataDashShowAttributes()},A.prototype._getLataExpressionVars=function(t){t=t||this._getLataExpressions();const e={};for(const o of t)for(const t of E(o))e[t]=1;return Object.keys(e)},A.prototype._getLataExpressions=function(){const t=this._getLataExprElements();return Array.from(t).map((t=>t.getAttribute("lata-expr")))},A.prototype._getLataExprElements=function(){return this.$el.querySelectorAll("[lata-expr]")},A.prototype._getLataOnclickElements=function(){return this.$el.querySelectorAll("[lata-onclick]")},A.prototype._processLataDashExprAttributes=function(){const t=this._getLataExprElements(),e=y(this,b);for(let o=0,n=t.length;ot));t.sort(e);const n=t.map((t=>t)),r={},s={};for(let a=0,l=t.length;a