var f=class extends Error{constructor(e){super(e),this.name="Panic"}};var l=class t{constructor(e){this.value=e}value;static some(e){return new t(e)}static none(){return new t(void 0)}is_some(){return new u(this.value!==void 0)}is_none(){return new u(this.value===void 0)}unwrap(){if(this.value===void 0)throw new f("called `Option.unwrap()` on a `None` value");return this.value}expect(e){if(this.value===void 0)throw new f(e.toString());return this.value}clone(){if(this.value===void 0)return t.none();let e=this.value;return t.some(typeof e?.clone=="function"?e.clone():this.value)}isAttributePresent(){return this.value===void 0?!1:this.value.isAttributePresent()}toAttributeValue(){return this.value.toAttributeValue()}toNodeText(){return this.value===void 0?"":this.value.toNodeText()}dehydrate(){return{t:"Option",v:this.value===void 0?null:this.value}}};var u=class t{constructor(e){this.v=e}v;clone(){return new t(this.v)}not(){return new t(!this.v)}eq(e){return new t(this.v===e.v)}ne(e){return new t(this.v!==e.v)}then(e){return this.v?l.some(e()):l.none()}then_some(e){return this.v?l.some(e):l.none()}isAttributePresent(){return this.v}toAttributeValue(){return"true"}toNodeText(){return this.v.toString()}dehydrate(){return this.v}toString(){return this.v.toString()}};var g=class t{constructor(e){this.v=e}v;add(e){return new t(this.v+e.v)}sub(e){return new t(this.v-e.v)}mul(e){return new t(this.v*e.v)}div(e){return new t(this.v/e.v)}neg(){return new t(-this.v)}eq(e){return new u(this.v===e.v)}ne(e){return new u(this.v!==e.v)}gt(e){return new u(this.v>e.v)}lt(e){return new u(this.v<e.v)}ge(e){return new u(this.v>=e.v)}le(e){return new u(this.v<=e.v)}clone(){return new t(this.v)}isAttributePresent(){return!0}toAttributeValue(){return this.v.toString()}toNodeText(){return this.v.toString()}dehydrate(){return this.v}toString(){return this.v.toString()}};var D=class{constructor(e){this.v=e}v;then(e,n){let r=this.v();return this.v=()=>r,r.then(e,n)}};var j=class{constructor(e,n){this.cx=e;this.id=n}cx;id;call(...e){return new D(async()=>{let n=await fetch(`/_topcoat/procedures/${encodeURIComponent(this.id)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e.map(r=>r.dehydrate()))});if(!n.ok)throw new Error(`Procedure call failed: ${n.status} ${n.statusText}`);return this.cx.hydrate(await n.json())})}dehydrate(){return{t:"Procedure",id:this.id}}};var v=class t{constructor(e,n){this.kind=e;this.value=n}kind;value;static from_ok(e){return new t("ok",e)}static from_err(e){return new t("err",e)}is_ok(){return new u(this.kind==="ok")}is_err(){return new u(this.kind==="err")}ok(){return this.kind==="ok"?l.some(this.value):l.none()}err(){return this.kind==="err"?l.some(this.value):l.none()}unwrap(){if(this.kind==="err")throw new f(`called \`Result.unwrap()\` on an \`Err\` value: ${W(this.value)}`);return this.value}expect(e){if(this.kind==="err")throw new f(`${e.toString()}: ${W(this.value)}`);return this.value}unwrap_err(){if(this.kind==="ok")throw new f(`called \`Result.unwrap_err()\` on an \`Ok\` value: ${W(this.value)}`);return this.value}expect_err(e){if(this.kind==="ok")throw new f(`${e.toString()}: ${W(this.value)}`);return this.value}clone(){let e=this.value,n=typeof e?.clone=="function"?e.clone():this.value;return this.kind==="ok"?t.from_ok(n):t.from_err(n)}dehydrate(){return{t:"Result",...this.kind==="ok"?{ok:this.value}:{err:this.value}}}};function W(t){if(typeof t=="string")return JSON.stringify(t);if(t!=null){let n=t.toString;if(typeof n=="function"&&n!==Object.prototype.toString)return n.call(t)}let e=JSON.stringify(t);return e===void 0?globalThis.String(t):e}var M=class{constructor(e,n){this.read=e;this.write=n}read;write;deref(){return this.read()}deref_mut(){this.write(this.read())}dehydrate(){throw new Error("Ref<T> cannot be dehydrated")}};var E=class{constructor(e,n){this.id=e;this.inner=n}id;inner;read(){return new M(()=>this.inner(),e=>this.inner.set(e))}get(){return this.read().deref().clone()}set(e){this.inner.set(e)}dehydrate(){return{t:"Signal",id:this.id}}};var F=class t{constructor(e){this.v=e}v;eq(e){return new u(this.v===e.v)}ne(e){return new u(this.v!==e.v)}gt(e){return new u(this.v>e.v)}lt(e){return new u(this.v<e.v)}ge(e){return new u(this.v>=e.v)}le(e){return new u(this.v<=e.v)}to_owned(){return new w(this.v)}is_empty(){return new u(this.v.length===0)}len(){return new g(this.v.length)}trim(){return new t(this.v.trim())}trim_start(){return new t(this.v.trimStart())}trim_end(){return new t(this.v.trimEnd())}starts_with(e){return new u(this.v.startsWith(e.v))}ends_with(e){return new u(this.v.endsWith(e.v))}contains(e){return new u(this.v.includes(e.v))}isAttributePresent(){return!0}toAttributeValue(){return this.v}toNodeText(){return this.v}dehydrate(){return this.v}toString(){return this.v.toString()}},w=class t extends F{deref(){return this}clone(){return new t(this.v)}dehydrate(){return this.v}};var L=class{constructor(e){this.inner=e}inner;get alt_key(){return x(this.inner,"altKey")}get bubbles(){return this.inner.bubbles}get button(){return c(this.inner,"button")}get buttons(){return c(this.inner,"buttons")}get cancelable(){return this.inner.cancelable}get client_x(){return c(this.inner,"clientX")}get client_y(){return c(this.inner,"clientY")}get code(){return y(this.inner,"code")}get ctrl_key(){return x(this.inner,"ctrlKey")}get current_target(){return new X(this.inner.currentTarget)}get data(){return y(this.inner,"data")}get default_prevented(){return this.inner.defaultPrevented}get delta_x(){return c(this.inner,"deltaX")}get delta_y(){return c(this.inner,"deltaY")}get delta_z(){return c(this.inner,"deltaZ")}get event_type(){return new w(this.inner.type)}get input_type(){return y(this.inner,"inputType")}get is_composing(){return x(this.inner,"isComposing")}get key(){return y(this.inner,"key")}get meta_key(){return x(this.inner,"metaKey")}get movement_x(){return c(this.inner,"movementX")}get movement_y(){return c(this.inner,"movementY")}get offset_x(){return c(this.inner,"offsetX")}get offset_y(){return c(this.inner,"offsetY")}get page_x(){return c(this.inner,"pageX")}get page_y(){return c(this.inner,"pageY")}get pointer_id(){return c(this.inner,"pointerId")}get pointer_type(){return y(this.inner,"pointerType")}get repeat(){return x(this.inner,"repeat")}get screen_x(){return c(this.inner,"screenX")}get screen_y(){return c(this.inner,"screenY")}get shift_key(){return x(this.inner,"shiftKey")}get target(){return new X(this.inner.target)}get time_stamp(){return new g(this.inner.timeStamp)}prevent_default(){this.inner.preventDefault()}stop_immediate_propagation(){this.inner.stopImmediatePropagation()}stop_propagation(){this.inner.stopPropagation()}},X=class{constructor(e){this.inner=e}inner;get checked(){return x(this.inner,"checked")}get id(){return y(this.inner,"id")}get name(){return y(this.inner,"name")}get text_content(){return new w(this.inner instanceof Node?this.inner.textContent??"":"")}get value(){return y(this.inner,"value")}};function x(t,e){return typeof t=="object"&&t!==null&&e in t?!!t[e]:!1}function c(t,e){let n=typeof t=="object"&&t!==null&&e in t?t[e]:0;return new g(typeof n=="number"&&Number.isFinite(n)?n:0)}function y(t,e){let n=typeof t=="object"&&t!==null&&e in t?t[e]:"";return new w(n==null?"":globalThis.String(n))}function _(t,e){switch(typeof t){case"string":return new w(t);case"number":return new g(t);case"boolean":return new u(t);case"bigint":case"symbol":case"undefined":case"function":throw new Error(`Unknown surrogate type: ${typeof t}`);case"object":switch(t.t){case"str":return new F(t.v);case"Option":return t.v===null?l.none():l.some(_(t.v,e));case"Result":return"ok"in t.v?v.from_ok(_(t.v.ok,e)):v.from_err(_(t.v.err,e));case"Signal":return new E(t.id,e.getRegistry().handle(t.id));case"Procedure":return new j(e,t.id);default:throw new Error(`Unknown surrogate type: ${t.t}`)}}}var P=class{constructor(e){this.registry=e}registry;getRegistry(){return this.registry}hydrate(e){return _(e,this)}signal(e){return new E(e,this.registry.handle(e))}some(e){return l.some(e)}none(){return l.none()}ok(e){return v.from_ok(e)}err(e){return v.from_err(e)}};var m=Symbol(0);var H=!1,ht=!1,$=null,C=null,h=null,p=0,R=[],G={},Rt=()=>{},A=0,ft=1,J=2,Y=3;function Ct(){H=!0,queueMicrotask(Nt)}function Nt(){if(!R.length){H=!1;return}ht=!0;for(let t=0;t<R.length;t++)R[t].$st!==A&&At(R[t]);R=[],H=!1,ht=!1}function At(t){let e=[t];for(;t=t[m];)t.$e&&t.$st!==A&&e.push(t);for(let n=e.length-1;n>=0;n--)at(e[n])}function Q(t){return et(null,t,null)}function Z(t,e){try{return et(e,t,null)}catch(n){nt(e,n);return}}function tt(t){if(!t||!$)return t||Rt;let e=$;return e.$d?Array.isArray(e.$d)?e.$d.push(t):e.$d=[e.$d,t]:e.$d=t,function(){e.$st!==Y&&(t.call(null),V(e.$d)?e.$d=null:Array.isArray(e.$d)&&e.$d.splice(e.$d.indexOf(t),1))}}function N(t=!0){if(this.$st!==Y){if(this.$h)if(Array.isArray(this.$h))for(let e=this.$h.length-1;e>=0;e--)N.call(this.$h[e]);else N.call(this.$h);if(t){let e=this[m];e&&(Array.isArray(e.$h)?e.$h.splice(e.$h.indexOf(this),1):e.$h=null),Ot(this)}}}function Ot(t){t.$st=Y,t.$d&&mt(t),t.$s&&z(t,0),t[m]=null,t.$s=null,t.$o=null,t.$h=null,t.$cx=G,t.$eh=null}function mt(t){try{if(Array.isArray(t.$d))for(let e=t.$d.length-1;e>=0;e--){let n=t.$d[e];n.call(n)}else t.$d.call(t.$d);t.$d=null}catch(e){nt(t,e)}}function et(t,e,n){let r=$,i=C;$=t,C=n;try{return e.call(t)}finally{$=r,C=i}}function nt(t,e){if(!t||!t.$eh)throw e;let n=0,r=t.$eh.length,i=e;for(n=0;n<r;n++)try{t.$eh[n](i);break}catch(s){i=s}if(n===r)throw i}function rt(){return this.$st===Y?this.$v:(C&&!this.$e&&(!h&&C.$s&&C.$s[p]==this?p++:h?h.push(this):h=[this]),this.$c&&at(this),this.$v)}function it(t){let e=V(t)?t(this.$v):t;if(this.$ch(this.$v,e)&&(this.$v=e,this.$o))for(let n=0;n<this.$o.length;n++)vt(this.$o[n],J);return this.$v}var ot=function(){this[m]=null,this.$h=null,$&&$.append(this)},b=ot.prototype;b.$cx=G;b.$eh=null;b.$c=null;b.$d=null;b.append=function(t){t[m]=this,this.$h?Array.isArray(this.$h)?this.$h.push(t):this.$h=[this.$h,t]:this.$h=t,t.$cx=t.$cx===G?this.$cx:{...this.$cx,...t.$cx},this.$eh&&(t.$eh=t.$eh?[...t.$eh,...this.$eh]:this.$eh)};b.dispose=function(){N.call(this)};function st(){return new ot}var gt=function(e,n,r){ot.call(this),this.$st=n?J:A,this.$i=!1,this.$e=!1,this.$s=null,this.$o=null,this.$v=e,n&&(this.$c=n),r&&r.dirty&&(this.$ch=r.dirty)},ut=gt.prototype;Object.setPrototypeOf(ut,b);ut.$ch=wt;ut.call=rt;function q(t,e,n){return new gt(t,e,n)}function wt(t,e){return t!==e}function V(t){return typeof t=="function"}function at(t){if(t.$st===ft)for(let e=0;e<t.$s.length&&(at(t.$s[e]),t.$st!==J);e++);t.$st===J?lt(t):t.$st=A}function Ft(t){t.$h&&N.call(t,!1),t.$d&&mt(t),t.$eh=t[m]?t[m].$eh:null}function lt(t){let e=h,n=p;h=null,p=0;try{Ft(t);let r=et(t,t.$c,t);dt(t),!t.$e&&t.$i?it.call(t,r):(t.$v=r,t.$i=!0)}catch(r){dt(t),nt(t,r)}finally{h=e,p=n,t.$st=A}}function dt(t){if(h){if(t.$s&&z(t,p),t.$s&&p>0){t.$s.length=p+h.length;for(let n=0;n<h.length;n++)t.$s[p+n]=h[n]}else t.$s=h;let e;for(let n=p;n<t.$s.length;n++)e=t.$s[n],e.$o?e.$o.push(t):e.$o=[t]}else t.$s&&p<t.$s.length&&(z(t,p),t.$s.length=p)}function vt(t,e){if(!(t.$st>=e)&&(t.$e&&t.$st===A&&(R.push(t),H||Ct()),t.$st=e,t.$o))for(let n=0;n<t.$o.length;n++)vt(t.$o[n],ft)}function z(t,e){let n,r;for(let i=e;i<t.$s.length;i++)n=t.$s[i],n.$o&&(r=n.$o.indexOf(t),n.$o[r]=n.$o[n.$o.length-1],n.$o.pop())}function ct(t,e){let n=q(t,null,e),r=rt.bind(n);return r[m]=!0,r.set=it.bind(n),r}function T(t,e){let n=q(null,function(){let i=t();return V(i)&&tt(i),null},void 0);return n.$e=!0,lt(n),N.bind(n,!0)}function yt(t){return t!==null&&typeof t=="object"&&typeof t.isAttributePresent=="function"&&typeof t.toAttributeValue=="function"}function St(t){return t!==null&&typeof t=="object"&&typeof t.toNodeText=="function"}var xt="data-topcoat-bind:",_t=new Set(["value","checked","selected","indeterminate"]);function $t(t,e,n){if(!e.name.startsWith(xt))return;let r=e.name.substring(xt.length),i=new Function("cx",`return ${e.value};`),{context:s}=n.runtime;n.run(()=>{T(()=>{Vt(t,r,i(s))})})}function Vt(t,e,n){if(_t.has(e)&&(t[e]=n),yt(n)){if(!n.isAttributePresent()){t.removeAttribute(e);return}t.setAttribute(e,n.toAttributeValue());return}if(n==null||n===!1){t.removeAttribute(e);return}if(n===!0){t.setAttribute(e,"");return}t.setAttribute(e,String(n))}var O=class{signals=new Map;has(e){return this.signals.has(e)}get(e){return this.signals.get(e)}insert(e,n){return this.signals.has(e)?!1:(this.signals.set(e,ct(n)),!0)}delete(e){this.signals.delete(e)}handle(e){let n=this.signals.get(e);if(!n)throw new Error(`Unknown signal id: ${e}`);return n}read(e){return this.handle(e)()}};var It=/^\s*::topcoat::signal\(([\s\S]*)\)\s*$/,Bt=/^\s*::topcoat::expr::start\("([^"]*)"\)\s*$/,Dt=/^\s*::topcoat::expr::end\s*$/,jt=/^\s*::topcoat::scope::start\(("[^"]+"), ("[^"]*"), (\[[\s\S]*\])\)\s*$/,Wt=/^\s*::topcoat::scope::end\(("[^"]+")\)\s*$/,pt=/"([^"]*)"/g;function Tt(t){let e=t.data,n=It.exec(e);if(n){let o=JSON.parse(n[1]??"");if(o.t!=="signal"||typeof o.id!="string")throw new Error("Invalid signal marker");let a=new P(new O).hydrate(o.v);return{kind:"signal",id:o.id,value:a}}let r=Bt.exec(e);if(r)return{kind:"expr-start",js:bt(r[1]??"")};if(Dt.test(e))return{kind:"expr-end"};let i=jt.exec(e);if(i){let o=[];pt.lastIndex=0;let a=pt.exec(i[3]??"");for(;a!==null;)o.push(bt(a[1]??"")),a=pt.exec(i[3]??"");return{kind:"scope-start",id:JSON.parse(i[1]??""),path:JSON.parse(i[2]??""),exprs:o}}let s=Wt.exec(e);return s?{kind:"scope-end",id:JSON.parse(s[1]??"")}:null}function bt(t){let e=new DOMParser().parseFromString(t,"text/html").documentElement.textContent;if(e===null)throw new Error("Failed to decode comment marker");return e}var kt="data-topcoat-on:";function Et(t,e,n){if(!e.name.startsWith(kt))return;let r=e.name.substring(kt.length),s=new Function("cx",`return ${e.value};`)(n.runtime.context);t.addEventListener(r,o=>s(new L(o)))}var k=class{constructor(e,n){this.parent=e;this.runtime=n;e?.children.add(this)}parent;runtime;children=new Set;signalIds=new Set;mScope=st();disposed=!1;run(e){return Z(e,this.mScope)}dispose(){if(!this.disposed){this.disposed=!0;for(let e of this.children)e.dispose();this.children.clear(),this.mScope.dispose();for(let e of this.signalIds)this.runtime.registry.delete(e);this.signalIds.clear(),this.parent?.children.delete(this)}}get isDisposed(){return this.disposed}},I=class extends k{constructor(n,r,i,s,o,a){super(n,r);this.scopeId=i;this.path=s;this.startNode=a;this.contentScope=new k(this,r),this.computes=o.map(d=>new Function("cx",`return ${d};`))}scopeId;path;startNode;contentScope;endNode=null;computes;abortController=null;flushPending=!1;attachEnd(n){this.endNode=n}startWatching(){let{context:n}=this.runtime,r=!0;this.run(()=>{T(()=>{for(let i of this.computes)i(n);if(r){r=!1;return}this.scheduleFetch()})})}scheduleFetch(){this.flushPending||(this.flushPending=!0,queueMicrotask(()=>{this.flushPending=!1,!this.isDisposed&&this.fetchAndReplace()}))}async fetchAndReplace(){if(this.endNode===null)return;this.abortController?.abort();let n=new AbortController;this.abortController=n;let{context:r}=this.runtime,i=Q(()=>this.computes.map(S=>S(r).dehydrate())),s;try{s=await(await fetch(this.path,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i),signal:n.signal})).text()}catch(S){if(S.name==="AbortError")return;throw S}if(this.isDisposed||this.abortController!==n)return;this.abortController=null;let o=this.startNode.parentNode,a=this.endNode;if(!o)return;this.contentScope.dispose(),this.contentScope=new k(this,this.runtime);let d=this.startNode.nextSibling;for(;d&&d!==a;){let S=d.nextSibling;o.removeChild(d),d=S}let B=document.createRange().createContextualFragment(s);o.insertBefore(B,a),K(o,this.startNode,a,this.contentScope)}};function Pt(t,e,n,r){let i=new Function("cx",`return ${n};`),{context:s}=r.runtime,o=!0;r.run(()=>{T(()=>{let a=i(s);if(o){o=!1;return}Mt(t,e,a)})})}function Mt(t,e,n){let r=t.parentNode;if(!r)return;let i=t.nextSibling;for(;i&&i!==e;){let o=i.nextSibling;r.removeChild(i),i=o}let s=Lt(n);s.length>0&&r.insertBefore(document.createTextNode(s),e)}function Lt(t){let e=t;for(;Xt(e);)e=e.deref();return e==null?"":St(e)?e.toNodeText():String(e)}function Xt(t){return t!==null&&typeof t=="object"&&typeof t.deref=="function"}function K(t,e,n,r){let i=document.createTreeWalker(t,NodeFilter.SHOW_COMMENT|NodeFilter.SHOW_ELEMENT);e&&(i.currentNode=e);let s=[r],o=[];for(let a=i.nextNode();a&&!(n&&a===n);a=i.nextNode()){let d=s[s.length-1];if(d===void 0)throw new Error("Stack was empty");if(a.nodeType===Node.ELEMENT_NODE){Ht(a,d);continue}let B=Tt(a);B&&Jt(B,a,s,o)}}function Ht(t,e){for(let n of Array.from(t.attributes))$t(t,n,e),Et(t,n,e)}function Jt(t,e,n,r){let i=n[n.length-1];if(i===void 0)throw new Error("Stack was empty");switch(t.kind){case"signal":{i.runtime.registry.insert(t.id,t.value)&&i.signalIds.add(t.id);break}case"expr-start":{r.push({start:e,js:t.js,scope:i});break}case"expr-end":{let s=r.pop();if(!s)throw new Error("Unbalanced text expression: end marker has no start");Pt(s.start,e,s.js,s.scope);break}case"scope-start":{let s=new I(i,i.runtime,t.id,t.path,t.exprs,e);n.push(s.contentScope);break}case"scope-end":{let o=n.pop()?.parent;if(!(o instanceof I))throw new Error(`Unbalanced reactive scope: end marker ${t.id} has no matching start`);if(o.scopeId!==t.id)throw new Error(`Mismatched reactive scope: end ${t.id} does not match start ${o.scopeId}`);o.attachEnd(e),o.startWatching();break}}}var U=class{registry=new O;context=new P(this.registry);rootScope=new k(null,this);start(e){K(e,null,null,this.rootScope)}};new U().start(document.body);
//# sourceMappingURL=index.js.map