@wcstack/state 1.4.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +26 -0
- package/README.md +26 -0
- package/dist/index.d.ts +218 -2
- package/dist/index.esm.js +122 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/package.json +71 -71
package/dist/index.esm.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const t={bindAttributeName:"data-wcs",commentTextPrefix:"wcs-text",commentForPrefix:"wcs-for",commentIfPrefix:"wcs-if",commentElseIfPrefix:"wcs-elseif",commentElsePrefix:"wcs-else",tagNames:{state:"wcs-state"},locale:"en",debug:!1,enableMustache:!0},e=t;function n(t){throw new Error(`[@wcstack/state] ${t}`)}const o=new WeakMap;let s=0;function i(t){let e,n=o.get(t)||null;if(null!==n)return n;const i=new Promise(t=>{e=t});return n={id:++s,promise:i,resolve:e},o.set(t,n),n}async function a(t){const e=i(t);await e.promise}function r(t){i(t).resolve()}function l(t){const e=t.node,n=t.replaceNode;e!==n&&null!==e.parentNode&&e.parentNode.replaceChild(n,e)}function d(t,e){let n=t;if(0===e.length)return n;for(let t=0;t<e.length&&(n=n?.childNodes[e[t]]??null,null!==n);t++);return n}function c(t,e){const n=[];for(const o of e)if("text"!==o.bindingType)n.push({...o,node:t,replaceNode:t});else{const e=document.createTextNode("");n.push({...o,node:t,replaceNode:e})}return n}const u=new WeakMap;function f(t){return u.get(t)||null}function h(t,e){u.set(t,e)}const p=".",m="*",g=128,w={};for(let t=0;t<128;t++)w[`$${t+1}`]=t;const b=Object.freeze(w),x="$connectedCallback",y="$disconnectedCallback",I="$stateReadyCallback",S=new Map;let N=0;function P(t){let e=S.get(t);return void 0!==e||(e=Object.freeze(new _(t)),S.set(t,e)),e}class _{id=++N;path;segments;lastSegment;cumulativePaths;cumulativePathSet;cumulativePathInfos;cumulativePathInfoSet;parentPath;wildcardPaths;wildcardPathSet;indexByWildcardPath;wildcardPathInfos;wildcardPathInfoSet;wildcardParentPaths;wildcardParentPathSet;wildcardParentPathInfos;wildcardParentPathInfoSet;wildcardPositions;lastWildcardPath;lastWildcardInfo;wildcardCount;parentPathInfo;constructor(t){const e=e=>t===e?this:P(e),n=t.split("."),o=[],s=[],i=[],a={},r=[],l=[],d=[],c=[];let u="",f="",h=0;for(let t=0;t<n.length;t++)u+=n[t],n[t]===m&&(i.push(u),a[u]=h,r.push(e(u)),l.push(f),d.push(e(f)),c.push(t),h++),o.push(u),s.push(e(u)),f=u,u+=".";const p=i.length>0?i[i.length-1]:null,g=o.length>1?o[o.length-2]:null;this.path=t,this.segments=n,this.lastSegment=n[n.length-1],this.cumulativePaths=o,this.cumulativePathSet=new Set(o),this.cumulativePathInfos=s,this.cumulativePathInfoSet=new Set(s),this.wildcardPaths=i,this.wildcardPathSet=new Set(i),this.indexByWildcardPath=a,this.wildcardPathInfos=r,this.wildcardPathInfoSet=new Set(r),this.wildcardParentPaths=l,this.wildcardParentPathSet=new Set(l),this.wildcardParentPathInfos=d,this.wildcardParentPathInfoSet=new Set(d),this.wildcardPositions=c,this.lastWildcardPath=p,this.lastWildcardInfo=p?e(p):null,this.parentPath=g,this.parentPathInfo=g?e(g):null,this.wildcardCount=h}}const v=new Set(["if","elseif","else","for"]);function A(t){n(`filter ${t} requires at least one option`)}function $(t){n(`filter ${t} requires a number as option`)}function E(t){n(`filter ${t} requires a number value`)}function C(t){n(`filter ${t} requires a date value`)}function k(t){return!(!t||isNaN(Number(t)))}const M={eq:t=>{const e=t?.[0]??A("eq");return t=>"number"==typeof t?(k(e)||$("eq"),t===Number(e)):t===e},ne:t=>{const e=t?.[0]??A("ne");return t=>"number"==typeof t?(k(e)||$("ne"),t!==Number(e)):t!==e},not:t=>t=>("boolean"!=typeof t&&n(`filter ${"not"} requires a boolean value`),!t),lt:t=>{const e=t?.[0]??A("lt");return k(e)||$("lt"),t=>("number"!=typeof t&&E("lt"),t<Number(e))},le:t=>{const e=t?.[0]??A("le");return k(e)||$("le"),t=>("number"!=typeof t&&E("le"),t<=Number(e))},gt:t=>{const e=t?.[0]??A("gt");return k(e)||$("gt"),t=>("number"!=typeof t&&E("gt"),t>Number(e))},ge:t=>{const e=t?.[0]??A("ge");return k(e)||$("ge"),t=>("number"!=typeof t&&E("ge"),t>=Number(e))},inc:t=>{const e=t?.[0]??A("inc");return k(e)||$("inc"),t=>("number"!=typeof t&&E("inc"),t+Number(e))},dec:t=>{const e=t?.[0]??A("dec");return k(e)||$("dec"),t=>("number"!=typeof t&&E("dec"),t-Number(e))},mul:t=>{const e=t?.[0]??A("mul");return k(e)||$("mul"),t=>("number"!=typeof t&&E("mul"),t*Number(e))},div:t=>{const e=t?.[0]??A("div");return k(e)||$("div"),t=>("number"!=typeof t&&E("div"),t/Number(e))},mod:t=>{const e=t?.[0]??A("mod");return k(e)||$("mod"),t=>("number"!=typeof t&&E("mod"),t%Number(e))},fix:t=>{const e=t?.[0]??"0";return k(e)||$("fix"),t=>("number"!=typeof t&&E("fix"),t.toFixed(Number(e)))},locale:t=>{const n=t?.[0]??e.locale;return t=>("number"!=typeof t&&E("locale"),t.toLocaleString(n))},uc:t=>t=>String(t).toUpperCase(),lc:t=>t=>String(t).toLowerCase(),cap:t=>t=>{const e=String(t);return 0===e.length?e:1===e.length?e.toUpperCase():e.charAt(0).toUpperCase()+e.slice(1)},trim:t=>t=>String(t).trim(),slice:t=>{const e=[],n=t?.[0]??A("slice");k(n)||$("slice"),e.push(Number(n));const o=t?.[1];return void 0!==o&&(k(o)||$("slice"),e.push(Number(o))),t=>String(t).slice(...e)},substr:t=>{const e=t?.[0]??A("substr");k(e)||$("substr");const n=t?.[1]??A("substr");return k(n)||$("substr"),t=>String(t).substr(Number(e),Number(n))},pad:t=>{const e=t?.[0]??A("pad");k(e)||$("pad");const n=t?.[1]??"0";return t=>String(t).padStart(Number(e),n)},rep:t=>{const e=t?.[0]??A("rep");return k(e)||$("rep"),t=>String(t).repeat(Number(e))},rev:t=>t=>String(t).split("").reverse().join(""),int:t=>t=>parseInt(String(t),10),float:t=>t=>parseFloat(String(t)),round:t=>{const e=t?.[0]??"0";return k(e)||$("round"),t=>{"number"!=typeof t&&E("round");const n=Math.pow(10,Number(e));return Math.round(t*n)/n}},floor:t=>{const e=t?.[0]??"0";return k(e)||$("floor"),t=>{"number"!=typeof t&&E("floor");const n=Math.pow(10,Number(e));return Math.floor(t*n)/n}},ceil:t=>{const e=t?.[0]??"0";return k(e)||$("ceil"),t=>{"number"!=typeof t&&E("ceil");const n=Math.pow(10,Number(e));return Math.ceil(t*n)/n}},percent:t=>{const e=t?.[0]??"0";return k(e)||$("percent"),t=>("number"!=typeof t&&E("percent"),`${(100*t).toFixed(Number(e))}%`)},date:t=>{const n=t?.[0]??e.locale;return t=>(t instanceof Date||C("date"),t.toLocaleDateString(n))},time:t=>{const n=t?.[0]??e.locale;return t=>(t instanceof Date||C("time"),t.toLocaleTimeString(n))},datetime:t=>{const n=t?.[0]??e.locale;return t=>(t instanceof Date||C("datetime"),t.toLocaleString(n))},ymd:t=>{const e=t?.[0]??"-";return t=>{t instanceof Date||C("ymd");const n=t.getFullYear().toString(),o=(t.getMonth()+1).toString().padStart(2,"0"),s=t.getDate().toString().padStart(2,"0");return`${n}${e}${o}${e}${s}`}},falsy:t=>t=>!1===t||null==t||0===t||""===t||Number.isNaN(t),truthy:t=>t=>!1!==t&&null!=t&&0!==t&&""!==t&&!Number.isNaN(t),defaults:t=>{const e=t?.[0]??A("defaults");return t=>!1===t||null==t||0===t||""===t||Number.isNaN(t)?e:t},boolean:t=>t=>Boolean(t),number:t=>t=>Number(t),string:t=>t=>String(t),null:t=>t=>""===t?null:t},T=M,L={input:M,output:T},W=(t,e)=>o=>{const s=o[t];return s||n(`filter not found: ${t}`),s(e)};const R=new Map;function O(t,e){const o=L[e];return t.map(t=>{const s=t.indexOf("("),i=t.lastIndexOf(")");if(-1!==s&&-1===i&&n(`Invalid filter format: missing closing parenthesis in "${t}"`),-1!==i&&-1===s&&n(`Invalid filter format: missing opening parenthesis in "${t}"`),-1===s){const n=t.trim(),s=`${n}():${e}`;let i=R.get(s);return void 0===i&&(i=W(n,[])(o),R.set(s,i)),{filterName:n,args:[],filterFn:i}}{const n=t.substring(s+1,i),a=t.substring(0,s).trim(),r=function(t){const e=[];let n="",o=null,s=!1;for(let i=0;i<t.length;i++){const a=t[i];o?a===o?o=null:n+=a:'"'===a||"'"===a?(o=a,s=!0):","===a?(e.push(n.trim()),n="",s=!1):n+=a}const i=n.trim();return(i||s)&&e.push(i),e}(n),l=`${a}(${r.join(",")}):${e}`;let d=R.get(l);return void 0===d&&(d=W(a,r)(o),R.set(l,d)),{filterName:a,args:r,filterFn:d}}})}const F=t=>t.trim(),D=new Map;const j=new Map;function B(t){const e=t.indexOf("|");let n="",o=[],s="",i=[];-1!==e?(n=t.slice(0,e).trim(),s=t.slice(e+1).trim(),j.has(s)?i=j.get(s):(o=s.split("|").map(F),i=O(o,"output"),j.set(s,i))):n=t.trim();const[a,r="default"]=n.split("@").map(F);return{stateName:r,statePathName:a,statePathInfo:P(a),outFilters:i}}function z(t){const[...e]=t.split(";").map(F).filter(t=>t.length>0),o=e.map(t=>{const e=t.indexOf(":");-1===e&&n(`Invalid bindText: "${t}". Missing ':' separator between propPart and statePart.`);const o=t.slice(0,e).trim(),s=t.slice(e+1).trim();if("else"===o){return{propName:"else",propSegments:["else"],propModifiers:[],statePathName:"#else",statePathInfo:P("#else"),stateName:"",inFilters:[],outFilters:[],bindingType:"else"}}if("if"===o||"elseif"===o||"for"===o||"radio"===o||"checkbox"===o){return{propName:o,propSegments:[o],propModifiers:[],inFilters:[],...B(s),bindingType:o}}{const t=B(s),e=function(t){const e=t.indexOf("|");let n="",o=[],s="",i=[];-1!==e?(n=t.slice(0,e).trim(),s=t.slice(e+1).trim(),D.has(s)?i=D.get(s):(o=s.split("|").map(F),i=O(o,"input"),D.set(s,i))):n=t.trim();const[a,r]=n.split("#").map(F),l=a.split(".").map(F);return{propName:a,propSegments:l,propModifiers:r?r.split(",").map(F):[],inFilters:i}}(o);return e.propSegments[0].startsWith("on")?{...e,...t,bindingType:"event"}:{...e,...t,bindingType:"prop"}}});if(o.length>1){o.some(t=>v.has(t.bindingType))&&n(`Invalid bindText: "${t}". 'if', 'elseif', 'else', and 'for' bindings must be single binding.`)}return o}const q=new WeakMap,U=new Set([e.commentTextPrefix,e.commentForPrefix,e.commentIfPrefix,e.commentElseIfPrefix,e.commentElsePrefix]),V=new RegExp("^\\s*@@\\s*(.*?)\\s*:\\s*(.+?)\\s*$");function H(t){const n=q.get(t);if("string"==typeof n)return n;if(t.nodeType!==Node.COMMENT_NODE)return null;const o=t.data.trim(),s=V.exec(o);if(null===s)return null;const i=s[1]||e.commentTextPrefix;return U.has(i)?(q.set(t,s[2]),s[2]):null}const J=new Map;function X(t,e,o){if(null===o)J.delete(t);else{J.set(t,o);const s=o.parseBindTextResult,i=yn(e,s.stateName);null===i&&n(`State element with name "${s.stateName}" not found for fragment info.`),i.setPathInfo(s.statePathName,s.bindingType);for(const t of o.nodeInfos)for(const o of t.parseBindTextResults){const t=yn(e,o.stateName);null===t&&n(`State element with name "${o.stateName}" not found for fragment info node.`),t.setPathInfo(o.statePathName,o.bindingType)}}}function K(t){return J.get(t)||null}function Y(t){if(t.nodeType===Node.ELEMENT_NODE){return z(t.getAttribute(e.bindAttributeName)||"")}if(t.nodeType===Node.COMMENT_NODE){const e=H(t);null===e&&n("Comment node binding text not found.");const o=K(e);let s=o?.parseBindTextResult??null,i=null;return null===s?(s={propName:"textContent",propSegments:["textContent"],propModifiers:[],inFilters:[],...B(e),bindingType:"text"},i=null):i=e,[{...s,uuid:i}]}return[]}function G(t){const n=[],o=document.createTreeWalker(t,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_COMMENT,{acceptNode(t){if(t.nodeType===Node.ELEMENT_NODE){return t.hasAttribute(e.bindAttributeName)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}return null!==H(t)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});for(;o.nextNode();)n.push(o.currentNode);return n}const Q=new WeakSet;const Z=new WeakMap;function tt(t){let e=t;for(;e;){const t=Z.get(e);if(t)return t;e=e.parentNode}return null}function et(t,e){null!==e?Z.set(t,e):Z.delete(t)}const nt=Symbol("$$setLoopContextAsync"),ot=Symbol("$$setLoopContext"),st=Symbol("$$getByAddress"),it=Symbol("$$setByAddress"),at=Symbol("$$connectedCallback"),rt=Symbol("$$disconnectedCallback"),lt=Symbol("$$updatedCallback"),dt=new Map,ct=new Map;function ut(t){if(!t.propName.startsWith("on"))return!1;const e=function(t){const e=t.propModifiers.filter(t=>"prevent"===t||"stop"===t).sort().join(",");return`${t.stateName}::${t.statePathName}::${e}`}(t);let o=dt.get(e);var s,i,a;void 0===o&&(s=t.stateName,i=t.statePathName,a=t.propModifiers,o=t=>{a.includes("prevent")&&t.preventDefault(),a.includes("stop")&&t.stopPropagation();const e=t.target,o=yn(e.getRootNode(),s);null===o&&n(`State element with name "${s}" not found for event handler.`);const r=tt(e);o.createStateAsync("writable",async e=>{e[ot](r,()=>{const o=e[i];return"function"!=typeof o&&n(`Handler "${i}" is not a function on state "${s}".`),Reflect.apply(o,e,[t,...r?.listIndex.indexes??[]])})})},dt.set(e,o));const r=t.propName.slice(2);t.node.addEventListener(r,o);let l=ct.get(e);return void 0===l?(l=new Set([t]),ct.set(e,l)):l.add(t),!0}const ft=new WeakMap;function ht(t){const e=ft.get(t);if(void 0!==e)return e;let n=null;try{if(t.nodeType!==Node.ELEMENT_NODE)return n;const e=t,o=e.tagName.toLowerCase();if(o.includes("-"))return n=o;if(e.hasAttribute("is")){const t=e.getAttribute("is");if(t.includes("-"))return n=t}return n}finally{ft.set(t,n)}}const pt=new Set(["radio","checkbox"]),mt=new Set(["value","valueAsNumber","valueAsDate"]);const gt=new Map,wt=new Map,bt=t=>t.detail;function xt(t){const e=ht(t.node);if(null!==e){if(void 0===customElements.get(e))return void customElements.whenDefined(e).then(()=>{xt(t)})}if(function(t,e){if(t.nodeType!==Node.ELEMENT_NODE)return!1;const o=t,s=o.tagName.toLowerCase();if("input"===s){const t=(o.getAttribute("type")||"text").toLowerCase();if("button"===t)return!1;if(pt.has(t)&&"checked"===e)return!0;if(mt.has(e))return!0}if("select"===s&&"value"===e)return!0;if("textarea"===s&&"value"===e)return!0;const i=ht(o);if(null!==i){const t=customElements.get(i);void 0===t&&n(`Custom element <${i}> is not defined. Cannot determine if property "${e}" is suitable for two-way binding.`);const o=t.wcBindable;if("wc-bindable"===o?.protocol&&1===o?.version&&o.properties.some(t=>t.name===e))return!0}return!1}(t.node,t.propName)&&-1===t.propModifiers.indexOf("ro")){const e=function(t){let e="select"===t.node.tagName.toLowerCase()?"change":"input";const o=ht(t.node);if(null!==o){const s=customElements.get(o);void 0===s&&n(`Custom element <${o}> is not defined. Cannot determine event name for two-way binding.`);const i=s.wcBindable;if("wc-bindable"===i?.protocol&&1===i?.version){const n=i.properties.find(e=>e.name===t.propName);n&&(e=n.event)}}for(const n of t.propModifiers)n.startsWith("on")&&(e=n.slice(2));return e}(t),o=function(t){const e=ht(t.node);if(null!==e){const n=customElements.get(e);if(n){const e=n.wcBindable;if("wc-bindable"===e?.protocol&&1===e?.version){const n=e.properties.find(e=>e.name===t.propName);if(n)return n.getter??bt}}}return null}(t),s=function(t,e,n){const o=t.inFilters.map(t=>t.filterName+"("+t.args.join(",")+")").join("|");return`${t.stateName}::${t.propName}::${t.statePathName}::${e}::${o}::${n?"g":"n"}`}(t,e,null!==o);let i=gt.get(s);void 0===i&&(i=((t,e,o,s,i)=>a=>{const r=a.target;if(null===r)return void console.warn("[@wcstack/state] event.target is null.");let l;if(null!==i)l=i(a);else{if(!(e in r))return void console.warn(`[@wcstack/state] Property "${e}" does not exist on target element.`);l=r[e]}let d=l;for(const t of s)d=t.filterFn(d);const c=yn(r.getRootNode(),t);null===c&&n(`State element with name "${t}" not found for two-way binding.`);const u=tt(r);c.createState("writable",t=>{t[ot](u,()=>{t[o]=d})})})(t.stateName,t.propName,t.statePathName,t.inFilters,o),gt.set(s,i)),t.node.addEventListener(e,i);let a=wt.get(s);void 0===a?(a=new Set([t]),wt.set(s,a)):a.add(t)}}const yt=new WeakMap;function It(t){return yt.get(t)??[]}function St(t,e){yt.set(t,e)}const Nt=new WeakMap;function Pt(t,e){if(Nt.has(t)){const n=Nt.get(t);if(n.has(e))return n.get(e)}else Nt.set(t,new WeakMap);const n=Object.freeze(new _t(t,e));return Nt.get(t).set(e,n),n}class _t{pathInfo;stateName;stateElement;parentAbsolutePathInfo;constructor(t,e){this.pathInfo=e,this.stateName=t.name,this.stateElement=t,null===e.parentPathInfo?this.parentAbsolutePathInfo=null:this.parentAbsolutePathInfo=Pt(t,e.parentPathInfo)}}const vt=new WeakMap,At=new WeakMap;class $t{absolutePathInfo;listIndex;_parentAbsoluteAddress;constructor(t,e){this.absolutePathInfo=t,this.listIndex=e}get parentAbsoluteAddress(){if(void 0!==this._parentAbsoluteAddress)return this._parentAbsoluteAddress;const t=this.absolutePathInfo.parentAbsolutePathInfo;if(null===t)return null;let e=null;return e=this.absolutePathInfo.pathInfo.segments[this.absolutePathInfo.pathInfo.segments.length-1]===m?this.listIndex?.parentListIndex??null:this.listIndex,this._parentAbsoluteAddress=Et(t,e)}}function Et(t,e){if(null===e){let e=At.get(t);return void 0!==e||(e=new $t(t,null),At.set(t,e)),e}{let n=vt.get(e);void 0===n&&(n=new WeakMap,vt.set(e,n));let o=n.get(t);return void 0!==o||(o=new $t(t,e),n.set(t,o)),o}}const Ct=new WeakMap;function kt(t,e){null===e?Ct.delete(t):Ct.set(t,e)}function Mt(t){return Ct.get(t)||null}const Tt=new Map;function Lt(t,e){let n,o;if(0===t.wildcardCount||0===e.wildcardCount)return 0;if(1===t.wildcardCount&&e.wildcardCount>0&&e.wildcardPathSet.has(t.path))return 1;t.id<e.id?(n=t,o=e):(n=e,o=t);const s=`${n.path}\t${o.path}`;let i=Tt.get(s);if(void 0!==i)return i;return i=n.wildcardPathSet.intersection(o.wildcardPathSet).size,Tt.set(s,i),i}const Wt=new WeakMap;function Rt(t){const e=tt(t.node);if(null===e)return null;let n=Wt.get(e);if(void 0===n)n=new WeakMap,Wt.set(e,n);else{const e=n.get(t);if(void 0!==e)return e}let o=null;try{const n=Lt(e.pathInfo,t.statePathInfo);return n>0&&(o=e.listIndex.at(n-1)),o}finally{n.set(t,o)}}const Ot=new WeakMap;function Ft(t){let e=null;if(e=Ot.get(t)||null,null!==e)return e;let o=t.replaceNode.getRootNode();if(!1===t.replaceNode.isConnected){const e=Mt(o);null===e?n(`Cannot get absolute state address for disconnected binding: ${t.bindingType} ${t.statePathName} on ${t.node.nodeName}`):o=e}const s=Rt(t),i=yn(o,t.stateName);null===i&&n(`State element with name "${t.stateName}" not found for binding.`);return e=Et(Pt(i,t.statePathInfo),s),Ot.set(t,e),e}function Dt(t){Ot.delete(t)}const jt=new WeakMap;function Bt(t,e){let n=t;for(const t of e)n=t.filterFn(n);return n}const zt=Object.freeze([]);const qt=new WeakMap,Ut=new WeakMap;class Vt{pathInfo;listIndex;_parentAddress;constructor(t,e){this.pathInfo=t,this.listIndex=e}get parentAddress(){if(void 0!==this._parentAddress)return this._parentAddress;const t=this.pathInfo.parentPathInfo;if(null===t)return null;let e=null;return e=this.pathInfo.segments[this.pathInfo.segments.length-1]===m?this.listIndex?.parentListIndex??null:this.listIndex,this._parentAddress=Ht(t,e)}}function Ht(t,e){if(null===e){let e=Ut.get(t);return void 0!==e||(e=new Vt(t,null),Ut.set(t,e)),e}{let n=qt.get(e);void 0===n&&(n=new WeakMap,qt.set(e,n));let o=n.get(t);return void 0!==o||(o=new Vt(t,e),n.set(t,o)),o}}const Jt=new WeakMap;function Xt(t){return Jt.get(t)??[]}Set.prototype.difference||(Set.prototype.difference=function(t){const e=new Set(this);for(const n of t)e.delete(n);return e}),Set.prototype.intersection||(Set.prototype.intersection=function(t){const e=new Set;for(const n of t)this.has(n)&&e.add(n);return e});let Kt=0;function Yt(){return`u${(Kt++).toString(36)}`}let Gt=0;class Qt{uuid=Yt();parentListIndex;position;length;_index;_version;_indexes;_listIndexes;constructor(t,e){this.parentListIndex=t,this.position=t?t.position+1:0,this.length=this.position+1,this._index=e,this._version=Gt}get index(){return this._index}set index(t){this._index=t,this._version=++Gt,this.indexes[this.position]=t}get version(){return this._version}get dirty(){return null!==this.parentListIndex&&(this.parentListIndex.dirty||this.parentListIndex.version>this._version)}get indexes(){return null===this.parentListIndex?void 0===this._indexes&&(this._indexes=[this._index]):(void 0===this._indexes||this.dirty)&&(this._indexes=[...this.parentListIndex.indexes,this._index],this._version=Gt),this._indexes}get listIndexes(){return null===this.parentListIndex?void 0===this._listIndexes&&(this._listIndexes=[new WeakRef(this)]):void 0===this._listIndexes&&(this._listIndexes=[...this.parentListIndex.listIndexes,new WeakRef(this)]),this._listIndexes}get varName(){return`$${this.position+1}`}at(t){return t>=0?this.listIndexes[t]?.deref()||null:this.listIndexes[this.listIndexes.length+t]?.deref()||null}}function Zt(t,e){return new Qt(t,e)}const te=new WeakMap;function ee(t){return te.get(t)||null}const ne=new WeakMap,oe=Object.freeze([]),se=new Set;function ie(t,e,n){const o=Array.isArray(e)&&e.length>0?e:oe,s=Array.isArray(n)&&n.length>0?n:oe,i=function(t,e){const n=Array.isArray(t)&&t.length>0?t:oe,o=Array.isArray(e)&&e.length>0?e:oe;let s=ne.get(n);return s&&s.get(o)||null}(o,s);if(i)return i;const a=ee(o)||[];let r;try{if(0===s.length)return r={oldIndexes:a,newIndexes:[],changeIndexSet:se,deleteIndexSet:new Set(a),addIndexSet:se};let e=ee(s);if(0===o.length){if(null===e){e=[];for(let n=0;n<s.length;n++){const o=Zt(t,n);e.push(o)}}return r={oldIndexes:a,newIndexes:e,changeIndexSet:se,deleteIndexSet:se,addIndexSet:new Set(e)}}if(function(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}(o,s))return r={oldIndexes:a,newIndexes:a,changeIndexSet:se,deleteIndexSet:se,addIndexSet:se};const n=new Map;for(let t=0;t<o.length;t++){const e=o[t];let s=n.get(e);s||(s=[],n.set(e,s)),s.push(t)}if(null!==e)return function(t,e,n,o,s){const i=new Set(o),a=new Set(n),r=new Set,l=i.difference(a),d=a.difference(i);for(let t=0;t<e.length;t++){const o=e[t],i=s.get(o),a=i&&i.length>0?i.shift():void 0;if(void 0!==a){const e=n[a];e.index!==t&&r.add(e)}}return{oldIndexes:n,newIndexes:o,changeIndexSet:r,deleteIndexSet:d,addIndexSet:l}}(0,s,a,e,n);e=[];const i=new Set,l=new Set;for(let o=0;o<s.length;o++){const r=s[o],d=n.get(r),c=d&&d.length>0?d.shift():void 0;if(void 0===c){const n=Zt(t,o);e.push(n),l.add(n)}else{const t=a[c];t.index!==o&&(t.index=o,i.add(t)),e.push(t)}}return r={oldIndexes:a,newIndexes:e,changeIndexSet:i,deleteIndexSet:new Set(a).difference(new Set(e)),addIndexSet:l}}finally{void 0!==r&&(!function(t,e,n){let o=ne.get(t);o||(o=new WeakMap,ne.set(t,o)),o.set(e,n)}(o,s,r),l=s,null!==(d=r.newIndexes)?te.set(l,d):te.delete(l))}var l,d}const ae=new WeakMap;function re(t){let e=null;return e=ae.get(t)||null,null===e&&(e=new Set,ae.set(t,e)),e}function le(t,e){re(t).add(e)}function de(t,e){re(t).delete(e)}const ce=new WeakMap;function ue(t){ce.delete(t)}const fe=new WeakMap;function he(t){return fe.get(t)??[]}const pe=new WeakMap;function me(t){return pe.get(t)??[]}function ge(t,e,n){!function(t,e){const n=me(t);for(const t of n)et(t,e)}(t,e);const o=he(t);for(const t of o){le(Ft(t),t),Be(t,n)}}function we(t){if(!t.mounted)return;const e=he(t);for(const t of e){de(Ft(t),t),Dt(t),ue(t)}!function(t){const e=me(t);for(const t of e)et(t,null)}(t)}const be=new WeakMap,xe=Object.freeze(new Set);function ye(t){const e=be.get(t);return void 0!==e?e:xe}const Ie=new Set(["if","elseif","else","for"]);class Se{_content;_childNodeArray=[];_firstNode=null;_lastNode=null;_mounted=!1;constructor(t){this._content=t,this._childNodeArray=Array.from(this._content.childNodes),this._firstNode=this._childNodeArray.length>0?this._childNodeArray[0]:null,this._lastNode=this._childNodeArray.length>0?this._childNodeArray[this._childNodeArray.length-1]:null}get firstNode(){return this._firstNode}get lastNode(){return this._lastNode}get mounted(){return this._mounted}appendTo(t){for(const e of this._childNodeArray)t.appendChild(e);this._mounted=!0}mountAfter(t){const e=t.parentNode,n=t.nextSibling;if(e)for(const t of this._childNodeArray)e.insertBefore(t,n);this._mounted=!0}unmount(){for(const t of this._childNodeArray)null!==t.parentNode&&t.parentNode.removeChild(t);const t=he(this);for(const e of t){if(Ie.has(e.bindingType)){const t=ye(e.node);for(const e of t)e.unmount()}ue(e),Dt(e)}this._mounted=!1}}function Ne(t){void 0!==t.uuid&&null!==t.uuid||n("BindingInfo.uuid is null.");const e=K(t.uuid);e||n(`Fragment with UUID "${t.uuid}" not found.`);const o=document.importNode(e.fragment,!0),s=function(t,e){const[o,s]=function(t,e){const o=[],s=[];for(const i of e){const e=d(t,i.nodePath);if(null===e&&n(`Node not found by path [${i.nodePath.join(", ")}] in fragment.`),!Q.has(e)){Q.add(e);const t=c(e,i.parseBindTextResults);h(e,t),r(e),s.push(...t),o.push(e)}}return[o,s]}(t,e);return Ge(s),{nodes:o,bindingInfos:s}}(o,e.nodeInfos),i=new Se(o);!function(t,e){fe.set(t,e)}(i,s.bindingInfos);const a=[];for(const t of s.bindingInfos)t.statePathName in b&&a.push(t);return function(t,e){Jt.set(t,e)}(i,a),function(t,e){pe.set(t,e)}(i,s.nodes),function(t,e){const n=be.get(t);n?n.add(e):be.set(t,new Set([e]))}(t.node,i),i}const Pe=new WeakMap,_e=new WeakMap,ve=new WeakMap,Ae=new WeakMap;function $e(t){return ve.get(t.node)||[]}function Ee(t,e){const n=ve.get(t.node);void 0===n?ve.set(t.node,[e]):n.push(e)}function Ce(t,e){let n=_e.get(t);if(void 0===n)return null;const o=n.get(e);return void 0===o?null:o}function ke(t,e,n){let o=_e.get(t);if(void 0===o){if(null===n)return;o=new WeakMap,_e.set(t,o)}null===n?o.delete(e):o.set(e,n)}const Me=new WeakMap;function Te(t){return`${t.bindingType} ${t.statePathName} ${t.outFilters.map(t=>t.filterName).join("|")} ${t.node.isConnected?"(connected)":"(disconnected)"}`}function Le(t,n,o){const s=t.node.isConnected,i=Boolean(o);let a;const r=ye(t.node);a=0===r.size?Ne(t):r.values().next().value;try{if(i||(e.debug&&console.log(`unmount if content : ${Te(t)}`),we(a),a.unmount()),i){e.debug&&console.log(`mount if content : ${Te(t)}`),a.mountAfter(t.node);ge(a,tt(t.node),n)}}finally{Me.set(t.node,s)}}function We(t,n,o){const s=t.node,i=t.propSegments;if(1===i.length){const t=i[0];if(s[t]!==o)try{s[t]=o}catch(n){e.debug&&console.warn(`Failed to set property '${t}' on element.`,{element:s,newValue:o,error:n})}return}let a=s[i[0]];for(let t=1;t<i.length-1;t++){const e=i[t];if(null==a)return;a=a[e]}const r=a[i[i.length-1]];if(r!==o){if(Object.isFrozen(a))return void(e.debug&&console.warn("Attempting to set property on frozen object.",{element:s,propSegments:i,oldValue:r,newValue:o}));try{a[i[i.length-1]]=o}catch(t){e.debug&&console.warn("Failed to set property on sub-object.",{element:s,propSegments:i,oldValue:r,newValue:o,error:t})}}}function Re(t,e,o){const s=t.node,i=t.propSegments;i.length<=1&&n(`Invalid propSegments for web component binding: ${i.join(".")}`);const[a,...r]=i,l=s[a];void 0===l&&n(`Property "${a}" not found on web component.`),l[r.join(".")]=o}function Oe(t,e){const o=function(t){let e=null;if(e=ce.get(t)||null,null!==e)return e;if(t.statePathInfo.wildcardCount>0){const o=Rt(t);null===o&&n(`Cannot resolve state address for binding with wildcard statePathName "${t.statePathName}" because list index is null.`),e=Ht(t.statePathInfo,o)}else e=Ht(t.statePathInfo,null);return ce.set(t,e),e}(e);if(o.pathInfo.path in b){const t=tt(e.node);return null===t&&n(`ListIndex not found for binding: ${e.statePathName}`),function(t,e){null===t.listIndex&&n("ListIndex not found for loopContext:");const o=b[e];void 0===o&&n(`Invalid index name: ${e}`);const s=t.listIndex.at(o);return null===s&&n(`Index not found at position ${o} for loopContext:`),s.index}(t,o.pathInfo.path)}return t[st](o)}const Fe={class:function(t,e,o){const s=t.node,i=t.propSegments[1];"boolean"!=typeof o&&n("Invalid value for class application: expected boolean, got "+typeof o),s.classList.toggle(i,o)},attr:function(t,e,n){const o=t.node,s=t.propSegments[1];o.getAttribute(s)!==n&&o.setAttribute(s,n)},style:function(t,e,n){const o=t.propSegments[1],s=t.node.style;s[o]!==n&&(s[o]=n)}},De={text:function(t,e,n){t.replaceNode.nodeValue!==n&&(t.replaceNode.nodeValue=n)},for:function(t,e,o){const s=t.statePathInfo,i=Rt(t),a=Ft(t),r=It(a),l=ie(i,r,o);if(e.newListValueByAbsAddress.set(a,Array.isArray(o)?o:[]),Array.isArray(r)&&r.length===l.deleteIndexSet.size&&l.deleteIndexSet.size>0&&null!==t.node.parentNode){let e=Ae.get(t.node);if(void 0===e){const n=Pe.get(t.node)||t.node;e=function(t,e){let n=t.previousSibling,o=e.nextSibling,s=!0;for(;null!==n;){if(n.nodeType===Node.ELEMENT_NODE||n.nodeType===Node.TEXT_NODE&&""!==(n.textContent?.trim()??"")){s=!1;break}n=n.previousSibling}for(;null!==o;){if(o.nodeType===Node.ELEMENT_NODE||o.nodeType===Node.TEXT_NODE&&""!==(o.textContent?.trim()??"")){s=!1;break}o=o.nextSibling}return s}(t.node,n),Ae.set(t.node,e)}if(e){const e=t.node.parentNode;e.textContent="",e.appendChild(t.node)}}for(const e of l.deleteIndexSet){const n=Ce(t.node,e);null!==n&&(we(n),n.unmount(),Ee(t,n),ke(t.node,e,null))}let d=t.node;const c=P(s.path+"."+m),u=e.stateElement.loopContextStack;let f=null;l.newIndexes.length==l.addIndexSet.size&&l.newIndexes.length>0&&d.isConnected&&(f=document.createDocumentFragment(),kt(f,e.rootNode));for(const o of l.newIndexes){let i;if(l.addIndexSet.has(o)){const a=Ht(c,o);u.createLoopContext(a,n=>{const o=$e(t);i=o.pop(),void 0===i&&(i=Ne(t)),null!==f?i.appendTo(f):d.nextSibling!==i.firstNode&&i.mountAfter(d),ge(i,n,e)}),void 0===i&&n(`Content not found for ListIndex: ${o.index} at path "${s.path}"`)}else{if(i=Ce(t.node,o),l.changeIndexSet.has(o)){const t=Xt(i);for(const n of t)Be(n,e)}null===i&&n(`Content not found for ListIndex: ${o.index} at path "${s.path}"`),d.nextSibling!==i.firstNode&&i.mountAfter(d)}d=i.lastNode||d,ke(t.node,o,i)}Pe.set(t.node,d),null!==f&&(t.node.parentNode.insertBefore(f,t.node.nextSibling),kt(f,null))},if:Le,else:Le,elseif:Le,radio:function(t,e,n){const o=t.node,s=Bt(o.value,t.inFilters);o.checked=n===s},checkbox:function(t,e,n){const o=t.node,s=Bt(o.value,t.inFilters),i=Array.isArray(n)?n:zt;o.checked=i.includes(s)}};function je(t,e){const n=Bt(Oe(e.state,t),t.outFilters);let o=De[t.bindingType];if(void 0===o){const n=t.propSegments[0];o=Fe[n],void 0===o&&(o=function(t,e){const n=jt.get(t);return!!n&&!0===n.get(e)}(t.replaceNode,e.stateElement)?Re:We)}if(o===We){if("SELECT"===t.node.tagName){const o=t.propSegments[0];if("value"===o||"selectedIndex"===o)return void e.deferredSelectBindings.push({binding:t,value:n})}}o(t,e,n)}function Be(t,e){if(e.appliedBindingSet.has(t))return;e.appliedBindingSet.add(t);const o=Ft(t);if(e.updatedAbsAddressSetByStateElement.has(e.stateElement)){e.updatedAbsAddressSetByStateElement.get(e.stateElement).add(o)}else e.updatedAbsAddressSetByStateElement.set(e.stateElement,new Set([o]));if("event"===t.bindingType)return;const s=ht(t.replaceNode);if(s&&void 0===customElements.get(s))return;let i=t.replaceNode.getRootNode();if(i instanceof DocumentFragment&&!(i instanceof ShadowRoot)&&(i=Mt(i),null===i&&n("Root node for fragment not found for binding.")),t.stateName!==e.stateName||i!==e.rootNode){const o=yn(i,t.stateName);null===o&&n(`State element with name "${t.stateName}" not found for binding.`),o.createState("readonly",n=>{const s={stateName:t.stateName,rootNode:i,stateElement:o,state:n,appliedBindingSet:e.appliedBindingSet,newListValueByAbsAddress:e.newListValueByAbsAddress,updatedAbsAddressSetByStateElement:e.updatedAbsAddressSetByStateElement,deferredSelectBindings:e.deferredSelectBindings};je(t,s)})}else je(t,e)}function ze(t){let o=0;const s=new Set,i=new Map,a=new Map,r=[];for(;o<t.length;){let l=t[o];const d=l.stateName;if(!1===l.replaceNode.isConnected){e.debug&&console.log(`applyChangeFromBindings: skip disconnected binding: ${l.bindingType} ${l.statePathName} on ${l.node.nodeName}`,l),o++;continue}let c=l.replaceNode.getRootNode();c instanceof DocumentFragment&&!(c instanceof ShadowRoot)&&(c=Mt(c),null===c&&n("Root node for fragment not found for binding."));const u=yn(c,d);null===u&&n(`State element with name "${d}" not found for binding.`),u.createState("readonly",e=>{const n={rootNode:c,stateName:d,stateElement:u,state:e,appliedBindingSet:s,newListValueByAbsAddress:i,updatedAbsAddressSetByStateElement:a,deferredSelectBindings:r};for(;;){Be(l,n),o++;const e=t[o];if(!e)break;const s=e.replaceNode.getRootNode();if(e.stateName!==d||s!==n.rootNode)break;l=e}})}for(const{binding:t,value:e}of r)We(t,0,e);for(const[t,e]of i.entries())St(t,e);for(const[t,e]of a.entries())t.createState("writable",t=>{t[lt](Array.from(e))})}const qe=new Map,Ue=new Map;const Ve=(t,e,o)=>s=>{const i=s.target;if(null===i)return void console.warn("[@wcstack/state] event.target is null.");if("radio"!==i.type)return void console.warn("[@wcstack/state] event.target is not a radio input element.");if(!1===i.checked)return;let a=i.value;for(const t of o)a=t.filterFn(a);const r=yn(i.getRootNode(),t);null===r&&n(`State element with name "${t}" not found for two-way binding.`);const l=tt(i);r.createState("writable",t=>{t[ot](l,()=>{t[e]=a})})};function He(t){if("radio"===t.bindingType&&-1===t.propModifiers.indexOf("ro")){const e=function(t){let e="input";for(const n of t.propModifiers)n.startsWith("on")&&(e=n.slice(2));return e}(t),n=function(t,e){const n=t.inFilters.map(t=>t.filterName+"("+t.args.join(",")+")").join("|");return`${t.stateName}::${t.statePathName}::${e}::${n}`}(t,e);let o=qe.get(n);void 0===o&&(o=Ve(t.stateName,t.statePathName,t.inFilters),qe.set(n,o)),t.node.addEventListener(e,o);let s=Ue.get(n);return void 0===s?(s=new Set([t]),Ue.set(n,s)):s.add(t),!0}return!1}const Je=new Map,Xe=new Map;const Ke=(t,e,o)=>s=>{const i=s.target;if(null===i)return void console.warn("[@wcstack/state] event.target is null.");if("checkbox"!==i.type)return void console.warn("[@wcstack/state] event.target is not a checkbox input element.");const a=i.checked;let r=i.value;for(const t of o)r=t.filterFn(r);const l=yn(i.getRootNode(),t);null===l&&n(`State element with name "${t}" not found for two-way binding.`);const d=tt(i);l.createState("writable",t=>{t[ot](d,()=>{let n=t[e];if(Array.isArray(n))if(a)-1===n.indexOf(r)&&(t[e]=n.concat(r));else{const o=n.indexOf(r);-1!==o&&(t[e]=n.toSpliced(o,1))}else t[e]=a?[r]:[]})})};function Ye(t){if("checkbox"===t.bindingType&&-1===t.propModifiers.indexOf("ro")){const e=function(t){let e="input";for(const n of t.propModifiers)n.startsWith("on")&&(e=n.slice(2));return e}(t),n=function(t,e){const n=t.inFilters.map(t=>t.filterName+"("+t.args.join(",")+")").join("|");return`${t.stateName}::${t.statePathName}::${e}::${n}`}(t,e);let o=Je.get(n);void 0===o&&(o=Ke(t.stateName,t.statePathName,t.inFilters),Je.set(n,o)),t.node.addEventListener(e,o);let s=Xe.get(n);return void 0===s?(s=new Set([t]),Xe.set(n,s)):s.add(t),!0}return!1}function Ge(t){for(const e of t)l(e),ut(e)||(xt(e),He(e),Ye(e))}function Qe(t,e){const[o,s]=function(t){const e=G(t),n=[];for(const t of e)if(!Q.has(t)){Q.add(t);const e=c(t,Y(t));h(t,e),r(t),n.push(...e)}return[e,n]}(t);for(const t of o)et(t,e);Ge(s);for(const t of s){le(Ft(t),t);const e=yn(t.replaceNode.getRootNode(),t.stateName);null===e&&n(`State element with name "${t.stateName}" not found for binding.`),"event"!==t.bindingType&&e.setPathInfo(t.statePathName,t.bindingType)}ze(s)}const Ze=/\{\{\s*(.+?)\s*\}\}/g,tn=new Set(["SCRIPT","STYLE"]);function en(t){if(!e.enableMustache)return;!function(t){const e=document.createTreeWalker(t,NodeFilter.SHOW_TEXT),n=[];for(;e.nextNode();)n.push(e.currentNode);for(const t of n)t.parentElement&&tn.has(t.parentElement.tagName)||nn(t)}(t);const n=Array.from(t.querySelectorAll("template"));for(const t of n)if("http://www.w3.org/2000/svg"===t.namespaceURI){const e=document.createElement("template"),n=Array.from(t.childNodes);for(let t=0;t<n.length;t++){const o=n[t];e.content.appendChild(o)}for(const n of t.attributes)e.setAttribute(n.name,n.value);t.replaceWith(e),en(e.content)}else en(t.content)}function nn(t){const e=t.data;if(Ze.lastIndex=0,!Ze.test(e))return;Ze.lastIndex=0;const n=document.createDocumentFragment();let o,s=0;for(;null!==(o=Ze.exec(e));){o.index>s&&n.appendChild(document.createTextNode(e.slice(s,o.index)));const t=o[1];n.appendChild(document.createComment(`@@: ${t}`)),s=o.index+o[0].length}s<e.length&&n.appendChild(document.createTextNode(e.slice(s))),t.parentNode.replaceChild(n,t)}let on;const sn=/^(\s*@@\s*(?:.*?)\s*:\s*)(.+?)(\s*)$/;function an(t,e){const n=e+p+m,o=t.indexOf("|"),s=t.indexOf("@");let i,a;if(-1!==o?(i=t.slice(0,o).trim(),a=t.slice(o)):-1!==s?(i=t.slice(0,s).trim(),a=t.slice(s)):(i=t.trim(),a=""),"."===i)i=n;else{if(!i.startsWith("."))return t;i=n+p+i.slice(1)}return a.length>0?i+a:i}function rn(t,e){const n=sn.exec(t);if(null===n)return t;const o=n[1],s=n[2],i=n[3];return o+an(s,e)+i}function ln(t,e){const n=t.split(";");let o=!1;const s=n.map(t=>{const n=t.trim();if(0===n.length)return t;const s=n.indexOf(":");if(-1===s)return t;const i=n.slice(0,s).trim(),a=n.slice(s+1).trim(),r=an(a,e);return r!==a?(o=!0,`${i}: ${r}`):t});return o?s.join(";"):t}function dn(t,e){return ln(t,e)}function cn(t){let e=t;const n=[];for(;null!==e.parentNode;){const t=Array.from(e.parentNode.childNodes).indexOf(e);n.unshift(t),e=e.parentNode}return n}function un(t){const e=[],n=G(t);for(const t of n){const n=Y(t);e.push({nodePath:cn(t),parseBindTextResults:n})}return e}const fn=new Map([["for",e.commentForPrefix],["if",e.commentIfPrefix],["elseif",e.commentElseIfPrefix],["else",e.commentElsePrefix]]),hn=function(){if(on)return on;const t=[],e=W("not",t)(T);return on={filterName:"not",args:t,filterFn:e},on}();function pn(t,e){const n=e.outFilters;return{...e,outFilters:[...n,hn],bindingType:t}}function mn(t,n,o,s){!function(t){const e=Array.from(t.childNodes);for(const n of e)n.nodeType===Node.TEXT_NODE&&""===(n.textContent||"").trim()&&t.removeChild(n)}(n),"string"==typeof s&&function(t,n){const o=e.bindAttributeName,s=document.createTreeWalker(t,NodeFilter.SHOW_COMMENT|NodeFilter.SHOW_ELEMENT);for(;s.nextNode();){const t=s.currentNode;if(t.nodeType===Node.COMMENT_NODE){const e=t;e.data=rn(e.data,n);continue}const e=t;if(e instanceof HTMLTemplateElement)continue;const i=e.getAttribute(o);if(null!==i){const t=ln(i,n);t!==i&&e.setAttribute(o,t)}}}(n,s),gn(t,n,s);return{fragment:n,parseBindTextResult:o,nodeInfos:un(n)}}function gn(t,o,s){const i=e.commentElsePrefix,a=document.createTreeWalker(o,NodeFilter.SHOW_ELEMENT,{acceptNode(t){const n=t;if("template"===n.tagName.toLowerCase()){if((n.getAttribute(e.bindAttributeName)||"").length>0)return NodeFilter.FILTER_ACCEPT}return NodeFilter.FILTER_SKIP}});let r=null;const l=[],d=[];for(;a.nextNode();){const t=a.currentNode;d.push(t)}for(const o of d){let a=o.getAttribute(e.bindAttributeName)||"";"string"==typeof s&&(a=dn(a,s));let d=z(a)[0];const c=fn.get(d.bindingType);if(void 0===c)continue;const u=d.bindingType,f=o.content,h=Yt();let p=null;const m="for"===u?d.statePathName:s;if("else"===u){null===r&&n("'else' binding found without preceding 'if' or 'elseif' binding."),d=pn("else",r.parseBindTextResult),p=mn(t,f,d,m),X(h,t,p);const e=l.at(-1),s=document.createComment(`@@${c}:${h}`);void 0!==e?(o.remove(),e.fragment.appendChild(s),e.nodeInfos.push({nodePath:cn(s),parseBindTextResults:Y(s)})):o.replaceWith(s)}else if("elseif"===u){null===r&&n("'elseif' binding found without preceding 'if' or 'elseif' binding."),p=mn(t,f,d,m),X(h,t,p);const e=document.createComment(`@@${c}:${h}`),s=Yt(),a={fragment:document.createDocumentFragment(),parseBindTextResult:pn("else",r.parseBindTextResult),nodeInfos:[]};a.fragment.appendChild(e),a.nodeInfos.push({nodePath:cn(e),parseBindTextResults:Y(e)}),X(s,t,a);const u=l.at(-1);l.push(a);const g=document.createComment(`@@${i}:${s}`);void 0!==u?(o.remove(),u.fragment.appendChild(g),u.nodeInfos.push({nodePath:cn(g),parseBindTextResults:Y(g)})):o.replaceWith(g)}else{p=mn(t,f,d,m),X(h,t,p);const e=document.createComment(`@@${c}:${h}`);o.replaceWith(e)}"if"===u?(l.length=0,r=p):"elseif"===u?r=p:"else"===u&&(r=null,l.length=0)}}async function wn(t){const n=t.querySelectorAll(e.tagNames.state),o=[];await customElements.whenDefined(e.tagNames.state);for(const t of n){const e=t;o.push(e.initializePromise)}await Promise.all(o)}async function bn(t){if(t===document)await wn(document),en(document),gn(document,document),Qe(document.body,null);else{const n=t;n.host.hasAttribute(e.bindAttributeName)&&await a(n.host),await wn(n),en(n),gn(n,n),Qe(n,null)}}const xn=new WeakMap;function yn(t,e){let n=xn.get(t);return n&&n.get(e)||null}function In(t,o,s){let i=xn.get(t);if(null===s){if(!i)return;i.delete(o),0===i.size&&xn.delete(t),e.debug&&console.debug(`State element unregistered: name="${o}"`)}else i||(i=new Map,xn.set(t,i),("HTMLDocument"===t.constructor.name||"Document"===t.constructor.name||"ShadowRoot"===t.constructor.name)&&queueMicrotask(()=>{bn(t)})),i.has(o)&&n(`State element with name "${o}" is already registered.`),i.set(o,s),e.debug&&console.debug(`State element registered: name="${o}"`,s)}class Sn{_loopContextStack=Array(g).fill(void 0);_length=0;createLoopContext(t,e){null===t.listIndex&&n("Cannot create loop context for a state address that does not have a list index.");const o=t;this._length>=g&&n("Exceeded maximum loop context stack depth of 128. Possible infinite loop.");const s=this._loopContextStack[this._length-1];if(void 0!==s){s.pathInfo.wildcardCount+1!==o.pathInfo.wildcardCount&&n("Cannot push loop context for a list whose wildcard count is not exactly one more than the current active loop context.");const t=o.pathInfo.wildcardPathInfos[o.pathInfo.wildcardPathInfos.length-2];s.pathInfo!==t&&n("Cannot push loop context for a list whose parent wildcard path info does not match the current active loop context.")}else 1!==o.pathInfo.wildcardCount&&n("Cannot push loop context for a list with wildcard positions when there is no active loop context.");let i;this._loopContextStack[this._length]=o,this._length++;try{i=e(o)}finally{i instanceof Promise?i.finally(()=>{this._length--,this._loopContextStack[this._length]=void 0}):(this._length--,this._loopContextStack[this._length]=void 0)}return i}}const Nn=new Map;class Pn{path;segments;paths;wildcardCount;wildcardType;wildcardIndexes;pathInfo;constructor(t){const e=t.split("."),n=e.slice(),o=[];let s=0,i=0,a="",r=0,l="none";const d=[];for(let t=0;t<e.length;t++){const l=e[t];if("*"===l)n[t]="*",d.push(null),s++,r++;else{const e=Number(l);Number.isNaN(e)||(n[t]="*",d.push(e),i++,r++)}a+=l,o.push(a),a+=t<l.length-1?".":""}const c=P(n.join("."));(s>0||i>0)&&(l=s===r?"context":i===r?"all":"partial"),this.path=t,this.segments=e,this.paths=o,this.wildcardCount=r,this.wildcardType=l,this.wildcardIndexes=d,this.pathInfo=c}}function _n(t){let e;return Nn.get(t)??(Nn.set(t,e=new Pn(t)),e)}const vn=new WeakMap;function An(t,e){null===e?vn.delete(t):vn.set(t,e)}function $n(t){const e=vn.get(t);e&&(e.dirty=!0)}function En(t,e,o,s,i){if(!(e.pathInfo.path in t)){const i=kn(t,e.parentAddress??n(`address.parentAddress is undefined path: ${e.pathInfo.path}`),o,s),a=e.pathInfo.segments[e.pathInfo.segments.length-1];if(a===m){const t=e.listIndex?.index??n(`address.listIndex?.index is undefined path: ${e.pathInfo.path}`);return Reflect.get(i,t)}return Reflect.get(i,a)}if(!i.getterPaths.has(e.pathInfo.path))return Reflect.get(t,e.pathInfo.path);s.pushAddress(e);try{return Reflect.get(t,e.pathInfo.path,o)}finally{s.popAddress()}}function Cn(t,e,n,o,s){const i=Et(Pt(s,e.pathInfo),e.listIndex),a=function(t){return vn.get(t)??null}(i);if(null!==a&&!1===a.dirty)return a.value;const r=En(t,e,n,o,s);return An(i,{value:r,dirty:!1}),r}function kn(t,e,n,o){!function(t,e){if(t.addressStackLength>0){const n=t.lastAddressStack?.pathInfo??null,o=t.stateElement;null!==n&&o.getterPaths.has(n.path)&&n.path!==e.pathInfo.path&&o.addDynamicDependency(e.pathInfo.path,n.path)}}(o,e);const s=o.stateElement;return e.pathInfo.wildcardCount>0||s.getterPaths.has(e.pathInfo.path)?Cn(t,e,n,o,s):En(t,e,n,o,s)}function Mn(t,e){if(0===t.addressStackLength)return null;const n=t.lastAddressStack;if(null===n)return null;const o=n.pathInfo.indexByWildcardPath[e];return void 0===o?null:n.listIndex?.at(o)??null}const Tn=new class{_queueAbsoluteAddresses=[];constructor(){}enqueueAbsoluteAddress(t){const e=0===this._queueAbsoluteAddresses.length;this._queueAbsoluteAddresses.push(t),e&&queueMicrotask(()=>{const t=this._queueAbsoluteAddresses;this._queueAbsoluteAddresses=[],this._applyChange(t)})}testApplyChange(t){this._applyChange(t)}_applyChange(t){const e=new Set(t),n=[];for(const t of e){const e=re(t);for(const t of e)!1!==t.replaceNode.isConnected&&n.push(t)}ze(n)}};function Ln(){return Tn}const Wn=new WeakMap;function Rn(t,e){null===e?Wn.delete(t):Wn.set(t,e)}function On(t,n,o){const s=P(t.wildcardParentPaths[n]),i=Pt(t.stateElement,s),a=Ht(s,o),r=It(Et(i,o)),l=t.stateProxy[st](a),d=function(t,n){switch(n){case"old":return t.oldIndexes;case"new":return t.newIndexes;case"add":return t.addIndexSet;case"change":return t.changeIndexSet;case"delete":return t.deleteIndexSet;default:return e.debug&&console.log(`Invalid search type: ${n}`),[]}}(ie(a.listIndex,r,l),t.searchType);if(n===t.wildcardPaths.length-1)t.targetListIndexes.push(...d);else for(const e of d)On(t,n+1,e)}function Fn(t,e,o,s,i,a,r,l,d){const c={stateElement:e,staticMap:s,dynamicMap:i,result:new Set,listPathSet:a,visited:new Set,stateProxy:r,searchType:l};return function(t,e,o){const s=[{address:e,depth:0}];for(;s.length>0;){const{address:e,depth:i}=s.pop();if(i>1e3&&n(`Maximum dependency depth of 1000 exceeded. Possible circular dependency detected at path: ${e.pathInfo.path}`),t.visited.has(e))continue;t.visited.add(e),o(e);const a=e.pathInfo.path,r=i+1,l=[],d=t.staticMap.get(a);if(d)for(const n of d){const o=P(n);if(t.listPathSet.has(a)&&o.lastSegment===m){const n=t.stateProxy[st](e),s=It(Et(Pt(t.stateElement,e.pathInfo),e.listIndex)),i=ie(e.listIndex,s,n);for(const e of i.newIndexes){const n=Ht(o,e);t.result.add(n),l.push({address:n,depth:r})}}else{const n=Ht(o,e.listIndex);t.result.add(n),l.push({address:n,depth:r})}}const c=t.dynamicMap.get(a);if(c)for(const o of c){const s=P(o),i=[];if(s.wildcardCount>0){const o=Lt(e.pathInfo,s);if(s.wildcardCount-o>=1){let a;o>0?(null===e.listIndex&&n(`Cannot expand dynamic dependency with wildcard for non-list address: ${e.pathInfo.path}`),a=e.listIndex.at(o-1)):a=null;const r={stateElement:t.stateElement,targetListIndexes:[],wildcardPaths:s.wildcardPaths,wildcardParentPaths:s.wildcardParentPaths,stateProxy:t.stateProxy,searchType:t.searchType};On(r,o,a),i.push(...r.targetListIndexes)}else{null===e.listIndex&&n(`Cannot expand dynamic dependency with wildcard for non-list address: ${e.pathInfo.path}`);const t=e.listIndex.at(o-1);i.push(t)}}else i.push(null);for(const e of i){const n=Ht(s,e);t.result.add(n),l.push({address:n,depth:r})}}for(let t=l.length-1;t>=0;t--)s.push(l[t])}}(c,o,d),Array.from(c.result)}function Dn(t,e,o,s,i,a){try{if(!(e.pathInfo.path in t)){const o=kn(t,e.parentAddress??n(`address.parentAddress is undefined path: ${e.pathInfo.path}`),i,a),r=e.pathInfo.segments[e.pathInfo.segments.length-1];if(r===m){const t=e.listIndex?.index??n(`address.listIndex?.index is undefined path: ${e.pathInfo.path}`);return Reflect.set(o,t,s)}return Reflect.set(o,r,s)}if(!a.stateElement.setterPaths.has(e.pathInfo.path))return Reflect.set(t,e.pathInfo.path,s);a.pushAddress(e);try{return Reflect.set(t,e.pathInfo.path,s,i)}finally{a.popAddress()}}finally{const t=Ln();t.enqueueAbsoluteAddress(o),Fn(a.stateName,a.stateElement,e,a.stateElement.staticDependency,a.stateElement.dynamicDependency,a.stateElement.listPaths,i,"new",n=>{if(n===e)return;const o=Et(Pt(a.stateElement,n.pathInfo),n.listIndex);$n(o),t.enqueueAbsoluteAddress(o)})}}function jn(t,e,o,s,i,a){let r=e.parentAddress??n(`address.parentAddress is undefined path: ${e.pathInfo.path}`),l=function(t){return Wn.get(t)??null}(r);if(null===l){const e=kn(t,r,i,a)??[],n=ee(e)??[];l={value:[...e],listIndexes:[...n]},Rn(r,l)}try{return Dn(t,e,o,s,i,a)}finally{const n=l.value.indexOf(s),o=kn(t,r,i,a)??[],d=Array.isArray(o)?ee(o)??[]:[],c=e.listIndex.index,u=-1!==n?l.listIndexes[n]:Zt(r.listIndex,-1);d[c]=u;if(new Set(o).size===l.value.length){for(let t=0;t<d.length;t++)d[t].index=t;Rn(r,null)}}}function Bn(t,e,n,o,s){const i=s.stateElement,a=i.elementPaths.has(e.pathInfo.path),r=e.pathInfo.wildcardCount>0||i.getterPaths.has(e.pathInfo.path),l=Et(Pt(i,e.pathInfo),e.listIndex);try{return a?jn(t,e,l,n,o,s):Dn(t,e,l,n,o,s)}finally{r&&An(l,{value:n,dirty:!1})}}function zn(t,e,o,s){return(e,i,a)=>{const r=P(e);if(s.addressStackLength>0){const t=s.lastAddressStack?.pathInfo??null,e=s.stateElement;null!==t&&t.path!==r.path&&e.getterPaths.has(t.path)&&e.addDynamicDependency(r.path,t.path)}r.wildcardParentPathInfos.length>i.length&&n(`indexes length is insufficient: ${e}`);let l=null;for(let e=0;e<r.wildcardParentPathInfos.length;e++){const a=r.wildcardParentPathInfos[e],d=Ht(a,l),c=ee(kn(t,d,o,s));null==c&&n(`ListIndexes not found: ${a.path}`);l=c[i[e]]??n(`ListIndex not found: ${a.path}`)}const d=Ht(r,l);if(!(void 0!==a))return kn(t,d,o,s);Bn(t,d,a,o,s)}}const qn=new WeakMap;function Un(t,e,o,s){const i=e.pathInfo;switch(e.wildcardType){case"none":return null;case"context":return Mn(s,i.wildcardPaths.at(-1)??n(`lastWildcardPath is null: ${e.pathInfo.path}`))??n(`ListIndex not found: ${e.pathInfo.path}`);case"all":{let i=null;for(let a=0;a<e.pathInfo.wildcardCount;a++){const r=e.pathInfo.wildcardParentPathInfos[a]??n(`wildcardParentPathInfo is null: ${e.pathInfo.path}`);i=(ee(kn(t,Ht(r,i),o,s))??n(`ListIndex not found: ${r.path}`))[e.wildcardIndexes[a]??n(`wildcardIndex is null: ${e.pathInfo.path}`)]??n(`ListIndex not found: ${r.path}`)}return i}case"partial":n(`Partial wildcard type is not supported yet: ${e.pathInfo.path}`)}}function Vn(t,e,o){void 0!==t.loopContext&&n("already in loop context"),t.setLoopContext(e);try{t.pushAddress(e);try{return o()}finally{t.popAddress()}}finally{t.clearLoopContext()}}function Hn(t,e,o,s){const i=b[e];if(void 0!==i){0===s.addressStackLength&&n(`No active state reference to get list index for "${e.toString()}".`);const t=s.lastAddressStack?.listIndex;return t?.indexes[i]??n(`ListIndex not found: ${e.toString()}`)}if("string"==typeof e){if("$"!==e[0]){const n=_n(e),i=Un(t,n,o,s),a=Ht(n.pathInfo,i);return kn(t,a,o,s)}switch(e){case"$stateElement":return s.stateElement;case"$getAll":return(e,i)=>function(t,e,o,s){const i=zn(t,0,o,s);return(e,a)=>{const r=new Map,l=P(e);if(s.addressStackLength>0){const t=s.lastAddressStack?.pathInfo??null,e=s.stateElement;null!==t&&t.path!==l.path&&e.getterPaths.has(t.path)&&e.addDynamicDependency(l.path,t.path)}if(void 0===a){for(let t=0;t<l.wildcardParentPathInfos.length;t++){const e=l.wildcardParentPathInfos[t],n=Mn(s,e.path);if(n){a=n.indexes;break}}void 0===a&&(a=[])}const d=(e,i,a,l,c,u,f)=>{const h=e[i]??null;if(null===h)return void f.push(u);const p=Ht(h,a),m=qn.get(p),g=kn(t,p,o,s),w=ie(a,m,g).newIndexes,b=l[c]??null;if(r.set(p,g),null===b)for(let t=0;t<w.length;t++){const n=w[t];d(e,i+1,n,l,c+1,u.concat(n.index),f)}else{const t=w[b]??n(`ListIndex not found: ${h.path}`);i+1<e.length?d(e,i+1,t,l,c+1,u.concat(t.index),f):f.push(u.concat(t.index))}},c=[];d(l.wildcardParentPathInfos,0,null,a,0,[],c);const u=[];for(let t=0;t<c.length;t++)u.push(i(l.path,c[t]));for(const[t,e]of r.entries())qn.set(t,e);return u}}(t,0,o,s)(e,i);case"$postUpdate":return e=>function(t,e,n,o){const s=o.stateElement;return e=>{const i=_n(e),a=Un(t,i,n,o),r=Ht(i.pathInfo,a),l=Et(Pt(s,r.pathInfo),r.listIndex),d=Ln();d.enqueueAbsoluteAddress(l),Fn(o.stateName,o.stateElement,r,o.stateElement.staticDependency,o.stateElement.dynamicDependency,o.stateElement.listPaths,n,"new",t=>{const e=Et(Pt(s,t.pathInfo),t.listIndex);$n(e),d.enqueueAbsoluteAddress(e)})}}(t,0,o,s)(e);case"$resolve":return(e,n,i)=>zn(t,0,o,s)(e,n,i);case"$trackDependency":return t=>function(t,e,o,s){return t=>{0===s.addressStackLength&&n(`No active state reference to track dependency for path "${t}".`);const e=s.lastAddressStack?.pathInfo??n("Internal error: lastAddressStack is null"),o=s.stateElement;s.stateElement.getterPaths.has(e.path)&&e.path!==t&&o.addDynamicDependency(t,e.path)}}(0,0,0,s)(t)}}else if("symbol"==typeof e){switch(e){case nt:return(t,e=async()=>{})=>async function(t,e,n){return await Vn(t,e,n)}(s,t,e);case ot:return(t,e=()=>{})=>function(t,e,n){return Vn(t,e,n)}(s,t,e);case st:return e=>kn(t,e,o,s);case it:return(e,n)=>Bn(t,e,n,o,s);case at:return()=>async function(t,e,n){const o=Reflect.get(t,x);"function"==typeof o&&await o.call(n)}(t,0,o);case rt:return()=>function(t,e,n){const o=Reflect.get(t,y);"function"==typeof o&&o.call(n)}(t,0,o);case lt:return e=>function(t,e,n,o){const s=Reflect.get(t,"$updatedCallback");if("function"==typeof s){const t=new Set,i={};for(const n of e){const e=n.absolutePathInfo.pathInfo;let s;if(s=n.absolutePathInfo.stateName===o.stateName?e.path:e.path+"@"+n.absolutePathInfo.stateName,t.add(s),e.wildcardCount>0){const t=n.listIndex.indexes??[],e=i[s];void 0===e?i[s]=[t]:e.push(t)}}return s.call(n,Array.from(t),i)}}(t,e,o,s)}return Reflect.get(t,e,o)}}class Jn{_stateElement;_stateName;_addressStack=Array(g).fill(void 0);_addressStackIndex=-1;_loopContext;_mutability;constructor(t,e,o){this._stateName=e;const s=yn(t,this._stateName);null===s&&n(`StateHandler: State element with name "${this._stateName}" not found.`),this._stateElement=s,this._mutability=o}get stateName(){return this._stateName}get stateElement(){return this._stateElement}get lastAddressStack(){let t;return this._addressStackIndex>=0&&(t=this._addressStack[this._addressStackIndex]),void 0===t&&n("Last address stack is undefined."),t}get addressStackLength(){return this._addressStackIndex+1}get loopContext(){return this._loopContext}pushAddress(t){this._addressStackIndex++,this._addressStackIndex>=g&&n("Exceeded maximum address stack depth of 128. Possible infinite loop."),this._addressStack[this._addressStackIndex]=t}popAddress(){if(this._addressStackIndex<0)return null;const t=this._addressStack[this._addressStackIndex];return void 0===t&&n(`Address stack at index ${this._addressStackIndex} is undefined.`),this._addressStack[this._addressStackIndex]=void 0,this._addressStackIndex--,t}setLoopContext(t){this._loopContext=t}clearLoopContext(){this._loopContext=void 0}get(t,e,n){return Hn(t,e,n,this)}set(t,e,o,s){return"readonly"===this._mutability&&n(`State "${this._stateName}" is readonly.`),function(t,e,n,o,s){if("string"==typeof e){const i=_n(e),a=Un(t,i,o,s);return Bn(t,Ht(i.pathInfo,a),n,o,s)}return Reflect.set(t,e,n,o)}(t,e,o,s,this)}has(t,e){return Reflect.has(t,e)}}const Xn=new WeakMap;const Kn=new WeakMap;function Yn(t,e){const n=Kn.get(t);return n?n.get(e)??null:null}const Gn=new WeakMap,Qn=new WeakMap,Zn=new WeakMap,to=new WeakMap;function eo(t,e){return{innerAbsPathInfo:Pt(t,P(e.propSegments.slice(1).join(p))),outerAbsPathInfo:Ft(e).absolutePathInfo}}function no(t,e){let o=Gn.get(t);if(void 0===o&&(o=new Map,Gn.set(t,o)),o.has(e))return o.get(e);let s=Qn.get(t);void 0===s&&(s=new Map,Qn.set(t,s));const i=Zn.get(t);if(void 0===i)return null;let a=null;for(const t of i)if(e.pathInfo.cumulativePathInfoSet.has(t.innerAbsPathInfo.pathInfo)){t.innerAbsPathInfo.pathInfo.segments.length===e.pathInfo.segments.length&&n("Duplicate mapping rule for web component."),a=t;break}if(null===a)return null;const r=to.get(a);void 0===r&&n("Binding not found for primary mapping rule on web component.");const l=e.pathInfo.segments.slice(a.innerAbsPathInfo.pathInfo.segments.length),d=P(a.outerAbsPathInfo.pathInfo.segments.concat(l).join(p)),c=yn(t.getRootNode(),r.stateName);null===c&&n(`State element with name "${r.stateName}" not found for web component.`);const u=Pt(c,d);o.set(e,u),s.set(u,e);return function(t,e){const n=f(t);null===n?h(t,[e]):n.push(e)}(t,{...r,propName:e.pathInfo.path,propSegments:e.pathInfo.segments,statePathName:u.pathInfo.path,statePathInfo:u.pathInfo}),u}function oo(t){return function(t){const e=Object.getPrototypeOf(t),n=Object.create(e),o=Object.getOwnPropertyDescriptors(t);for(const t in o){const e=o[t];!1===e.writable&&(e.writable=!0)}return Object.defineProperties(n,o),n}(t)}class so{_webComponent;_innerStateElement;constructor(t,e){this._webComponent=t,this._innerStateElement=Yn(t,e)??n("State element not found for web component.")}get(t,e,o){if("string"!=typeof e)return Reflect.get(t,e,o);{if("then"===e)return;if("$"===e[0])return;if(this._innerStateElement.getterPaths.has(e)&&e in t)return Reflect.get(t,e,o);const s=P(e),i=Pt(this._innerStateElement,s),a=no(this._webComponent,i);if(null!==a){const t=tt(this._webComponent);let e;return a.stateElement.createState("readonly",n=>{n[ot](t,()=>{e=n[a.pathInfo.path];let o=null;null!==t&&null!==t.listIndex&&a.pathInfo.wildcardCount>0&&(o=t.listIndex.at(a.pathInfo.wildcardCount-1));!function(t,e){Xn.set(t,e)}(Et(a,o),e)})}),e}if(e in t)return Reflect.get(t,e,o);n(`Property "${e}" not found in inner state: no mapping rule and no local state property.`)}}set(t,e,o,s){if("string"!=typeof e)return Reflect.set(t,e,o,s);{if(this._innerStateElement.setterPaths.has(e)&&e in t)return Reflect.set(t,e,o,s);const i=P(e),a=Pt(this._innerStateElement,i),r=no(this._webComponent,a);if(null!==r){const t=tt(this._webComponent);return r.stateElement.createState("writable",e=>{e[ot](t,()=>{e[r.pathInfo.path]=o})}),!0}if(e in t)return Reflect.set(t,e,o,s);n(`Property "${e}" not found in inner state: no mapping rule and no local state property.`)}}has(t,e){if("string"==typeof e){if("$"===e[0])return!1;if((this._innerStateElement.getterPaths.has(e)||this._innerStateElement.setterPaths.has(e))&&e in t)return!0;const n=P(e),o=Pt(this._innerStateElement,n);return null!==no(this._webComponent,o)||e in t}return Reflect.has(t,e)}}class io{_innerStateElement;constructor(t,e){this._innerStateElement=Yn(t,e)??n("State element not found for web component.")}get(t,e,n){if("string"==typeof e){const t=P(e),n=Et(Pt(this._innerStateElement,t),null);return o=n,Xn.get(o)}return Reflect.get(t,e,n);var o}set(t,e,n,o){if("string"==typeof e){const t=P(e),n=Pt(this._innerStateElement,t);return this._innerStateElement.createState("readonly",t=>{t.$postUpdate(n.pathInfo.path)}),!0}return Reflect.set(t,e,n,o)}}class ao{_innerStateElement;constructor(t,e){this._innerStateElement=Yn(t,e)??n("State element not found for web component.")}get(t,e,n){if("string"==typeof e){let t;return this._innerStateElement.createState("readonly",n=>{t=n[e]}),t}return Reflect.get(t,e,n)}set(t,e,n,o){return"string"==typeof e?(this._innerStateElement.createState("writable",t=>{t[e]=n}),!0):Reflect.set(t,e,n,o)}}const ro=t=>()=>t;function lo(t,o,s,i){if(function(t,e,n){let o=Kn.get(t);o||(o=new Map,Kn.set(t,o)),o.set(e,n)}(o,s,t),o.hasAttribute(e.bindAttributeName)){const e=(f(o)??[]).filter(t=>t.propSegments[0]===s);!function(t,e,o){if(0===o.length)return;const s=Yn(t,e);null===s&&n("State element not found for web component.");const i=new Map,a=new Map;for(const e of o){const n=eo(s,e);let o=Zn.get(t);void 0===o?Zn.set(t,new Set([n])):o.add(n);const r=n.innerAbsPathInfo,l=n.outerAbsPathInfo;to.set(n,e),i.set(r,l),a.set(l,r)}Gn.set(t,i),Qn.set(t,a)}(o,s,e);const i=function(t,e){const n=new io(t,e);return new Proxy({},n)}(o,s),a=function(t,e){const o=new so(t,e),s=Yn(t,e);null===s&&n("State element not found for web component."),null===s.boundComponentStateProp&&n("State element is not bound to any component state prop."),s.boundComponentStateProp in t||n(`State element is not bound to a valid component state prop: ${s.boundComponentStateProp}`);const i=t[s.boundComponentStateProp];return"object"==typeof i&&null!==i||n(`Invalid state object for component state prop: ${s.boundComponentStateProp}`),new Proxy(oo(i),o)}(o,s);t.setInitialState(a),Object.defineProperty(o,s,{get:ro(i),enumerable:!0,configurable:!0})}else{t.setInitialState(oo(i));const e=function(t,e){const n=new ao(t,e);return new Proxy({},n)}(o,s);Object.defineProperty(o,s,{get:ro(e),enumerable:!0,configurable:!0})}if(function(t,e){let n=jt.get(t);n||(n=new WeakMap,jt.set(t,n)),n.set(e,!0)}(o,t),I in o){const t=o[I];"function"==typeof t?t.call(o,s).catch(t=>{n(`Error in ${I}: ${t instanceof Error?t.message:String(t)}`)}):n(`${I} is not a function.`)}}function co(t){const e=new Set,n=new Set,o=function(t){let e={},n=t;for(;n&&n!==Object.prototype;)Object.assign(e,Object.getOwnPropertyDescriptors(n)),n=Object.getPrototypeOf(n);return e}(t);for(const[t,s]of Object.entries(o))"function"==typeof s.get&&e.add(t),"function"==typeof s.set&&n.add(t);return{getterPaths:e,setterPaths:n}}class uo extends HTMLElement{__state;_name="default";_initialized=!1;_initializePromise;_resolveInitialize=null;_loadingPromise;_resolveLoading=null;_setStatePromise=null;_resolveSetState=null;_listPaths=new Set;_elementPaths=new Set;_getterPaths=new Set;_setterPaths=new Set;_loopContextStack=function(){return new Sn}();_dynamicDependency=new Map;_staticDependency=new Map;_pathSet=new Set;_version=0;_rootNode=null;_boundComponent=null;_boundComponentStateProp=null;constructor(){super(),this._initializePromise=new Promise(t=>{this._resolveInitialize=t}),this._loadingPromise=new Promise(t=>{this._resolveLoading=t}),this._setStatePromise=new Promise(t=>{this._resolveSetState=t})}get _state(){return void 0===this.__state&&n(`${e.tagNames.state} _state is not initialized yet.`),this.__state}set _state(t){this.__state=t,this._listPaths.clear(),this._elementPaths.clear(),this._getterPaths.clear(),this._pathSet.clear();const e=co(t);for(const t of e.getterPaths)this._getterPaths.add(t);for(const t of e.setterPaths)this._setterPaths.add(t);this._resolveLoading?.()}get name(){return this._name}async _initialize(){try{if(this.hasAttribute("state")){const t=this.getAttribute("state");this._state=function(t){const e=document.getElementById(t);if(e&&"application/json"===e.type)try{return JSON.parse(e.textContent||"{}")}catch(t){n("Failed to parse JSON from script element:"+t)}return{}}(t)}else if(this.hasAttribute("src")){const t=this.getAttribute("src");t&&t.endsWith(".json")?this._state=await async function(t){try{const e=await fetch(t);return e.ok||n(`Failed to fetch JSON file: ${e.statusText}`),await e.json()}catch(t){return console.error("Failed to load JSON file:",t),{}}}(t):t&&t.endsWith(".js")?this._state=await async function(t){try{return(await import(t)).default||{}}catch(t){n(`Failed to load script file: ${t}`)}}(t):n(`Unsupported src file type: ${t}`)}else if(this.hasAttribute("json")){const t=this.getAttribute("json");this._state=JSON.parse(t)}else{const t=this.querySelector('script[type="module"]');if(t)this._state=await async function(t,e){let n=null;const o=`\n//# sourceURL=${e}\n`;if("function"==typeof URL.createObjectURL){const e=new Blob([t.text+o],{type:"application/javascript"}),s=URL.createObjectURL(e);try{n=await import(s)}finally{URL.revokeObjectURL(s)}}else{const e=btoa(String.fromCodePoint(...(new TextEncoder).encode(t.text+o)));n=await import(`data:application/javascript;base64,${e}`)}return n&&"object"==typeof n.default?n.default:{}}(t,`${this._name}`);else{const t=setTimeout(()=>{console.warn(`[@wcstack/state] Warning: No state source found for <${e.tagNames.state}> element with name="${this._name}".`)},6e4);this._state=await this._setStatePromise,clearTimeout(t)}}}catch(t){n(`Failed to initialize state: ${t}`)}await this._loadingPromise,this._name=this.getAttribute("name")||"default",In(this.rootNode,this._name,this)}async _initializeBindWebComponent(){if(this.hasAttribute("bind-component")){const t=this.parentNode,o=t instanceof ShadowRoot?t.host:t instanceof Element?t:null,s=o?ht(o):null;null!==o&&null!==s||n(`"bind-component" requires <${e.tagNames.state}> to be a direct child of a custom element.`),t instanceof ShadowRoot||this.hasAttribute("name")||n('"bind-component" in Light DOM requires a "name" attribute to avoid namespace conflicts with the parent scope.');const i=this.getAttribute("bind-component");await customElements.whenDefined(s.toLowerCase()),o.hasAttribute(e.bindAttributeName)&&await a(o),i in o||n(`Component does not have property "${i}" for state binding.`);const r=o[i];"object"==typeof r&&null!==r||n(`Component property "${i}" is not an object for state binding.`),this._boundComponent=o,this._boundComponentStateProp=i,lo(this,this._boundComponent,this._boundComponentStateProp,r)}}async _callStateConnectedCallback(){await this.createStateAsync("writable",async t=>{x in t&&await t[at]()})}_callStateDisconnectedCallback(){this.createState("writable",t=>{y in t&&t[rt]()})}async connectedCallback(){this._rootNode=this.getRootNode(),this._initialized||(await this._initializeBindWebComponent(),await this._initialize(),this._initialized=!0,this._resolveInitialize?.()),await this._callStateConnectedCallback()}disconnectedCallback(){null!==this._rootNode&&(this._callStateDisconnectedCallback(),In(this.rootNode,this._name,null),this._rootNode=null)}get initializePromise(){return this._initializePromise}get listPaths(){return this._listPaths}get elementPaths(){return this._elementPaths}get getterPaths(){return this._getterPaths}get setterPaths(){return this._setterPaths}get loopContextStack(){return this._loopContextStack}get dynamicDependency(){return this._dynamicDependency}get staticDependency(){return this._staticDependency}get version(){return this._version}get rootNode(){return null===this._rootNode&&n("State rootNode is not available."),this._rootNode}get boundComponentStateProp(){return this._boundComponentStateProp}_addDependency(t,e,n){const o=t.get(e);return void 0===o?(t.set(e,[n]),!0):!o.includes(n)&&(o.push(n),!0)}addDynamicDependency(t,e){return this._addDependency(this._dynamicDependency,t,e)}addStaticDependency(t,e){return this._addDependency(this._staticDependency,t,e)}setPathInfo(t,e){if("for"===e&&(this._listPaths.add(t),this._elementPaths.add(t+"."+m)),!this._pathSet.has(t)){const e=P(t);if(this._pathSet.add(t),null!==e.parentPath){let t=e;for(;null!==t.parentPath&&this.addStaticDependency(t.parentPath,t.path);)t=P(t.parentPath)}}}_createState(t,e,n){try{const o=function(t,e,n,o){const s=new Jn(t,n,o);return new Proxy(e,s)}(t,this._state,this._name,e);return n(o)}finally{}}async createStateAsync(t,e){return await this._createState(this.rootNode,t,e)}createState(t,e){this._createState(this.rootNode,t,e)}nextVersion(){return this._version++,this._version}bindProperty(t,e){Object.defineProperty(this._state,t,e)}setInitialState(t){this._initialized?this._state=t:this._resolveSetState?.(t)}}function fo(){customElements.get(e.tagNames.state)||customElements.define(e.tagNames.state,uo)}function ho(e){var n;e&&((n=e).tagNames&&Object.assign(t.tagNames,n.tagNames),"string"==typeof n.bindAttributeName&&(t.bindAttributeName=n.bindAttributeName),"string"==typeof n.commentTextPrefix&&(t.commentTextPrefix=n.commentTextPrefix),"string"==typeof n.commentForPrefix&&(t.commentForPrefix=n.commentForPrefix),"string"==typeof n.commentIfPrefix&&(t.commentIfPrefix=n.commentIfPrefix),"string"==typeof n.commentElseIfPrefix&&(t.commentElseIfPrefix=n.commentElseIfPrefix),"string"==typeof n.commentElsePrefix&&(t.commentElsePrefix=n.commentElsePrefix),"string"==typeof n.locale&&(t.locale=n.locale),"boolean"==typeof n.debug&&(t.debug=n.debug),"boolean"==typeof n.enableMustache&&(t.enableMustache=n.enableMustache)),fo()}export{ho as bootstrapState};
|
|
1
|
+
const t={bindAttributeName:"data-wcs",commentTextPrefix:"wcs-text",commentForPrefix:"wcs-for",commentIfPrefix:"wcs-if",commentElseIfPrefix:"wcs-elseif",commentElsePrefix:"wcs-else",tagNames:{state:"wcs-state"},locale:"en",debug:!1,enableMustache:!0},e=t;function n(t){throw new Error(`[@wcstack/state] ${t}`)}const o=new WeakMap;let s=0;function i(t){let e,n=o.get(t)||null;if(null!==n)return n;const i=new Promise(t=>{e=t});return n={id:++s,promise:i,resolve:e},o.set(t,n),n}async function a(t){const e=i(t);await e.promise}function r(t){i(t).resolve()}function l(t){const e=t.node,n=t.replaceNode;e!==n&&null!==e.parentNode&&e.parentNode.replaceChild(n,e)}function d(t,e){let n=t;if(0===e.length)return n;for(let t=0;t<e.length&&(n=n?.childNodes[e[t]]??null,null!==n);t++);return n}function c(t,e){const n=[];for(const o of e)if("text"!==o.bindingType)n.push({...o,node:t,replaceNode:t});else{const e=document.createTextNode("");n.push({...o,node:t,replaceNode:e})}return n}const u=new WeakMap;function f(t){return u.get(t)||null}function p(t,e){u.set(t,e)}const h=".",m="*",g=128,w={};for(let t=0;t<128;t++)w[`$${t+1}`]=t;const b=Object.freeze(w),x="$connectedCallback",y="$disconnectedCallback",I="$stateReadyCallback",S=new Map;let N=0;function P(t){let e=S.get(t);return void 0!==e||(e=Object.freeze(new _(t)),S.set(t,e)),e}class _{id=++N;path;segments;lastSegment;cumulativePaths;cumulativePathSet;cumulativePathInfos;cumulativePathInfoSet;parentPath;wildcardPaths;wildcardPathSet;indexByWildcardPath;wildcardPathInfos;wildcardPathInfoSet;wildcardParentPaths;wildcardParentPathSet;wildcardParentPathInfos;wildcardParentPathInfoSet;wildcardPositions;lastWildcardPath;lastWildcardInfo;wildcardCount;parentPathInfo;constructor(t){const e=e=>t===e?this:P(e),n=t.split("."),o=[],s=[],i=[],a={},r=[],l=[],d=[],c=[];let u="",f="",p=0;for(let t=0;t<n.length;t++)u+=n[t],n[t]===m&&(i.push(u),a[u]=p,r.push(e(u)),l.push(f),d.push(e(f)),c.push(t),p++),o.push(u),s.push(e(u)),f=u,u+=".";const h=i.length>0?i[i.length-1]:null,g=o.length>1?o[o.length-2]:null;this.path=t,this.segments=n,this.lastSegment=n[n.length-1],this.cumulativePaths=o,this.cumulativePathSet=new Set(o),this.cumulativePathInfos=s,this.cumulativePathInfoSet=new Set(s),this.wildcardPaths=i,this.wildcardPathSet=new Set(i),this.indexByWildcardPath=a,this.wildcardPathInfos=r,this.wildcardPathInfoSet=new Set(r),this.wildcardParentPaths=l,this.wildcardParentPathSet=new Set(l),this.wildcardParentPathInfos=d,this.wildcardParentPathInfoSet=new Set(d),this.wildcardPositions=c,this.lastWildcardPath=h,this.lastWildcardInfo=h?e(h):null,this.parentPath=g,this.parentPathInfo=g?e(g):null,this.wildcardCount=p}}const v=new Set(["if","elseif","else","for"]);function A(t){n(`filter ${t} requires at least one option`)}function $(t){n(`filter ${t} requires a number as option`)}function E(t){n(`filter ${t} requires a number value`)}function C(t){n(`filter ${t} requires a date value`)}function k(t){return!(!t||isNaN(Number(t)))}const M={eq:t=>{const e=t?.[0]??A("eq");return t=>"number"==typeof t?(k(e)||$("eq"),t===Number(e)):t===e},ne:t=>{const e=t?.[0]??A("ne");return t=>"number"==typeof t?(k(e)||$("ne"),t!==Number(e)):t!==e},not:t=>t=>("boolean"!=typeof t&&n(`filter ${"not"} requires a boolean value`),!t),lt:t=>{const e=t?.[0]??A("lt");return k(e)||$("lt"),t=>("number"!=typeof t&&E("lt"),t<Number(e))},le:t=>{const e=t?.[0]??A("le");return k(e)||$("le"),t=>("number"!=typeof t&&E("le"),t<=Number(e))},gt:t=>{const e=t?.[0]??A("gt");return k(e)||$("gt"),t=>("number"!=typeof t&&E("gt"),t>Number(e))},ge:t=>{const e=t?.[0]??A("ge");return k(e)||$("ge"),t=>("number"!=typeof t&&E("ge"),t>=Number(e))},inc:t=>{const e=t?.[0]??A("inc");return k(e)||$("inc"),t=>("number"!=typeof t&&E("inc"),t+Number(e))},dec:t=>{const e=t?.[0]??A("dec");return k(e)||$("dec"),t=>("number"!=typeof t&&E("dec"),t-Number(e))},mul:t=>{const e=t?.[0]??A("mul");return k(e)||$("mul"),t=>("number"!=typeof t&&E("mul"),t*Number(e))},div:t=>{const e=t?.[0]??A("div");return k(e)||$("div"),t=>("number"!=typeof t&&E("div"),t/Number(e))},mod:t=>{const e=t?.[0]??A("mod");return k(e)||$("mod"),t=>("number"!=typeof t&&E("mod"),t%Number(e))},fix:t=>{const e=t?.[0]??"0";return k(e)||$("fix"),t=>("number"!=typeof t&&E("fix"),t.toFixed(Number(e)))},locale:t=>{const n=t?.[0]??e.locale;return t=>("number"!=typeof t&&E("locale"),t.toLocaleString(n))},uc:t=>t=>String(t).toUpperCase(),lc:t=>t=>String(t).toLowerCase(),cap:t=>t=>{const e=String(t);return 0===e.length?e:1===e.length?e.toUpperCase():e.charAt(0).toUpperCase()+e.slice(1)},trim:t=>t=>String(t).trim(),slice:t=>{const e=[],n=t?.[0]??A("slice");k(n)||$("slice"),e.push(Number(n));const o=t?.[1];return void 0!==o&&(k(o)||$("slice"),e.push(Number(o))),t=>String(t).slice(...e)},substr:t=>{const e=t?.[0]??A("substr");k(e)||$("substr");const n=t?.[1]??A("substr");return k(n)||$("substr"),t=>String(t).substr(Number(e),Number(n))},pad:t=>{const e=t?.[0]??A("pad");k(e)||$("pad");const n=t?.[1]??"0";return t=>String(t).padStart(Number(e),n)},rep:t=>{const e=t?.[0]??A("rep");return k(e)||$("rep"),t=>String(t).repeat(Number(e))},rev:t=>t=>String(t).split("").reverse().join(""),int:t=>t=>parseInt(String(t),10),float:t=>t=>parseFloat(String(t)),round:t=>{const e=t?.[0]??"0";return k(e)||$("round"),t=>{"number"!=typeof t&&E("round");const n=Math.pow(10,Number(e));return Math.round(t*n)/n}},floor:t=>{const e=t?.[0]??"0";return k(e)||$("floor"),t=>{"number"!=typeof t&&E("floor");const n=Math.pow(10,Number(e));return Math.floor(t*n)/n}},ceil:t=>{const e=t?.[0]??"0";return k(e)||$("ceil"),t=>{"number"!=typeof t&&E("ceil");const n=Math.pow(10,Number(e));return Math.ceil(t*n)/n}},percent:t=>{const e=t?.[0]??"0";return k(e)||$("percent"),t=>("number"!=typeof t&&E("percent"),`${(100*t).toFixed(Number(e))}%`)},date:t=>{const n=t?.[0]??e.locale;return t=>(t instanceof Date||C("date"),t.toLocaleDateString(n))},time:t=>{const n=t?.[0]??e.locale;return t=>(t instanceof Date||C("time"),t.toLocaleTimeString(n))},datetime:t=>{const n=t?.[0]??e.locale;return t=>(t instanceof Date||C("datetime"),t.toLocaleString(n))},ymd:t=>{const e=t?.[0]??"-";return t=>{t instanceof Date||C("ymd");const n=t.getFullYear().toString(),o=(t.getMonth()+1).toString().padStart(2,"0"),s=t.getDate().toString().padStart(2,"0");return`${n}${e}${o}${e}${s}`}},falsy:t=>t=>!1===t||null==t||0===t||""===t||Number.isNaN(t),truthy:t=>t=>!1!==t&&null!=t&&0!==t&&""!==t&&!Number.isNaN(t),defaults:t=>{const e=t?.[0]??A("defaults");return t=>!1===t||null==t||0===t||""===t||Number.isNaN(t)?e:t},boolean:t=>t=>Boolean(t),number:t=>t=>Number(t),string:t=>t=>String(t),null:t=>t=>""===t?null:t},T=M,L={input:M,output:T},W=(t,e)=>o=>{const s=o[t];return s||n(`filter not found: ${t}`),s(e)};const R=new Map;function O(t,e){const o=L[e];return t.map(t=>{const s=t.indexOf("("),i=t.lastIndexOf(")");if(-1!==s&&-1===i&&n(`Invalid filter format: missing closing parenthesis in "${t}"`),-1!==i&&-1===s&&n(`Invalid filter format: missing opening parenthesis in "${t}"`),-1===s){const n=t.trim(),s=`${n}():${e}`;let i=R.get(s);return void 0===i&&(i=W(n,[])(o),R.set(s,i)),{filterName:n,args:[],filterFn:i}}{const n=t.substring(s+1,i),a=t.substring(0,s).trim(),r=function(t){const e=[];let n="",o=null,s=!1;for(let i=0;i<t.length;i++){const a=t[i];o?a===o?o=null:n+=a:'"'===a||"'"===a?(o=a,s=!0):","===a?(e.push(n.trim()),n="",s=!1):n+=a}const i=n.trim();return(i||s)&&e.push(i),e}(n),l=`${a}(${r.join(",")}):${e}`;let d=R.get(l);return void 0===d&&(d=W(a,r)(o),R.set(l,d)),{filterName:a,args:r,filterFn:d}}})}const F=t=>t.trim(),D=new Map;const j=new Map;function B(t){const e=t.indexOf("|");let n="",o=[],s="",i=[];-1!==e?(n=t.slice(0,e).trim(),s=t.slice(e+1).trim(),j.has(s)?i=j.get(s):(o=s.split("|").map(F),i=O(o,"output"),j.set(s,i))):n=t.trim();const[a,r="default"]=n.split("@").map(F);return{stateName:r,statePathName:a,statePathInfo:P(a),outFilters:i}}function z(t){const[...e]=t.split(";").map(F).filter(t=>t.length>0),o=e.map(t=>{const e=t.indexOf(":");-1===e&&n(`Invalid bindText: "${t}". Missing ':' separator between propPart and statePart.`);const o=t.slice(0,e).trim(),s=t.slice(e+1).trim();if("else"===o){return{propName:"else",propSegments:["else"],propModifiers:[],statePathName:"#else",statePathInfo:P("#else"),stateName:"",inFilters:[],outFilters:[],bindingType:"else"}}if("if"===o||"elseif"===o||"for"===o||"radio"===o||"checkbox"===o){return{propName:o,propSegments:[o],propModifiers:[],inFilters:[],...B(s),bindingType:o}}{const t=B(s),e=function(t){const e=t.indexOf("|");let n="",o=[],s="",i=[];-1!==e?(n=t.slice(0,e).trim(),s=t.slice(e+1).trim(),D.has(s)?i=D.get(s):(o=s.split("|").map(F),i=O(o,"input"),D.set(s,i))):n=t.trim();const[a,r]=n.split("#").map(F),l=a.split(".").map(F);return{propName:a,propSegments:l,propModifiers:r?r.split(",").map(F):[],inFilters:i}}(o);return e.propSegments[0].startsWith("on")?{...e,...t,bindingType:"event"}:{...e,...t,bindingType:"prop"}}});if(o.length>1){o.some(t=>v.has(t.bindingType))&&n(`Invalid bindText: "${t}". 'if', 'elseif', 'else', and 'for' bindings must be single binding.`)}return o}const q=new WeakMap,U=new Set([e.commentTextPrefix,e.commentForPrefix,e.commentIfPrefix,e.commentElseIfPrefix,e.commentElsePrefix]),V=new RegExp("^\\s*@@\\s*(.*?)\\s*:\\s*(.+?)\\s*$");function H(t){const n=q.get(t);if("string"==typeof n)return n;if(t.nodeType!==Node.COMMENT_NODE)return null;const o=t.data.trim(),s=V.exec(o);if(null===s)return null;const i=s[1]||e.commentTextPrefix;return U.has(i)?(q.set(t,s[2]),s[2]):null}const J=new Map;function X(t,e,o){if(null===o)J.delete(t);else{J.set(t,o);const s=o.parseBindTextResult,i=Nn(e,s.stateName);null===i&&n(`State element with name "${s.stateName}" not found for fragment info.`),i.setPathInfo(s.statePathName,s.bindingType);for(const t of o.nodeInfos)for(const o of t.parseBindTextResults){const t=Nn(e,o.stateName);null===t&&n(`State element with name "${o.stateName}" not found for fragment info node.`),t.setPathInfo(o.statePathName,o.bindingType)}}}function K(t){return J.get(t)||null}function Y(t){if(t.nodeType===Node.ELEMENT_NODE){return z(t.getAttribute(e.bindAttributeName)||"")}if(t.nodeType===Node.COMMENT_NODE){const e=H(t);null===e&&n("Comment node binding text not found.");const o=K(e);let s=o?.parseBindTextResult??null,i=null;return null===s?(s={propName:"textContent",propSegments:["textContent"],propModifiers:[],inFilters:[],...B(e),bindingType:"text"},i=null):i=e,[{...s,uuid:i}]}return[]}function G(t){const n=[],o=document.createTreeWalker(t,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_COMMENT,{acceptNode(t){if(t.nodeType===Node.ELEMENT_NODE){return t.hasAttribute(e.bindAttributeName)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}return null!==H(t)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});for(;o.nextNode();)n.push(o.currentNode);return n}const Q=new WeakSet;const Z=new WeakMap;function tt(t){let e=t;for(;e;){const t=Z.get(e);if(t)return t;e=e.parentNode}return null}function et(t,e){null!==e?Z.set(t,e):Z.delete(t)}const nt=Symbol("$$setLoopContextAsync"),ot=Symbol("$$setLoopContext"),st=Symbol("$$getByAddress"),it=Symbol("$$setByAddress"),at=Symbol("$$connectedCallback"),rt=Symbol("$$disconnectedCallback"),lt=Symbol("$$updatedCallback"),dt=new Map,ct=new Map;function ut(t){if(!t.propName.startsWith("on"))return!1;const e=function(t){const e=t.propModifiers.filter(t=>"prevent"===t||"stop"===t).sort().join(",");return`${t.stateName}::${t.statePathName}::${e}`}(t);let o=dt.get(e);var s,i,a;void 0===o&&(s=t.stateName,i=t.statePathName,a=t.propModifiers,o=t=>{a.includes("prevent")&&t.preventDefault(),a.includes("stop")&&t.stopPropagation();const e=t.target,o=Nn(e.getRootNode(),s);null===o&&n(`State element with name "${s}" not found for event handler.`);const r=tt(e);o.createStateAsync("writable",async e=>{e[ot](r,()=>{const o=e[i];return"function"!=typeof o&&n(`Handler "${i}" is not a function on state "${s}".`),Reflect.apply(o,e,[t,...r?.listIndex.indexes??[]])})})},dt.set(e,o));const r=t.propName.slice(2);t.node.addEventListener(r,o);let l=ct.get(e);return void 0===l?(l=new Set([t]),ct.set(e,l)):l.add(t),!0}const ft=new WeakMap;function pt(t){const e=ft.get(t);if(void 0!==e)return e;let n=null;try{if(t.nodeType!==Node.ELEMENT_NODE)return n;const e=t,o=e.tagName.toLowerCase();if(o.includes("-"))return n=o;if(e.hasAttribute("is")){const t=e.getAttribute("is");if(t.includes("-"))return n=t}return n}finally{ft.set(t,n)}}const ht=new Set(["radio","checkbox"]),mt=new Set(["value","valueAsNumber","valueAsDate"]);const gt=new Map,wt=new Map,bt=t=>t.detail;function xt(t){const e=pt(t.node);if(null!==e){if(void 0===customElements.get(e))return void customElements.whenDefined(e).then(()=>{xt(t)})}if(function(t,e){if(t.nodeType!==Node.ELEMENT_NODE)return!1;const o=t,s=o.tagName.toLowerCase();if("input"===s){const t=(o.getAttribute("type")||"text").toLowerCase();if("button"===t)return!1;if(ht.has(t)&&"checked"===e)return!0;if(mt.has(e))return!0}if("select"===s&&"value"===e)return!0;if("textarea"===s&&"value"===e)return!0;const i=pt(o);if(null!==i){const t=customElements.get(i);void 0===t&&n(`Custom element <${i}> is not defined. Cannot determine if property "${e}" is suitable for two-way binding.`);const o=t.wcBindable;if("wc-bindable"===o?.protocol&&1===o?.version&&o.properties.some(t=>t.name===e))return!0}return!1}(t.node,t.propName)&&-1===t.propModifiers.indexOf("ro")){const e=function(t){let e="select"===t.node.tagName.toLowerCase()?"change":"input";const o=pt(t.node);if(null!==o){const s=customElements.get(o);void 0===s&&n(`Custom element <${o}> is not defined. Cannot determine event name for two-way binding.`);const i=s.wcBindable;if("wc-bindable"===i?.protocol&&1===i?.version){const n=i.properties.find(e=>e.name===t.propName);n&&(e=n.event)}}for(const n of t.propModifiers)n.startsWith("on")&&(e=n.slice(2));return e}(t),o=function(t){const e=pt(t.node);if(null!==e){const n=customElements.get(e);if(n){const e=n.wcBindable;if("wc-bindable"===e?.protocol&&1===e?.version){const n=e.properties.find(e=>e.name===t.propName);if(n)return n.getter??bt}}}return null}(t),s=function(t,e,n){const o=t.inFilters.map(t=>t.filterName+"("+t.args.join(",")+")").join("|");return`${t.stateName}::${t.propName}::${t.statePathName}::${e}::${o}::${n?"g":"n"}`}(t,e,null!==o);let i=gt.get(s);void 0===i&&(i=((t,e,o,s,i)=>a=>{const r=a.target;if(null===r)return void console.warn("[@wcstack/state] event.target is null.");let l;if(null!==i)l=i(a);else{if(!(e in r))return void console.warn(`[@wcstack/state] Property "${e}" does not exist on target element.`);l=r[e]}let d=l;for(const t of s)d=t.filterFn(d);const c=Nn(r.getRootNode(),t);null===c&&n(`State element with name "${t}" not found for two-way binding.`);const u=tt(r);c.createState("writable",t=>{t[ot](u,()=>{t[o]=d})})})(t.stateName,t.propName,t.statePathName,t.inFilters,o),gt.set(s,i)),t.node.addEventListener(e,i);let a=wt.get(s);void 0===a?(a=new Set([t]),wt.set(s,a)):a.add(t)}}const yt=new WeakMap;function It(t){return yt.get(t)??[]}function St(t,e){yt.set(t,e)}const Nt=new WeakMap;function Pt(t,e){if(Nt.has(t)){const n=Nt.get(t);if(n.has(e))return n.get(e)}else Nt.set(t,new WeakMap);const n=Object.freeze(new _t(t,e));return Nt.get(t).set(e,n),n}class _t{pathInfo;stateName;stateElement;parentAbsolutePathInfo;constructor(t,e){this.pathInfo=e,this.stateName=t.name,this.stateElement=t,null===e.parentPathInfo?this.parentAbsolutePathInfo=null:this.parentAbsolutePathInfo=Pt(t,e.parentPathInfo)}}const vt=new WeakMap,At=new WeakMap;class $t{absolutePathInfo;listIndex;_parentAbsoluteAddress;constructor(t,e){this.absolutePathInfo=t,this.listIndex=e}get parentAbsoluteAddress(){if(void 0!==this._parentAbsoluteAddress)return this._parentAbsoluteAddress;const t=this.absolutePathInfo.parentAbsolutePathInfo;if(null===t)return null;let e=null;return e=this.absolutePathInfo.pathInfo.segments[this.absolutePathInfo.pathInfo.segments.length-1]===m?this.listIndex?.parentListIndex??null:this.listIndex,this._parentAbsoluteAddress=Et(t,e)}}function Et(t,e){if(null===e){let e=At.get(t);return void 0!==e||(e=new $t(t,null),At.set(t,e)),e}{let n=vt.get(e);void 0===n&&(n=new WeakMap,vt.set(e,n));let o=n.get(t);return void 0!==o||(o=new $t(t,e),n.set(t,o)),o}}const Ct=new WeakMap;function kt(t,e){null===e?Ct.delete(t):Ct.set(t,e)}function Mt(t){return Ct.get(t)||null}const Tt=new Map;function Lt(t,e){let n,o;if(0===t.wildcardCount||0===e.wildcardCount)return 0;if(1===t.wildcardCount&&e.wildcardCount>0&&e.wildcardPathSet.has(t.path))return 1;t.id<e.id?(n=t,o=e):(n=e,o=t);const s=`${n.path}\t${o.path}`;let i=Tt.get(s);if(void 0!==i)return i;return i=n.wildcardPathSet.intersection(o.wildcardPathSet).size,Tt.set(s,i),i}const Wt=new WeakMap;function Rt(t){const e=tt(t.node);if(null===e)return null;let n=Wt.get(e);if(void 0===n)n=new WeakMap,Wt.set(e,n);else{const e=n.get(t);if(void 0!==e)return e}let o=null;try{const n=Lt(e.pathInfo,t.statePathInfo);return n>0&&(o=e.listIndex.at(n-1)),o}finally{n.set(t,o)}}const Ot=new WeakMap;function Ft(t){let e=null;if(e=Ot.get(t)||null,null!==e)return e;let o=t.replaceNode.getRootNode();if(!1===t.replaceNode.isConnected){const e=Mt(o);null===e?n(`Cannot get absolute state address for disconnected binding: ${t.bindingType} ${t.statePathName} on ${t.node.nodeName}`):o=e}const s=Rt(t),i=Nn(o,t.stateName);null===i&&n(`State element with name "${t.stateName}" not found for binding.`);return e=Et(Pt(i,t.statePathInfo),s),Ot.set(t,e),e}function Dt(t){Ot.delete(t)}const jt=new WeakMap;function Bt(t,e){const n=jt.get(t);return!!n&&!0===n.get(e)}function zt(t,e){let n=t;for(const t of e)n=t.filterFn(n);return n}const qt=Object.freeze([]);const Ut=new WeakMap,Vt=new WeakMap;class Ht{pathInfo;listIndex;_parentAddress;constructor(t,e){this.pathInfo=t,this.listIndex=e}get parentAddress(){if(void 0!==this._parentAddress)return this._parentAddress;const t=this.pathInfo.parentPathInfo;if(null===t)return null;let e=null;return e=this.pathInfo.segments[this.pathInfo.segments.length-1]===m?this.listIndex?.parentListIndex??null:this.listIndex,this._parentAddress=Jt(t,e)}}function Jt(t,e){if(null===e){let e=Vt.get(t);return void 0!==e||(e=new Ht(t,null),Vt.set(t,e)),e}{let n=Ut.get(e);void 0===n&&(n=new WeakMap,Ut.set(e,n));let o=n.get(t);return void 0!==o||(o=new Ht(t,e),n.set(t,o)),o}}const Xt=new WeakMap;function Kt(t){return Xt.get(t)??[]}Set.prototype.difference||(Set.prototype.difference=function(t){const e=new Set(this);for(const n of t)e.delete(n);return e}),Set.prototype.intersection||(Set.prototype.intersection=function(t){const e=new Set;for(const n of t)this.has(n)&&e.add(n);return e});let Yt=0;function Gt(){return`u${(Yt++).toString(36)}`}let Qt=0;class Zt{uuid=Gt();parentListIndex;position;length;_index;_version;_indexes;_listIndexes;constructor(t,e){this.parentListIndex=t,this.position=t?t.position+1:0,this.length=this.position+1,this._index=e,this._version=Qt}get index(){return this._index}set index(t){this._index=t,this._version=++Qt,this.indexes[this.position]=t}get version(){return this._version}get dirty(){return null!==this.parentListIndex&&(this.parentListIndex.dirty||this.parentListIndex.version>this._version)}get indexes(){return null===this.parentListIndex?void 0===this._indexes&&(this._indexes=[this._index]):(void 0===this._indexes||this.dirty)&&(this._indexes=[...this.parentListIndex.indexes,this._index],this._version=Qt),this._indexes}get listIndexes(){return null===this.parentListIndex?void 0===this._listIndexes&&(this._listIndexes=[new WeakRef(this)]):void 0===this._listIndexes&&(this._listIndexes=[...this.parentListIndex.listIndexes,new WeakRef(this)]),this._listIndexes}get varName(){return`$${this.position+1}`}at(t){return t>=0?this.listIndexes[t]?.deref()||null:this.listIndexes[this.listIndexes.length+t]?.deref()||null}}function te(t,e){return new Zt(t,e)}const ee=new WeakMap;function ne(t){return ee.get(t)||null}const oe=new WeakMap,se=Object.freeze([]),ie=new Set;function ae(t,e,n){const o=Array.isArray(e)&&e.length>0?e:se,s=Array.isArray(n)&&n.length>0?n:se,i=function(t,e){const n=Array.isArray(t)&&t.length>0?t:se,o=Array.isArray(e)&&e.length>0?e:se;let s=oe.get(n);return s&&s.get(o)||null}(o,s);if(i)return i;const a=ne(o)||[];let r;try{if(0===s.length)return r={oldIndexes:a,newIndexes:[],changeIndexSet:ie,deleteIndexSet:new Set(a),addIndexSet:ie};let e=ne(s);if(0===o.length){if(null===e){e=[];for(let n=0;n<s.length;n++){const o=te(t,n);e.push(o)}}return r={oldIndexes:a,newIndexes:e,changeIndexSet:ie,deleteIndexSet:ie,addIndexSet:new Set(e)}}if(function(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}(o,s))return r={oldIndexes:a,newIndexes:a,changeIndexSet:ie,deleteIndexSet:ie,addIndexSet:ie};const n=new Map;for(let t=0;t<o.length;t++){const e=o[t];let s=n.get(e);s||(s=[],n.set(e,s)),s.push(t)}if(null!==e)return function(t,e,n,o,s){const i=new Set(o),a=new Set(n),r=new Set,l=i.difference(a),d=a.difference(i);for(let t=0;t<e.length;t++){const o=e[t],i=s.get(o),a=i&&i.length>0?i.shift():void 0;if(void 0!==a){const e=n[a];e.index!==t&&r.add(e)}}return{oldIndexes:n,newIndexes:o,changeIndexSet:r,deleteIndexSet:d,addIndexSet:l}}(0,s,a,e,n);e=[];const i=new Set,l=new Set;for(let o=0;o<s.length;o++){const r=s[o],d=n.get(r),c=d&&d.length>0?d.shift():void 0;if(void 0===c){const n=te(t,o);e.push(n),l.add(n)}else{const t=a[c];t.index!==o&&(t.index=o,i.add(t)),e.push(t)}}return r={oldIndexes:a,newIndexes:e,changeIndexSet:i,deleteIndexSet:new Set(a).difference(new Set(e)),addIndexSet:l}}finally{void 0!==r&&(!function(t,e,n){let o=oe.get(t);o||(o=new WeakMap,oe.set(t,o)),o.set(e,n)}(o,s,r),l=s,null!==(d=r.newIndexes)?ee.set(l,d):ee.delete(l))}var l,d}const re=new WeakMap;function le(t){let e=null;return e=re.get(t)||null,null===e&&(e=new Set,re.set(t,e)),e}function de(t,e){le(t).add(e)}function ce(t,e){le(t).delete(e)}const ue=new WeakMap;function fe(t){ue.delete(t)}const pe=new WeakMap;function he(t){return pe.get(t)??[]}const me=new WeakMap;function ge(t){return me.get(t)??[]}function we(t,e,n){!function(t,e){const n=ge(t);for(const t of n)et(t,e)}(t,e);const o=he(t);for(const t of o){de(Ft(t),t),Ue(t,n)}}function be(t){if(!t.mounted)return;const e=he(t);for(const t of e){ce(Ft(t),t),Dt(t),fe(t)}!function(t){const e=ge(t);for(const t of e)et(t,null)}(t)}const xe=new WeakMap,ye=Object.freeze(new Set);function Ie(t){const e=xe.get(t);return void 0!==e?e:ye}const Se=new Set(["if","elseif","else","for"]);class Ne{_content;_childNodeArray=[];_firstNode=null;_lastNode=null;_mounted=!1;constructor(t){this._content=t,this._childNodeArray=Array.from(this._content.childNodes),this._firstNode=this._childNodeArray.length>0?this._childNodeArray[0]:null,this._lastNode=this._childNodeArray.length>0?this._childNodeArray[this._childNodeArray.length-1]:null}get firstNode(){return this._firstNode}get lastNode(){return this._lastNode}get mounted(){return this._mounted}appendTo(t){for(const e of this._childNodeArray)t.appendChild(e);this._mounted=!0}mountAfter(t){const e=t.parentNode,n=t.nextSibling;if(e)for(const t of this._childNodeArray)e.insertBefore(t,n);this._mounted=!0}unmount(){for(const t of this._childNodeArray)null!==t.parentNode&&t.parentNode.removeChild(t);const t=he(this);for(const e of t){if(Se.has(e.bindingType)){const t=Ie(e.node);for(const e of t)e.unmount()}fe(e),Dt(e)}this._mounted=!1}}function Pe(t){void 0!==t.uuid&&null!==t.uuid||n("BindingInfo.uuid is null.");const e=K(t.uuid);e||n(`Fragment with UUID "${t.uuid}" not found.`);const o=document.importNode(e.fragment,!0),s=function(t,e){const[o,s]=function(t,e){const o=[],s=[];for(const i of e){const e=d(t,i.nodePath);if(null===e&&n(`Node not found by path [${i.nodePath.join(", ")}] in fragment.`),!Q.has(e)){Q.add(e);const t=c(e,i.parseBindTextResults);p(e,t),r(e),s.push(...t),o.push(e)}}return[o,s]}(t,e);return tn(s),{nodes:o,bindingInfos:s}}(o,e.nodeInfos),i=new Ne(o);!function(t,e){pe.set(t,e)}(i,s.bindingInfos);const a=[];for(const t of s.bindingInfos)t.statePathName in b&&a.push(t);return function(t,e){Xt.set(t,e)}(i,a),function(t,e){me.set(t,e)}(i,s.nodes),function(t,e){const n=xe.get(t);n?n.add(e):xe.set(t,new Set([e]))}(t.node,i),i}const _e=new WeakMap,ve=new WeakMap,Ae=new WeakMap,$e=new WeakMap;function Ee(t){return Ae.get(t.node)||[]}function Ce(t,e){const n=Ae.get(t.node);void 0===n?Ae.set(t.node,[e]):n.push(e)}function ke(t,e){let n=ve.get(t);if(void 0===n)return null;const o=n.get(e);return void 0===o?null:o}function Me(t,e,n){let o=ve.get(t);if(void 0===o){if(null===n)return;o=new WeakMap,ve.set(t,o)}null===n?o.delete(e):o.set(e,n)}const Te=new WeakMap;function Le(t){return`${t.bindingType} ${t.statePathName} ${t.outFilters.map(t=>t.filterName).join("|")} ${t.node.isConnected?"(connected)":"(disconnected)"}`}function We(t,n,o){const s=t.node.isConnected,i=Boolean(o);let a;const r=Ie(t.node);a=0===r.size?Pe(t):r.values().next().value;try{if(i||(e.debug&&console.log(`unmount if content : ${Le(t)}`),be(a),a.unmount()),i){e.debug&&console.log(`mount if content : ${Le(t)}`),a.mountAfter(t.node);we(a,tt(t.node),n)}}finally{Te.set(t.node,s)}}function Re(t,n,o){const s=t.node,i=t.propSegments;if(1===i.length){const t=i[0];if(s[t]!==o)try{s[t]=o}catch(n){e.debug&&console.warn(`Failed to set property '${t}' on element.`,{element:s,newValue:o,error:n})}return}let a=s[i[0]];for(let t=1;t<i.length-1;t++){const e=i[t];if(null==a)return;a=a[e]}const r=a[i[i.length-1]];if(r!==o){if(Object.isFrozen(a))return void(e.debug&&console.warn("Attempting to set property on frozen object.",{element:s,propSegments:i,oldValue:r,newValue:o}));try{a[i[i.length-1]]=o}catch(t){e.debug&&console.warn("Failed to set property on sub-object.",{element:s,propSegments:i,oldValue:r,newValue:o,error:t})}}}function Oe(t,e,o){const s=t.node,i=t.propSegments;i.length<=1&&n(`Invalid propSegments for web component binding: ${i.join(".")}`);const[a,...r]=i,l=s[a];void 0===l&&n(`Property "${a}" not found on web component.`),l[r.join(".")]=o}function Fe(t,e){const o=function(t){let e=null;if(e=ue.get(t)||null,null!==e)return e;if(t.statePathInfo.wildcardCount>0){const o=Rt(t);null===o&&n(`Cannot resolve state address for binding with wildcard statePathName "${t.statePathName}" because list index is null.`),e=Jt(t.statePathInfo,o)}else e=Jt(t.statePathInfo,null);return ue.set(t,e),e}(e);if(o.pathInfo.path in b){const t=tt(e.node);return null===t&&n(`ListIndex not found for binding: ${e.statePathName}`),function(t,e){null===t.listIndex&&n("ListIndex not found for loopContext:");const o=b[e];void 0===o&&n(`Invalid index name: ${e}`);const s=t.listIndex.at(o);return null===s&&n(`Index not found at position ${o} for loopContext:`),s.index}(t,o.pathInfo.path)}return t[st](o)}const De={class:function(t,e,o){const s=t.node,i=t.propSegments[1];"boolean"!=typeof o&&n("Invalid value for class application: expected boolean, got "+typeof o),s.classList.toggle(i,o)},attr:function(t,e,n){const o=t.node,s=t.propSegments[1];o.getAttribute(s)!==n&&o.setAttribute(s,n)},style:function(t,e,n){const o=t.propSegments[1],s=t.node.style;s[o]!==n&&(s[o]=n)}},je={text:function(t,e,n){t.replaceNode.nodeValue!==n&&(t.replaceNode.nodeValue=n)},for:function(t,e,o){const s=t.statePathInfo,i=Rt(t),a=Ft(t),r=It(a),l=ae(i,r,o);if(e.newListValueByAbsAddress.set(a,Array.isArray(o)?o:[]),Array.isArray(r)&&r.length===l.deleteIndexSet.size&&l.deleteIndexSet.size>0&&null!==t.node.parentNode){let e=$e.get(t.node);if(void 0===e){const n=_e.get(t.node)||t.node;e=function(t,e){let n=t.previousSibling,o=e.nextSibling,s=!0;for(;null!==n;){if(n.nodeType===Node.ELEMENT_NODE||n.nodeType===Node.TEXT_NODE&&""!==(n.textContent?.trim()??"")){s=!1;break}n=n.previousSibling}for(;null!==o;){if(o.nodeType===Node.ELEMENT_NODE||o.nodeType===Node.TEXT_NODE&&""!==(o.textContent?.trim()??"")){s=!1;break}o=o.nextSibling}return s}(t.node,n),$e.set(t.node,e)}if(e){const e=t.node.parentNode;e.textContent="",e.appendChild(t.node)}}for(const e of l.deleteIndexSet){const n=ke(t.node,e);null!==n&&(be(n),n.unmount(),Ce(t,n),Me(t.node,e,null))}let d=t.node;const c=P(s.path+"."+m),u=e.stateElement.loopContextStack;let f=null;l.newIndexes.length==l.addIndexSet.size&&l.newIndexes.length>0&&d.isConnected&&(f=document.createDocumentFragment(),kt(f,e.rootNode));for(const o of l.newIndexes){let i;if(l.addIndexSet.has(o)){const a=Jt(c,o);u.createLoopContext(a,n=>{const o=Ee(t);i=o.pop(),void 0===i&&(i=Pe(t)),null!==f?i.appendTo(f):d.nextSibling!==i.firstNode&&i.mountAfter(d),we(i,n,e)}),void 0===i&&n(`Content not found for ListIndex: ${o.index} at path "${s.path}"`)}else{if(i=ke(t.node,o),l.changeIndexSet.has(o)){const t=Kt(i);for(const n of t)Ue(n,e)}null===i&&n(`Content not found for ListIndex: ${o.index} at path "${s.path}"`),d.nextSibling!==i.firstNode&&i.mountAfter(d)}d=i.lastNode||d,Me(t.node,o,i)}_e.set(t.node,d),null!==f&&(t.node.parentNode.insertBefore(f,t.node.nextSibling),kt(f,null))},if:We,else:We,elseif:We,radio:function(t,e,n){const o=t.node,s=zt(o.value,t.inFilters);o.checked=n===s},checkbox:function(t,e,n){const o=t.node,s=zt(o.value,t.inFilters),i=Array.isArray(n)?n:qt;o.checked=i.includes(s)}},Be=new WeakMap,ze=new WeakMap;function qe(t,e){const n=zt(Fe(e.state,t),t.outFilters);if(!0===ze.get(t))return void e.deferredSelectBindings.push({binding:t,value:n});let o=Be.get(t);if(void 0===o){if(Be.has(t))return Bt(t.replaceNode,e.stateElement)?(o=Oe,Be.set(t,o)):o=Re,void o(t,e,n);if(o=je[t.bindingType],void 0===o){const n=t.propSegments[0];if(o=De[n],Be.set(t,o),void 0===o){pt(t.replaceNode)?Bt(t.replaceNode,e.stateElement)?(o=Oe,Be.set(t,o)):o=Re:(o=Re,Be.set(t,o))}}if(o===Re){if("SELECT"===t.node.tagName){const o=t.propSegments[0];if("value"===o||"selectedIndex"===o)return e.deferredSelectBindings.push({binding:t,value:n}),void ze.set(t,!0)}}o(t,e,n)}else o(t,e,n)}function Ue(t,e){if(e.appliedBindingSet.has(t))return;e.appliedBindingSet.add(t);const o=Ft(t);if(e.updatedAbsAddressSetByStateElement.has(e.stateElement)){e.updatedAbsAddressSetByStateElement.get(e.stateElement).add(o)}else e.updatedAbsAddressSetByStateElement.set(e.stateElement,new Set([o]));if("event"===t.bindingType)return;const s=pt(t.replaceNode);if(s&&void 0===customElements.get(s))return;let i=t.replaceNode.getRootNode();if(i instanceof DocumentFragment&&!(i instanceof ShadowRoot)&&(i=Mt(i),null===i&&n("Root node for fragment not found for binding.")),t.stateName!==e.stateName||i!==e.rootNode){const o=Nn(i,t.stateName);null===o&&n(`State element with name "${t.stateName}" not found for binding.`),o.createState("readonly",n=>{const s={stateName:t.stateName,rootNode:i,stateElement:o,state:n,appliedBindingSet:e.appliedBindingSet,newListValueByAbsAddress:e.newListValueByAbsAddress,updatedAbsAddressSetByStateElement:e.updatedAbsAddressSetByStateElement,deferredSelectBindings:e.deferredSelectBindings};qe(t,s)})}else qe(t,e)}function Ve(t){let o=0;const s=new Set,i=new Map,a=new Map,r=[];for(;o<t.length;){let l=t[o];const d=l.stateName;if(!1===l.replaceNode.isConnected){e.debug&&console.log(`applyChangeFromBindings: skip disconnected binding: ${l.bindingType} ${l.statePathName} on ${l.node.nodeName}`,l),o++;continue}let c=l.replaceNode.getRootNode();c instanceof DocumentFragment&&!(c instanceof ShadowRoot)&&(c=Mt(c),null===c&&n("Root node for fragment not found for binding."));const u=Nn(c,d);null===u&&n(`State element with name "${d}" not found for binding.`),u.createState("readonly",e=>{const n={rootNode:c,stateName:d,stateElement:u,state:e,appliedBindingSet:s,newListValueByAbsAddress:i,updatedAbsAddressSetByStateElement:a,deferredSelectBindings:r};for(;;){Ue(l,n),o++;const e=t[o];if(!e)break;const s=e.replaceNode.getRootNode();if(e.stateName!==d||s!==n.rootNode)break;l=e}})}for(const{binding:t,value:e}of r)Re(t,0,e);for(const[t,e]of i.entries())St(t,e);for(const[t,e]of a.entries())t.createState("writable",t=>{t[lt](Array.from(e))})}const He=new Map,Je=new Map;const Xe=(t,e,o)=>s=>{const i=s.target;if(null===i)return void console.warn("[@wcstack/state] event.target is null.");if("radio"!==i.type)return void console.warn("[@wcstack/state] event.target is not a radio input element.");if(!1===i.checked)return;let a=i.value;for(const t of o)a=t.filterFn(a);const r=Nn(i.getRootNode(),t);null===r&&n(`State element with name "${t}" not found for two-way binding.`);const l=tt(i);r.createState("writable",t=>{t[ot](l,()=>{t[e]=a})})};function Ke(t){if("radio"===t.bindingType&&-1===t.propModifiers.indexOf("ro")){const e=function(t){let e="input";for(const n of t.propModifiers)n.startsWith("on")&&(e=n.slice(2));return e}(t),n=function(t,e){const n=t.inFilters.map(t=>t.filterName+"("+t.args.join(",")+")").join("|");return`${t.stateName}::${t.statePathName}::${e}::${n}`}(t,e);let o=He.get(n);void 0===o&&(o=Xe(t.stateName,t.statePathName,t.inFilters),He.set(n,o)),t.node.addEventListener(e,o);let s=Je.get(n);return void 0===s?(s=new Set([t]),Je.set(n,s)):s.add(t),!0}return!1}const Ye=new Map,Ge=new Map;const Qe=(t,e,o)=>s=>{const i=s.target;if(null===i)return void console.warn("[@wcstack/state] event.target is null.");if("checkbox"!==i.type)return void console.warn("[@wcstack/state] event.target is not a checkbox input element.");const a=i.checked;let r=i.value;for(const t of o)r=t.filterFn(r);const l=Nn(i.getRootNode(),t);null===l&&n(`State element with name "${t}" not found for two-way binding.`);const d=tt(i);l.createState("writable",t=>{t[ot](d,()=>{let n=t[e];if(Array.isArray(n))if(a)-1===n.indexOf(r)&&(t[e]=n.concat(r));else{const o=n.indexOf(r);-1!==o&&(t[e]=n.toSpliced(o,1))}else t[e]=a?[r]:[]})})};function Ze(t){if("checkbox"===t.bindingType&&-1===t.propModifiers.indexOf("ro")){const e=function(t){let e="input";for(const n of t.propModifiers)n.startsWith("on")&&(e=n.slice(2));return e}(t),n=function(t,e){const n=t.inFilters.map(t=>t.filterName+"("+t.args.join(",")+")").join("|");return`${t.stateName}::${t.statePathName}::${e}::${n}`}(t,e);let o=Ye.get(n);void 0===o&&(o=Qe(t.stateName,t.statePathName,t.inFilters),Ye.set(n,o)),t.node.addEventListener(e,o);let s=Ge.get(n);return void 0===s?(s=new Set([t]),Ge.set(n,s)):s.add(t),!0}return!1}function tn(t){for(const e of t)l(e),ut(e)||(xt(e),Ke(e),Ze(e))}function en(t,e){const[o,s]=function(t){const e=G(t),n=[];for(const t of e)if(!Q.has(t)){Q.add(t);const e=c(t,Y(t));p(t,e),r(t),n.push(...e)}return[e,n]}(t);for(const t of o)et(t,e);tn(s);for(const t of s){de(Ft(t),t);const e=Nn(t.replaceNode.getRootNode(),t.stateName);null===e&&n(`State element with name "${t.stateName}" not found for binding.`),"event"!==t.bindingType&&e.setPathInfo(t.statePathName,t.bindingType)}Ve(s)}const nn=/\{\{\s*(.+?)\s*\}\}/g,on=new Set(["SCRIPT","STYLE"]);function sn(t){if(!e.enableMustache)return;!function(t){const e=document.createTreeWalker(t,NodeFilter.SHOW_TEXT),n=[];for(;e.nextNode();)n.push(e.currentNode);for(const t of n)t.parentElement&&on.has(t.parentElement.tagName)||an(t)}(t);const n=Array.from(t.querySelectorAll("template"));for(const t of n)if("http://www.w3.org/2000/svg"===t.namespaceURI){const e=document.createElement("template"),n=Array.from(t.childNodes);for(let t=0;t<n.length;t++){const o=n[t];e.content.appendChild(o)}for(const n of t.attributes)e.setAttribute(n.name,n.value);t.replaceWith(e),sn(e.content)}else sn(t.content)}function an(t){const e=t.data;if(nn.lastIndex=0,!nn.test(e))return;nn.lastIndex=0;const n=document.createDocumentFragment();let o,s=0;for(;null!==(o=nn.exec(e));){o.index>s&&n.appendChild(document.createTextNode(e.slice(s,o.index)));const t=o[1];n.appendChild(document.createComment(`@@: ${t}`)),s=o.index+o[0].length}s<e.length&&n.appendChild(document.createTextNode(e.slice(s))),t.parentNode.replaceChild(n,t)}let rn;const ln=/^(\s*@@\s*(?:.*?)\s*:\s*)(.+?)(\s*)$/;function dn(t,e){const n=e+h+m,o=t.indexOf("|"),s=t.indexOf("@");let i,a;if(-1!==o?(i=t.slice(0,o).trim(),a=t.slice(o)):-1!==s?(i=t.slice(0,s).trim(),a=t.slice(s)):(i=t.trim(),a=""),"."===i)i=n;else{if(!i.startsWith("."))return t;i=n+h+i.slice(1)}return a.length>0?i+a:i}function cn(t,e){const n=ln.exec(t);if(null===n)return t;const o=n[1],s=n[2],i=n[3];return o+dn(s,e)+i}function un(t,e){const n=t.split(";");let o=!1;const s=n.map(t=>{const n=t.trim();if(0===n.length)return t;const s=n.indexOf(":");if(-1===s)return t;const i=n.slice(0,s).trim(),a=n.slice(s+1).trim(),r=dn(a,e);return r!==a?(o=!0,`${i}: ${r}`):t});return o?s.join(";"):t}function fn(t,e){return un(t,e)}function pn(t){let e=t;const n=[];for(;null!==e.parentNode;){const t=Array.from(e.parentNode.childNodes).indexOf(e);n.unshift(t),e=e.parentNode}return n}function hn(t){const e=[],n=G(t);for(const t of n){const n=Y(t);e.push({nodePath:pn(t),parseBindTextResults:n})}return e}const mn=new Map([["for",e.commentForPrefix],["if",e.commentIfPrefix],["elseif",e.commentElseIfPrefix],["else",e.commentElsePrefix]]),gn=function(){if(rn)return rn;const t=[],e=W("not",t)(T);return rn={filterName:"not",args:t,filterFn:e},rn}();function wn(t,e){const n=e.outFilters;return{...e,outFilters:[...n,gn],bindingType:t}}function bn(t,n,o,s){!function(t){const e=Array.from(t.childNodes);for(const n of e)n.nodeType===Node.TEXT_NODE&&""===(n.textContent||"").trim()&&t.removeChild(n)}(n),"string"==typeof s&&function(t,n){const o=e.bindAttributeName,s=document.createTreeWalker(t,NodeFilter.SHOW_COMMENT|NodeFilter.SHOW_ELEMENT);for(;s.nextNode();){const t=s.currentNode;if(t.nodeType===Node.COMMENT_NODE){const e=t;e.data=cn(e.data,n);continue}const e=t;if(e instanceof HTMLTemplateElement)continue;const i=e.getAttribute(o);if(null!==i){const t=un(i,n);t!==i&&e.setAttribute(o,t)}}}(n,s),xn(t,n,s);return{fragment:n,parseBindTextResult:o,nodeInfos:hn(n)}}function xn(t,o,s){const i=e.commentElsePrefix,a=document.createTreeWalker(o,NodeFilter.SHOW_ELEMENT,{acceptNode(t){const n=t;if("template"===n.tagName.toLowerCase()){if((n.getAttribute(e.bindAttributeName)||"").length>0)return NodeFilter.FILTER_ACCEPT}return NodeFilter.FILTER_SKIP}});let r=null;const l=[],d=[];for(;a.nextNode();){const t=a.currentNode;d.push(t)}for(const o of d){let a=o.getAttribute(e.bindAttributeName)||"";"string"==typeof s&&(a=fn(a,s));let d=z(a)[0];const c=mn.get(d.bindingType);if(void 0===c)continue;const u=d.bindingType,f=o.content,p=Gt();let h=null;const m="for"===u?d.statePathName:s;if("else"===u){null===r&&n("'else' binding found without preceding 'if' or 'elseif' binding."),d=wn("else",r.parseBindTextResult),h=bn(t,f,d,m),X(p,t,h);const e=l.at(-1),s=document.createComment(`@@${c}:${p}`);void 0!==e?(o.remove(),e.fragment.appendChild(s),e.nodeInfos.push({nodePath:pn(s),parseBindTextResults:Y(s)})):o.replaceWith(s)}else if("elseif"===u){null===r&&n("'elseif' binding found without preceding 'if' or 'elseif' binding."),h=bn(t,f,d,m),X(p,t,h);const e=document.createComment(`@@${c}:${p}`),s=Gt(),a={fragment:document.createDocumentFragment(),parseBindTextResult:wn("else",r.parseBindTextResult),nodeInfos:[]};a.fragment.appendChild(e),a.nodeInfos.push({nodePath:pn(e),parseBindTextResults:Y(e)}),X(s,t,a);const u=l.at(-1);l.push(a);const g=document.createComment(`@@${i}:${s}`);void 0!==u?(o.remove(),u.fragment.appendChild(g),u.nodeInfos.push({nodePath:pn(g),parseBindTextResults:Y(g)})):o.replaceWith(g)}else{h=bn(t,f,d,m),X(p,t,h);const e=document.createComment(`@@${c}:${p}`);o.replaceWith(e)}"if"===u?(l.length=0,r=h):"elseif"===u?r=h:"else"===u&&(r=null,l.length=0)}}async function yn(t){const n=t.querySelectorAll(e.tagNames.state),o=[];await customElements.whenDefined(e.tagNames.state);for(const t of n){const e=t;o.push(e.initializePromise)}await Promise.all(o)}async function In(t){if(t===document)await yn(document),sn(document),xn(document,document),en(document.body,null);else{const n=t;n.host.hasAttribute(e.bindAttributeName)&&await a(n.host),await yn(n),sn(n),xn(n,n),en(n,null)}}const Sn=new WeakMap;function Nn(t,e){let n=Sn.get(t);return n&&n.get(e)||null}function Pn(t,o,s){let i=Sn.get(t);if(null===s){if(!i)return;i.delete(o),0===i.size&&Sn.delete(t),e.debug&&console.debug(`State element unregistered: name="${o}"`)}else i||(i=new Map,Sn.set(t,i),("HTMLDocument"===t.constructor.name||"Document"===t.constructor.name||"ShadowRoot"===t.constructor.name)&&queueMicrotask(()=>{In(t)})),i.has(o)&&n(`State element with name "${o}" is already registered.`),i.set(o,s),e.debug&&console.debug(`State element registered: name="${o}"`,s)}class _n{_loopContextStack=Array(g).fill(void 0);_length=0;createLoopContext(t,e){null===t.listIndex&&n("Cannot create loop context for a state address that does not have a list index.");const o=t;this._length>=g&&n("Exceeded maximum loop context stack depth of 128. Possible infinite loop.");const s=this._loopContextStack[this._length-1];if(void 0!==s){s.pathInfo.wildcardCount+1!==o.pathInfo.wildcardCount&&n("Cannot push loop context for a list whose wildcard count is not exactly one more than the current active loop context.");const t=o.pathInfo.wildcardPathInfos[o.pathInfo.wildcardPathInfos.length-2];s.pathInfo!==t&&n("Cannot push loop context for a list whose parent wildcard path info does not match the current active loop context.")}else 1!==o.pathInfo.wildcardCount&&n("Cannot push loop context for a list with wildcard positions when there is no active loop context.");let i;this._loopContextStack[this._length]=o,this._length++;try{i=e(o)}finally{i instanceof Promise?i.finally(()=>{this._length--,this._loopContextStack[this._length]=void 0}):(this._length--,this._loopContextStack[this._length]=void 0)}return i}}const vn=new Map;class An{path;segments;paths;wildcardCount;wildcardType;wildcardIndexes;pathInfo;constructor(t){const e=t.split("."),n=e.slice(),o=[];let s=0,i=0,a="",r=0,l="none";const d=[];for(let t=0;t<e.length;t++){const l=e[t];if("*"===l)n[t]="*",d.push(null),s++,r++;else{const e=Number(l);Number.isNaN(e)||(n[t]="*",d.push(e),i++,r++)}a+=l,o.push(a),a+=t<l.length-1?".":""}const c=P(n.join("."));(s>0||i>0)&&(l=s===r?"context":i===r?"all":"partial"),this.path=t,this.segments=e,this.paths=o,this.wildcardCount=r,this.wildcardType=l,this.wildcardIndexes=d,this.pathInfo=c}}function $n(t){let e;return vn.get(t)??(vn.set(t,e=new An(t)),e)}const En=new WeakMap;function Cn(t,e){null===e?En.delete(t):En.set(t,e)}function kn(t){const e=En.get(t);e&&(e.dirty=!0)}function Mn(t,e,o,s,i){if(!(e.pathInfo.path in t)){const i=Ln(t,e.parentAddress??n(`address.parentAddress is undefined path: ${e.pathInfo.path}`),o,s),a=e.pathInfo.segments[e.pathInfo.segments.length-1];if(a===m){const t=e.listIndex?.index??n(`address.listIndex?.index is undefined path: ${e.pathInfo.path}`);return Reflect.get(i,t)}return Reflect.get(i,a)}if(!i.getterPaths.has(e.pathInfo.path))return Reflect.get(t,e.pathInfo.path);s.pushAddress(e);try{return Reflect.get(t,e.pathInfo.path,o)}finally{s.popAddress()}}function Tn(t,e,n,o,s){const i=Et(Pt(s,e.pathInfo),e.listIndex),a=function(t){return En.get(t)??null}(i);if(null!==a&&!1===a.dirty)return a.value;const r=Mn(t,e,n,o,s);return Cn(i,{value:r,dirty:!1}),r}function Ln(t,e,n,o){!function(t,e){if(t.addressStackLength>0){const n=t.lastAddressStack?.pathInfo??null,o=t.stateElement;null!==n&&o.getterPaths.has(n.path)&&n.path!==e.pathInfo.path&&o.addDynamicDependency(e.pathInfo.path,n.path)}}(o,e);const s=o.stateElement;return e.pathInfo.wildcardCount>0||s.getterPaths.has(e.pathInfo.path)?Tn(t,e,n,o,s):Mn(t,e,n,o,s)}function Wn(t,e){if(0===t.addressStackLength)return null;const n=t.lastAddressStack;if(null===n)return null;const o=n.pathInfo.indexByWildcardPath[e];return void 0===o?null:n.listIndex?.at(o)??null}const Rn=new class{_queueAbsoluteAddresses=[];constructor(){}enqueueAbsoluteAddress(t){const e=0===this._queueAbsoluteAddresses.length;this._queueAbsoluteAddresses.push(t),e&&queueMicrotask(()=>{const t=this._queueAbsoluteAddresses;this._queueAbsoluteAddresses=[],this._applyChange(t)})}testApplyChange(t){this._applyChange(t)}_applyChange(t){const e=new Set(t),n=[];for(const t of e){const e=le(t);for(const t of e)!1!==t.replaceNode.isConnected&&n.push(t)}Ve(n)}};function On(){return Rn}const Fn=new WeakMap;function Dn(t,e){null===e?Fn.delete(t):Fn.set(t,e)}function jn(t,n,o){const s=P(t.wildcardParentPaths[n]),i=Pt(t.stateElement,s),a=Jt(s,o),r=It(Et(i,o)),l=t.stateProxy[st](a),d=function(t,n){switch(n){case"old":return t.oldIndexes;case"new":return t.newIndexes;case"add":return t.addIndexSet;case"change":return t.changeIndexSet;case"delete":return t.deleteIndexSet;default:return e.debug&&console.log(`Invalid search type: ${n}`),[]}}(ae(a.listIndex,r,l),t.searchType);if(n===t.wildcardPaths.length-1)t.targetListIndexes.push(...d);else for(const e of d)jn(t,n+1,e)}function Bn(t,e,o,s,i,a,r,l,d){const c={stateElement:e,staticMap:s,dynamicMap:i,result:new Set,listPathSet:a,visited:new Set,stateProxy:r,searchType:l};return function(t,e,o){const s=[{address:e,depth:0}];for(;s.length>0;){const{address:e,depth:i}=s.pop();if(i>1e3&&n(`Maximum dependency depth of 1000 exceeded. Possible circular dependency detected at path: ${e.pathInfo.path}`),t.visited.has(e))continue;t.visited.add(e),o(e);const a=e.pathInfo.path,r=i+1,l=[],d=t.staticMap.get(a);if(d)for(const n of d){const o=P(n);if(t.listPathSet.has(a)&&o.lastSegment===m){const n=t.stateProxy[st](e),s=It(Et(Pt(t.stateElement,e.pathInfo),e.listIndex)),i=ae(e.listIndex,s,n);for(const e of i.newIndexes){const n=Jt(o,e);t.result.add(n),l.push({address:n,depth:r})}}else{const n=Jt(o,e.listIndex);t.result.add(n),l.push({address:n,depth:r})}}const c=t.dynamicMap.get(a);if(c)for(const o of c){const s=P(o),i=[];if(s.wildcardCount>0){const o=Lt(e.pathInfo,s);if(s.wildcardCount-o>=1){let a;o>0?(null===e.listIndex&&n(`Cannot expand dynamic dependency with wildcard for non-list address: ${e.pathInfo.path}`),a=e.listIndex.at(o-1)):a=null;const r={stateElement:t.stateElement,targetListIndexes:[],wildcardPaths:s.wildcardPaths,wildcardParentPaths:s.wildcardParentPaths,stateProxy:t.stateProxy,searchType:t.searchType};jn(r,o,a),i.push(...r.targetListIndexes)}else{null===e.listIndex&&n(`Cannot expand dynamic dependency with wildcard for non-list address: ${e.pathInfo.path}`);const t=e.listIndex.at(o-1);i.push(t)}}else i.push(null);for(const e of i){const n=Jt(s,e);t.result.add(n),l.push({address:n,depth:r})}}for(let t=l.length-1;t>=0;t--)s.push(l[t])}}(c,o,d),Array.from(c.result)}function zn(t,e,o,s,i,a){try{if(!(e.pathInfo.path in t)){const o=Ln(t,e.parentAddress??n(`address.parentAddress is undefined path: ${e.pathInfo.path}`),i,a),r=e.pathInfo.segments[e.pathInfo.segments.length-1];if(r===m){const t=e.listIndex?.index??n(`address.listIndex?.index is undefined path: ${e.pathInfo.path}`);return Reflect.set(o,t,s)}return Reflect.set(o,r,s)}if(!a.stateElement.setterPaths.has(e.pathInfo.path))return Reflect.set(t,e.pathInfo.path,s);a.pushAddress(e);try{return Reflect.set(t,e.pathInfo.path,s,i)}finally{a.popAddress()}}finally{const t=On();t.enqueueAbsoluteAddress(o),Bn(a.stateName,a.stateElement,e,a.stateElement.staticDependency,a.stateElement.dynamicDependency,a.stateElement.listPaths,i,"new",n=>{if(n===e)return;const o=Et(Pt(a.stateElement,n.pathInfo),n.listIndex);kn(o),t.enqueueAbsoluteAddress(o)})}}function qn(t,e,o,s,i,a){let r=e.parentAddress??n(`address.parentAddress is undefined path: ${e.pathInfo.path}`),l=function(t){return Fn.get(t)??null}(r);if(null===l){const e=Ln(t,r,i,a)??[],n=ne(e)??[];l={value:[...e],listIndexes:[...n]},Dn(r,l)}try{return zn(t,e,o,s,i,a)}finally{const n=l.value.indexOf(s),o=Ln(t,r,i,a)??[],d=Array.isArray(o)?ne(o)??[]:[],c=e.listIndex.index,u=-1!==n?l.listIndexes[n]:te(r.listIndex,-1);d[c]=u;if(new Set(o).size===l.value.length){for(let t=0;t<d.length;t++)d[t].index=t;Dn(r,null)}}}function Un(t,e,n,o,s){const i=s.stateElement,a=i.elementPaths.has(e.pathInfo.path),r=e.pathInfo.wildcardCount>0||i.getterPaths.has(e.pathInfo.path),l=Et(Pt(i,e.pathInfo),e.listIndex);try{return a?qn(t,e,l,n,o,s):zn(t,e,l,n,o,s)}finally{r&&Cn(l,{value:n,dirty:!1})}}function Vn(t,e,o,s){return(e,i,a)=>{const r=P(e);if(s.addressStackLength>0){const t=s.lastAddressStack?.pathInfo??null,e=s.stateElement;null!==t&&t.path!==r.path&&e.getterPaths.has(t.path)&&e.addDynamicDependency(r.path,t.path)}r.wildcardParentPathInfos.length>i.length&&n(`indexes length is insufficient: ${e}`);let l=null;for(let e=0;e<r.wildcardParentPathInfos.length;e++){const a=r.wildcardParentPathInfos[e],d=Jt(a,l),c=ne(Ln(t,d,o,s));null==c&&n(`ListIndexes not found: ${a.path}`);l=c[i[e]]??n(`ListIndex not found: ${a.path}`)}const d=Jt(r,l);if(!(void 0!==a))return Ln(t,d,o,s);Un(t,d,a,o,s)}}const Hn=new WeakMap;function Jn(t,e,o,s){const i=e.pathInfo;switch(e.wildcardType){case"none":return null;case"context":return Wn(s,i.wildcardPaths.at(-1)??n(`lastWildcardPath is null: ${e.pathInfo.path}`))??n(`ListIndex not found: ${e.pathInfo.path}`);case"all":{let i=null;for(let a=0;a<e.pathInfo.wildcardCount;a++){const r=e.pathInfo.wildcardParentPathInfos[a]??n(`wildcardParentPathInfo is null: ${e.pathInfo.path}`);i=(ne(Ln(t,Jt(r,i),o,s))??n(`ListIndex not found: ${r.path}`))[e.wildcardIndexes[a]??n(`wildcardIndex is null: ${e.pathInfo.path}`)]??n(`ListIndex not found: ${r.path}`)}return i}case"partial":n(`Partial wildcard type is not supported yet: ${e.pathInfo.path}`)}}function Xn(t,e,o){void 0!==t.loopContext&&n("already in loop context"),t.setLoopContext(e);try{t.pushAddress(e);try{return o()}finally{t.popAddress()}}finally{t.clearLoopContext()}}function Kn(t,e,o,s){const i=b[e];if(void 0!==i){0===s.addressStackLength&&n(`No active state reference to get list index for "${e.toString()}".`);const t=s.lastAddressStack?.listIndex;return t?.indexes[i]??n(`ListIndex not found: ${e.toString()}`)}if("string"==typeof e){if("$"!==e[0]){const n=$n(e),i=Jn(t,n,o,s),a=Jt(n.pathInfo,i);return Ln(t,a,o,s)}switch(e){case"$stateElement":return s.stateElement;case"$getAll":return(e,i)=>function(t,e,o,s){const i=Vn(t,0,o,s);return(e,a)=>{const r=new Map,l=P(e);if(s.addressStackLength>0){const t=s.lastAddressStack?.pathInfo??null,e=s.stateElement;null!==t&&t.path!==l.path&&e.getterPaths.has(t.path)&&e.addDynamicDependency(l.path,t.path)}if(void 0===a){for(let t=0;t<l.wildcardParentPathInfos.length;t++){const e=l.wildcardParentPathInfos[t],n=Wn(s,e.path);if(n){a=n.indexes;break}}void 0===a&&(a=[])}const d=(e,i,a,l,c,u,f)=>{const p=e[i]??null;if(null===p)return void f.push(u);const h=Jt(p,a),m=Hn.get(h),g=Ln(t,h,o,s),w=ae(a,m,g).newIndexes,b=l[c]??null;if(r.set(h,g),null===b)for(let t=0;t<w.length;t++){const n=w[t];d(e,i+1,n,l,c+1,u.concat(n.index),f)}else{const t=w[b]??n(`ListIndex not found: ${p.path}`);i+1<e.length?d(e,i+1,t,l,c+1,u.concat(t.index),f):f.push(u.concat(t.index))}},c=[];d(l.wildcardParentPathInfos,0,null,a,0,[],c);const u=[];for(let t=0;t<c.length;t++)u.push(i(l.path,c[t]));for(const[t,e]of r.entries())Hn.set(t,e);return u}}(t,0,o,s)(e,i);case"$postUpdate":return e=>function(t,e,n,o){const s=o.stateElement;return e=>{const i=$n(e),a=Jn(t,i,n,o),r=Jt(i.pathInfo,a),l=Et(Pt(s,r.pathInfo),r.listIndex),d=On();d.enqueueAbsoluteAddress(l),Bn(o.stateName,o.stateElement,r,o.stateElement.staticDependency,o.stateElement.dynamicDependency,o.stateElement.listPaths,n,"new",t=>{const e=Et(Pt(s,t.pathInfo),t.listIndex);kn(e),d.enqueueAbsoluteAddress(e)})}}(t,0,o,s)(e);case"$resolve":return(e,n,i)=>Vn(t,0,o,s)(e,n,i);case"$trackDependency":return t=>function(t,e,o,s){return t=>{0===s.addressStackLength&&n(`No active state reference to track dependency for path "${t}".`);const e=s.lastAddressStack?.pathInfo??n("Internal error: lastAddressStack is null"),o=s.stateElement;s.stateElement.getterPaths.has(e.path)&&e.path!==t&&o.addDynamicDependency(t,e.path)}}(0,0,0,s)(t)}}else if("symbol"==typeof e){switch(e){case nt:return(t,e=async()=>{})=>async function(t,e,n){return await Xn(t,e,n)}(s,t,e);case ot:return(t,e=()=>{})=>function(t,e,n){return Xn(t,e,n)}(s,t,e);case st:return e=>Ln(t,e,o,s);case it:return(e,n)=>Un(t,e,n,o,s);case at:return()=>async function(t,e,n){const o=Reflect.get(t,x);"function"==typeof o&&await o.call(n)}(t,0,o);case rt:return()=>function(t,e,n){const o=Reflect.get(t,y);"function"==typeof o&&o.call(n)}(t,0,o);case lt:return e=>function(t,e,n,o){const s=Reflect.get(t,"$updatedCallback");if("function"==typeof s){const t=new Set,i={};for(const n of e){const e=n.absolutePathInfo.pathInfo;let s;if(s=n.absolutePathInfo.stateName===o.stateName?e.path:e.path+"@"+n.absolutePathInfo.stateName,t.add(s),e.wildcardCount>0){const t=n.listIndex.indexes??[],e=i[s];void 0===e?i[s]=[t]:e.push(t)}}return s.call(n,Array.from(t),i)}}(t,e,o,s)}return Reflect.get(t,e,o)}}class Yn{_stateElement;_stateName;_addressStack=Array(g).fill(void 0);_addressStackIndex=-1;_loopContext;_mutability;constructor(t,e,o){this._stateName=e;const s=Nn(t,this._stateName);null===s&&n(`StateHandler: State element with name "${this._stateName}" not found.`),this._stateElement=s,this._mutability=o}get stateName(){return this._stateName}get stateElement(){return this._stateElement}get lastAddressStack(){let t;return this._addressStackIndex>=0&&(t=this._addressStack[this._addressStackIndex]),void 0===t&&n("Last address stack is undefined."),t}get addressStackLength(){return this._addressStackIndex+1}get loopContext(){return this._loopContext}pushAddress(t){this._addressStackIndex++,this._addressStackIndex>=g&&n("Exceeded maximum address stack depth of 128. Possible infinite loop."),this._addressStack[this._addressStackIndex]=t}popAddress(){if(this._addressStackIndex<0)return null;const t=this._addressStack[this._addressStackIndex];return void 0===t&&n(`Address stack at index ${this._addressStackIndex} is undefined.`),this._addressStack[this._addressStackIndex]=void 0,this._addressStackIndex--,t}setLoopContext(t){this._loopContext=t}clearLoopContext(){this._loopContext=void 0}get(t,e,n){return Kn(t,e,n,this)}set(t,e,o,s){return"readonly"===this._mutability&&n(`State "${this._stateName}" is readonly.`),function(t,e,n,o,s){if("string"==typeof e){const i=$n(e),a=Jn(t,i,o,s);return Un(t,Jt(i.pathInfo,a),n,o,s)}return Reflect.set(t,e,n,o)}(t,e,o,s,this)}has(t,e){return Reflect.has(t,e)}}const Gn=new WeakMap;const Qn=new WeakMap;function Zn(t,e){const n=Qn.get(t);return n?n.get(e)??null:null}const to=new WeakMap,eo=new WeakMap,no=new WeakMap,oo=new WeakMap;function so(t,e){return{innerAbsPathInfo:Pt(t,P(e.propSegments.slice(1).join(h))),outerAbsPathInfo:Ft(e).absolutePathInfo}}function io(t,e){let o=to.get(t);if(void 0===o&&(o=new Map,to.set(t,o)),o.has(e))return o.get(e);let s=eo.get(t);void 0===s&&(s=new Map,eo.set(t,s));const i=no.get(t);if(void 0===i)return null;let a=null;for(const t of i)if(e.pathInfo.cumulativePathInfoSet.has(t.innerAbsPathInfo.pathInfo)){t.innerAbsPathInfo.pathInfo.segments.length===e.pathInfo.segments.length&&n("Duplicate mapping rule for web component."),a=t;break}if(null===a)return null;const r=oo.get(a);void 0===r&&n("Binding not found for primary mapping rule on web component.");const l=e.pathInfo.segments.slice(a.innerAbsPathInfo.pathInfo.segments.length),d=P(a.outerAbsPathInfo.pathInfo.segments.concat(l).join(h)),c=Nn(t.getRootNode(),r.stateName);null===c&&n(`State element with name "${r.stateName}" not found for web component.`);const u=Pt(c,d);o.set(e,u),s.set(u,e);return function(t,e){const n=f(t);null===n?p(t,[e]):n.push(e)}(t,{...r,propName:e.pathInfo.path,propSegments:e.pathInfo.segments,statePathName:u.pathInfo.path,statePathInfo:u.pathInfo}),u}function ao(t){return function(t){const e=Object.getPrototypeOf(t),n=Object.create(e),o=Object.getOwnPropertyDescriptors(t);for(const t in o){const e=o[t];!1===e.writable&&(e.writable=!0)}return Object.defineProperties(n,o),n}(t)}class ro{_webComponent;_innerStateElement;constructor(t,e){this._webComponent=t,this._innerStateElement=Zn(t,e)??n("State element not found for web component.")}get(t,e,o){if("string"!=typeof e)return Reflect.get(t,e,o);{if("then"===e)return;if("$"===e[0])return;if(this._innerStateElement.getterPaths.has(e)&&e in t)return Reflect.get(t,e,o);const s=P(e),i=Pt(this._innerStateElement,s),a=io(this._webComponent,i);if(null!==a){const t=tt(this._webComponent);let e;return a.stateElement.createState("readonly",n=>{n[ot](t,()=>{e=n[a.pathInfo.path];let o=null;null!==t&&null!==t.listIndex&&a.pathInfo.wildcardCount>0&&(o=t.listIndex.at(a.pathInfo.wildcardCount-1));!function(t,e){Gn.set(t,e)}(Et(a,o),e)})}),e}if(e in t)return Reflect.get(t,e,o);n(`Property "${e}" not found in inner state: no mapping rule and no local state property.`)}}set(t,e,o,s){if("string"!=typeof e)return Reflect.set(t,e,o,s);{if(this._innerStateElement.setterPaths.has(e)&&e in t)return Reflect.set(t,e,o,s);const i=P(e),a=Pt(this._innerStateElement,i),r=io(this._webComponent,a);if(null!==r){const t=tt(this._webComponent);return r.stateElement.createState("writable",e=>{e[ot](t,()=>{e[r.pathInfo.path]=o})}),!0}if(e in t)return Reflect.set(t,e,o,s);n(`Property "${e}" not found in inner state: no mapping rule and no local state property.`)}}has(t,e){if("string"==typeof e){if("$"===e[0])return!1;if((this._innerStateElement.getterPaths.has(e)||this._innerStateElement.setterPaths.has(e))&&e in t)return!0;const n=P(e),o=Pt(this._innerStateElement,n);return null!==io(this._webComponent,o)||e in t}return Reflect.has(t,e)}}class lo{_innerStateElement;constructor(t,e){this._innerStateElement=Zn(t,e)??n("State element not found for web component.")}get(t,e,n){if("string"==typeof e){const t=P(e),n=Et(Pt(this._innerStateElement,t),null);return o=n,Gn.get(o)}return Reflect.get(t,e,n);var o}set(t,e,n,o){if("string"==typeof e){const t=P(e),n=Pt(this._innerStateElement,t);return this._innerStateElement.createState("readonly",t=>{t.$postUpdate(n.pathInfo.path)}),!0}return Reflect.set(t,e,n,o)}}class co{_innerStateElement;constructor(t,e){this._innerStateElement=Zn(t,e)??n("State element not found for web component.")}get(t,e,n){if("string"==typeof e){let t;return this._innerStateElement.createState("readonly",n=>{t=n[e]}),t}return Reflect.get(t,e,n)}set(t,e,n,o){return"string"==typeof e?(this._innerStateElement.createState("writable",t=>{t[e]=n}),!0):Reflect.set(t,e,n,o)}}const uo=t=>()=>t;function fo(t,o,s,i){if(function(t,e,n){let o=Qn.get(t);o||(o=new Map,Qn.set(t,o)),o.set(e,n)}(o,s,t),o.hasAttribute(e.bindAttributeName)){const e=(f(o)??[]).filter(t=>t.propSegments[0]===s);!function(t,e,o){if(0===o.length)return;const s=Zn(t,e);null===s&&n("State element not found for web component.");const i=new Map,a=new Map;for(const e of o){const n=so(s,e);let o=no.get(t);void 0===o?no.set(t,new Set([n])):o.add(n);const r=n.innerAbsPathInfo,l=n.outerAbsPathInfo;oo.set(n,e),i.set(r,l),a.set(l,r)}to.set(t,i),eo.set(t,a)}(o,s,e);const i=function(t,e){const n=new lo(t,e);return new Proxy({},n)}(o,s),a=function(t,e){const o=new ro(t,e),s=Zn(t,e);null===s&&n("State element not found for web component."),null===s.boundComponentStateProp&&n("State element is not bound to any component state prop."),s.boundComponentStateProp in t||n(`State element is not bound to a valid component state prop: ${s.boundComponentStateProp}`);const i=t[s.boundComponentStateProp];return"object"==typeof i&&null!==i||n(`Invalid state object for component state prop: ${s.boundComponentStateProp}`),new Proxy(ao(i),o)}(o,s);t.setInitialState(a),Object.defineProperty(o,s,{get:uo(i),enumerable:!0,configurable:!0})}else{t.setInitialState(ao(i));const e=function(t,e){const n=new co(t,e);return new Proxy({},n)}(o,s);Object.defineProperty(o,s,{get:uo(e),enumerable:!0,configurable:!0})}if(function(t,e){let n=jt.get(t);n||(n=new WeakMap,jt.set(t,n)),n.set(e,!0)}(o,t),I in o){const t=o[I];"function"==typeof t?t.call(o,s).catch(t=>{n(`Error in ${I}: ${t instanceof Error?t.message:String(t)}`)}):n(`${I} is not a function.`)}}function po(t){const e=new Set,n=new Set,o=function(t){let e={},n=t;for(;n&&n!==Object.prototype;)Object.assign(e,Object.getOwnPropertyDescriptors(n)),n=Object.getPrototypeOf(n);return e}(t);for(const[t,s]of Object.entries(o))"function"==typeof s.get&&e.add(t),"function"==typeof s.set&&n.add(t);return{getterPaths:e,setterPaths:n}}class ho extends HTMLElement{__state;_name="default";_initialized=!1;_initializePromise;_resolveInitialize=null;_loadingPromise;_resolveLoading=null;_setStatePromise=null;_resolveSetState=null;_listPaths=new Set;_elementPaths=new Set;_getterPaths=new Set;_setterPaths=new Set;_loopContextStack=function(){return new _n}();_dynamicDependency=new Map;_staticDependency=new Map;_pathSet=new Set;_version=0;_rootNode=null;_boundComponent=null;_boundComponentStateProp=null;constructor(){super(),this._initializePromise=new Promise(t=>{this._resolveInitialize=t}),this._loadingPromise=new Promise(t=>{this._resolveLoading=t}),this._setStatePromise=new Promise(t=>{this._resolveSetState=t})}get _state(){return void 0===this.__state&&n(`${e.tagNames.state} _state is not initialized yet.`),this.__state}set _state(t){this.__state=t,this._listPaths.clear(),this._elementPaths.clear(),this._getterPaths.clear(),this._pathSet.clear();const e=po(t);for(const t of e.getterPaths)this._getterPaths.add(t);for(const t of e.setterPaths)this._setterPaths.add(t);this._resolveLoading?.()}get name(){return this._name}async _initialize(){try{if(this.hasAttribute("state")){const t=this.getAttribute("state");this._state=function(t){const e=document.getElementById(t);if(e&&"application/json"===e.type)try{return JSON.parse(e.textContent||"{}")}catch(t){n("Failed to parse JSON from script element:"+t)}return{}}(t)}else if(this.hasAttribute("src")){const t=this.getAttribute("src");t&&t.endsWith(".json")?this._state=await async function(t){try{const e=await fetch(t);return e.ok||n(`Failed to fetch JSON file: ${e.statusText}`),await e.json()}catch(t){return console.error("Failed to load JSON file:",t),{}}}(t):t&&t.endsWith(".js")?this._state=await async function(t){try{return(await import(t)).default||{}}catch(t){n(`Failed to load script file: ${t}`)}}(t):n(`Unsupported src file type: ${t}`)}else if(this.hasAttribute("json")){const t=this.getAttribute("json");this._state=JSON.parse(t)}else{const t=this.querySelector('script[type="module"]');if(t)this._state=await async function(t,e){let n=null;const o=`\n//# sourceURL=${e}\n`;if("function"==typeof URL.createObjectURL){const e=new Blob([t.text+o],{type:"application/javascript"}),s=URL.createObjectURL(e);try{n=await import(s)}finally{URL.revokeObjectURL(s)}}else{const e=btoa(String.fromCodePoint(...(new TextEncoder).encode(t.text+o)));n=await import(`data:application/javascript;base64,${e}`)}return n&&"object"==typeof n.default?n.default:{}}(t,`${this._name}`);else{const t=setTimeout(()=>{console.warn(`[@wcstack/state] Warning: No state source found for <${e.tagNames.state}> element with name="${this._name}".`)},6e4);this._state=await this._setStatePromise,clearTimeout(t)}}}catch(t){n(`Failed to initialize state: ${t}`)}await this._loadingPromise,this._name=this.getAttribute("name")||"default",Pn(this.rootNode,this._name,this)}async _initializeBindWebComponent(){if(this.hasAttribute("bind-component")){const t=this.parentNode,o=t instanceof ShadowRoot?t.host:t instanceof Element?t:null,s=o?pt(o):null;null!==o&&null!==s||n(`"bind-component" requires <${e.tagNames.state}> to be a direct child of a custom element.`),t instanceof ShadowRoot||this.hasAttribute("name")||n('"bind-component" in Light DOM requires a "name" attribute to avoid namespace conflicts with the parent scope.');const i=this.getAttribute("bind-component");await customElements.whenDefined(s.toLowerCase()),o.hasAttribute(e.bindAttributeName)&&await a(o),i in o||n(`Component does not have property "${i}" for state binding.`);const r=o[i];"object"==typeof r&&null!==r||n(`Component property "${i}" is not an object for state binding.`),this._boundComponent=o,this._boundComponentStateProp=i,fo(this,this._boundComponent,this._boundComponentStateProp,r)}}async _callStateConnectedCallback(){await this.createStateAsync("writable",async t=>{x in t&&await t[at]()})}_callStateDisconnectedCallback(){this.createState("writable",t=>{y in t&&t[rt]()})}async connectedCallback(){this._rootNode=this.getRootNode(),this._initialized||(await this._initializeBindWebComponent(),await this._initialize(),this._initialized=!0,this._resolveInitialize?.()),await this._callStateConnectedCallback()}disconnectedCallback(){null!==this._rootNode&&(this._callStateDisconnectedCallback(),Pn(this.rootNode,this._name,null),this._rootNode=null)}get initializePromise(){return this._initializePromise}get listPaths(){return this._listPaths}get elementPaths(){return this._elementPaths}get getterPaths(){return this._getterPaths}get setterPaths(){return this._setterPaths}get loopContextStack(){return this._loopContextStack}get dynamicDependency(){return this._dynamicDependency}get staticDependency(){return this._staticDependency}get version(){return this._version}get rootNode(){return null===this._rootNode&&n("State rootNode is not available."),this._rootNode}get boundComponentStateProp(){return this._boundComponentStateProp}_addDependency(t,e,n){const o=t.get(e);return void 0===o?(t.set(e,[n]),!0):!o.includes(n)&&(o.push(n),!0)}addDynamicDependency(t,e){return this._addDependency(this._dynamicDependency,t,e)}addStaticDependency(t,e){return this._addDependency(this._staticDependency,t,e)}setPathInfo(t,e){if("for"===e&&(this._listPaths.add(t),this._elementPaths.add(t+"."+m)),!this._pathSet.has(t)){const e=P(t);if(this._pathSet.add(t),null!==e.parentPath){let t=e;for(;null!==t.parentPath&&this.addStaticDependency(t.parentPath,t.path);)t=P(t.parentPath)}}}_createState(t,e,n){try{const o=function(t,e,n,o){const s=new Yn(t,n,o);return new Proxy(e,s)}(t,this._state,this._name,e);return n(o)}finally{}}async createStateAsync(t,e){return await this._createState(this.rootNode,t,e)}createState(t,e){this._createState(this.rootNode,t,e)}nextVersion(){return this._version++,this._version}bindProperty(t,e){Object.defineProperty(this._state,t,e)}setInitialState(t){this._initialized?this._state=t:this._resolveSetState?.(t)}}function mo(){customElements.get(e.tagNames.state)||customElements.define(e.tagNames.state,ho)}function go(e){var n;e&&((n=e).tagNames&&Object.assign(t.tagNames,n.tagNames),"string"==typeof n.bindAttributeName&&(t.bindAttributeName=n.bindAttributeName),"string"==typeof n.commentTextPrefix&&(t.commentTextPrefix=n.commentTextPrefix),"string"==typeof n.commentForPrefix&&(t.commentForPrefix=n.commentForPrefix),"string"==typeof n.commentIfPrefix&&(t.commentIfPrefix=n.commentIfPrefix),"string"==typeof n.commentElseIfPrefix&&(t.commentElseIfPrefix=n.commentElseIfPrefix),"string"==typeof n.commentElsePrefix&&(t.commentElsePrefix=n.commentElsePrefix),"string"==typeof n.locale&&(t.locale=n.locale),"boolean"==typeof n.debug&&(t.debug=n.debug),"boolean"==typeof n.enableMustache&&(t.enableMustache=n.enableMustache)),mo()}function wo(t){return t}export{go as bootstrapState,wo as defineState};
|
|
2
2
|
//# sourceMappingURL=index.esm.min.js.map
|