!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vega-util"),require("vega-dataflow")):"function"==typeof define&&define.amd?define(["exports","vega-util","vega-dataflow"],t):t((e=e||self).vega={},e.vega,e.vega)}(this,(function(e,t,r){"use strict";const n=t.toSet(["rule"]),s=t.toSet(["group","image","rect"]);function a(e){return(e+"").toLowerCase()}function o(e,t,r){";"!==r[r.length-1]&&(r="return("+r+");");var n=Function.apply(null,t.concat(r));return e&&e.functions?n.bind(e.functions):n}var i={operator:(e,t)=>o(e,["_"],t.code),parameter:(e,t)=>o(e,["datum","_"],t.code),event:(e,t)=>o(e,["event"],t.code),handler:(e,t)=>o(e,["_","event"],`var datum=event.item&&event.item.datum;return ${t.code};`),encode:(e,r)=>{const{marktype:a,channels:i}=r;let c="var o=item,datum=o.datum,m=0,$;";for(const e in i){const r="o["+t.stringValue(e)+"]";c+=`$=${i[e].code};if(${r}!==$)${r}=$,m=1;`}return c+=function(e,t){let r="";return n[t]||(e.x2&&(e.x?(s[t]&&(r+="if(o.x>o.x2)$=o.x,o.x=o.x2,o.x2=$;"),r+="o.width=o.x2-o.x;"):r+="o.x=o.x2-(o.width||0);"),e.xc&&(r+="o.x=o.xc-(o.width||0)/2;"),e.y2&&(e.y?(s[t]&&(r+="if(o.y>o.y2)$=o.y,o.y=o.y2,o.y2=$;"),r+="o.height=o.y2-o.y;"):r+="o.y=o.y2-(o.height||0);"),e.yc&&(r+="o.y=o.yc-(o.height||0)/2;")),r}(i,a),c+="return m;",o(e,["item","_"],c)},codegen:{get(e){const r=`[${e.map(t.stringValue).join("][")}]`,n=Function("_",`return _${r};`);return n.path=r,n},comparator(e,t){let r;const n=Function("a","b","var u, v; return "+e.map((e,n)=>{const s=t[n];let a,o;return e.path?(a="a"+e.path,o="b"+e.path):((r=r||{})["f"+n]=e,a=`this.f${n}(a)`,o=`this.f${n}(b)`),function(e,t,r,n){return`((u = ${e}) < (v = ${t}) || u == null) && v != null ? ${r}\n : (u > v || v == null) && u != null ? ${n}\n : ((v = v instanceof Date ? +v : v), (u = u instanceof Date ? +u : u)) !== u && v === v ? ${r}\n : v !== v && u === u ? ${n} : `}(a,o,-s,s)}).join("")+"0;");return r?n.bind(r):n}}};function c(e,r,n){if(!e||!t.isObject(e))return e;for(let s,a=0,o=u.length;ae&&e.$tupleid?r.tupleid:e);return n.fn[s]||(n.fn[s]=t.compare(a,e.$order,n.expr.codegen))}},{key:"$context",parse:function(e,t){return t}},{key:"$subflow",parse:function(e,t){const r=e.$subflow;return function(e,n,s){const a=t.fork().parse(r),o=a.get(r.operators[0].id),i=a.signals.parent;return i&&i.set(s),o.detachSubflow=()=>t.detach(a),o}}},{key:"$tupleid",parse:function(){return r.tupleid}}];var p={skip:!0};function f(e,t,r,n){this.dataflow=e,this.transforms=t,this.events=e.events.bind(e),this.expr=n||i,this.signals={},this.scales={},this.nodes={},this.data={},this.fn={},r&&(this.functions=Object.create(r),this.functions.context=this)}function d(e){this.dataflow=e.dataflow,this.transforms=e.transforms,this.events=e.events,this.expr=e.expr,this.signals=Object.create(e.signals),this.scales=Object.create(e.scales),this.nodes=Object.create(e.nodes),this.data=Object.create(e.data),this.fn=Object.create(e.fn),e.functions&&(this.functions=Object.create(e.functions),this.functions.context=this)}f.prototype=d.prototype={fork(){const e=new d(this);return(this.subcontext||(this.subcontext=[])).push(e),e},detach(e){this.subcontext=this.subcontext.filter(t=>t!==e);const t=Object.keys(e.nodes);for(const r of t)e.nodes[r]._targets=null;for(const r of t)e.nodes[r].detach();e.nodes=null},get(e){return this.nodes[e]},set(e,t){return this.nodes[e]=t},add(e,t){const r=this,n=r.dataflow,s=e.value;if(r.set(e.id,t),"collect"===a(e.type)&&s&&(s.$ingest?n.ingest(t,s.$ingest,s.$format):s.$request?n.preload(t,s.$request,s.$format):n.pulse(t,n.changeset().insert(s))),e.root&&(r.root=t),e.parent){var o=r.get(e.parent.$ref);o?(n.connect(o,[t]),t.targets().add(o)):(r.unresolved=r.unresolved||[]).push(()=>{o=r.get(e.parent.$ref),n.connect(o,[t]),t.targets().add(o)})}if(e.signal&&(r.signals[e.signal]=t),e.scale&&(r.scales[e.scale]=t),e.data)for(const n in e.data){const s=r.data[n]||(r.data[n]={});e.data[n].forEach(e=>s[e]=t)}},resolve(){return(this.unresolved||[]).forEach(e=>e()),delete this.unresolved,this},operator(e,t){this.add(e,this.dataflow.add(e.value,t))},transform(e,t){this.add(e,this.dataflow.add(this.transforms[a(t)]))},stream(e,t){this.set(e.id,t)},update(e,t,r,n,s){this.dataflow.on(t,r,n,s,e.options)},operatorExpression(e){return this.expr.operator(this,e)},parameterExpression(e){return this.expr.parameter(this,e)},eventExpression(e){return this.expr.event(this,e)},handlerExpression(e){return this.expr.handler(this,e)},encodeExpression(e){return this.expr.encode(this,e)},parse:function(e){const t=this,r=e.operators||[];return e.background&&(t.background=e.background),e.eventConfig&&(t.eventConfig=e.eventConfig),e.locale&&(t.locale=e.locale),r.forEach(e=>t.parseOperator(e)),r.forEach(e=>t.parseOperatorParameters(e)),(e.streams||[]).forEach(e=>t.parseStream(e)),(e.updates||[]).forEach(e=>t.parseUpdate(e)),t.resolve()},parseOperator:function(e){const t=this;"operator"!==a(e.type)&&e.type?t.transform(e,e.type):t.operator(e,e.update?t.operatorExpression(e.update):null)},parseOperatorParameters:function(e){const r=this;if(e.params){const n=r.get(e.id);n||t.error("Invalid operator id: "+e.id),r.dataflow.connect(n,n.parameters(r.parseParameters(e.params),e.react,e.initonly))}},parseParameters:function(e,r){r=r||{};const n=this;for(const s in e){const a=e[s];r[s]=t.isArray(a)?a.map(e=>c(e,n,r)):c(a,n,r)}return r},parseStream:function(e){var r,n=this,s=null!=e.filter?n.eventExpression(e.filter):void 0,a=null!=e.stream?n.get(e.stream):void 0;e.source?a=n.events(e.source,e.type,s):e.merge&&(a=(r=e.merge.map(e=>n.get(e)))[0].merge.apply(r[0],r.slice(1))),e.between&&(r=e.between.map(e=>n.get(e)),a=a.between(r[0],r[1])),e.filter&&(a=a.filter(s)),null!=e.throttle&&(a=a.throttle(+e.throttle)),null!=e.debounce&&(a=a.debounce(+e.debounce)),null==a&&t.error("Invalid stream definition: "+JSON.stringify(e)),e.consume&&a.consume(!0),n.stream(e,a)},parseUpdate:function(e){var r,n=t.isObject(n=e.source)?n.$ref:n,s=this.get(n),a=e.update,o=void 0;s||t.error("Source not defined: "+e.source),r=e.target&&e.target.$expr?this.eventExpression(e.target.$expr):this.get(e.target),a&&a.$expr&&(a.$params&&(o=this.parseParameters(a.$params)),a=this.handlerExpression(a.$expr)),this.update(e,s,r,a,o)},getState:function(e){var t=this,r={};if(e.signals){var n=r.signals={};Object.keys(t.signals).forEach(r=>{var s=t.signals[r];e.signals(r,s)&&(n[r]=s.value)})}if(e.data){var s=r.data={};Object.keys(t.data).forEach(r=>{var n=t.data[r];e.data(r,n)&&(s[r]=n.input.value)})}return t.subcontext&&!1!==e.recurse&&(r.subcontext=t.subcontext.map(t=>t.getState(e))),r},setState:function(e){var r=this,n=r.dataflow,s=e.data,a=e.signals;Object.keys(a||{}).forEach(e=>{n.update(r.signals[e],a[e],p)}),Object.keys(s||{}).forEach(e=>{n.pulse(r.data[e].input,n.changeset().remove(t.truthy).insert(s[e]))}),(e.subcontext||[]).forEach((e,t)=>{var n=r.subcontext[t];n&&n.setState(e)})}},e.context=function(e,t,r,n){return new f(e,t,r,n)},Object.defineProperty(e,"__esModule",{value:!0})}));