@wcstack/state 4.10.1 → 4.11.1

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.
Files changed (54) hide show
  1. package/README.ja.md +27 -7
  2. package/README.md +28 -7
  3. package/dist/auto.min.js +1 -1
  4. package/dist/auto.min.js.map +1 -1
  5. package/dist/index.d.ts +5 -1
  6. package/dist/index.esm.js +1 -1
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/split/auto.js +1 -1
  9. package/dist/split/chunks/{chunk-JCQTA35B.js → chunk-AKTGQ6TC.js} +2 -2
  10. package/dist/split/chunks/chunk-CYMUD7KV.js +2 -0
  11. package/dist/split/chunks/chunk-CYMUD7KV.js.map +1 -0
  12. package/dist/split/chunks/chunk-E3XSBW55.js +2 -0
  13. package/dist/split/chunks/chunk-IYPAK7PO.js +2 -0
  14. package/dist/split/chunks/chunk-IYPAK7PO.js.map +1 -0
  15. package/dist/split/chunks/{chunk-FFXJVUTT.js → chunk-JJYM4JVZ.js} +2 -2
  16. package/dist/split/chunks/chunk-KBQKAD3I.js +2 -0
  17. package/dist/split/chunks/{chunk-PTFWXISY.js.map → chunk-KBQKAD3I.js.map} +1 -1
  18. package/dist/split/chunks/{chunk-SJRJALR4.js → chunk-LPJFTEVT.js} +2 -2
  19. package/dist/split/chunks/chunk-NAV7FBTI.js +2 -0
  20. package/dist/split/chunks/{chunk-MZVF2WEY.js.map → chunk-NAV7FBTI.js.map} +1 -1
  21. package/dist/split/chunks/{chunk-FT7OW7CG.js → chunk-R6RHIIR4.js} +2 -2
  22. package/dist/split/chunks/chunk-REB4RMZ3.js +2 -0
  23. package/dist/split/chunks/{chunk-65B2K2XD.js.map → chunk-REB4RMZ3.js.map} +1 -1
  24. package/dist/split/chunks/chunk-T66VJGI5.js +2 -0
  25. package/dist/split/chunks/{chunk-SANIIUS2.js.map → chunk-T66VJGI5.js.map} +1 -1
  26. package/dist/split/core.js +1 -1
  27. package/dist/split/features/devtools.js +1 -1
  28. package/dist/split/features/devtools.js.map +1 -1
  29. package/dist/split/features/diagnostics.js +1 -1
  30. package/dist/split/features/diagnostics.js.map +1 -1
  31. package/dist/split/features/formats.js +1 -1
  32. package/dist/split/features/list-keys.js +1 -1
  33. package/dist/split/features/native-commands.js +1 -1
  34. package/dist/split/features/recursion.js +1 -1
  35. package/dist/split/features/scopes.js +1 -1
  36. package/dist/split/features/scopes.js.map +1 -1
  37. package/dist/split/features/ssr.js +1 -1
  38. package/dist/split/features/ssr.js.map +1 -1
  39. package/dist/split/features/temporal.js +1 -1
  40. package/package.json +1 -1
  41. package/dist/split/chunks/chunk-65B2K2XD.js +0 -2
  42. package/dist/split/chunks/chunk-GU7IAIWW.js +0 -2
  43. package/dist/split/chunks/chunk-GU7IAIWW.js.map +0 -1
  44. package/dist/split/chunks/chunk-IWY3I2DG.js +0 -2
  45. package/dist/split/chunks/chunk-IWY3I2DG.js.map +0 -1
  46. package/dist/split/chunks/chunk-MZVF2WEY.js +0 -2
  47. package/dist/split/chunks/chunk-PTFWXISY.js +0 -2
  48. package/dist/split/chunks/chunk-SANIIUS2.js +0 -2
  49. package/dist/split/chunks/chunk-UE6JK2ZA.js +0 -2
  50. /package/dist/split/chunks/{chunk-JCQTA35B.js.map → chunk-AKTGQ6TC.js.map} +0 -0
  51. /package/dist/split/chunks/{chunk-UE6JK2ZA.js.map → chunk-E3XSBW55.js.map} +0 -0
  52. /package/dist/split/chunks/{chunk-FFXJVUTT.js.map → chunk-JJYM4JVZ.js.map} +0 -0
  53. /package/dist/split/chunks/{chunk-SJRJALR4.js.map → chunk-LPJFTEVT.js.map} +0 -0
  54. /package/dist/split/chunks/{chunk-FT7OW7CG.js.map → chunk-R6RHIIR4.js.map} +0 -0
@@ -1,2 +1,2 @@
1
- import{a as e,b as t,d as n}from"../chunks/chunk-65B2K2XD.js";import{b as o,p as l,q as r,r as i}from"../chunks/chunk-IWY3I2DG.js";import"../chunks/chunk-FFXJVUTT.js";import{e as a,f as s}from"../chunks/chunk-GU7IAIWW.js";import{a as u}from"../chunks/chunk-JCQTA35B.js";import"../chunks/chunk-MZVF2WEY.js";import"../chunks/chunk-PTFWXISY.js";import"../chunks/chunk-UE6JK2ZA.js";import{a as c,b as d,c as f,d as h,f as m,h as p,j as g,k as $}from"../chunks/chunk-SJRJALR4.js";var y=new WeakMap,w=new WeakMap,b="will not graft: the root state failed to initialize.",v=new WeakSet,k=new WeakMap,j=["$stream","$streams","$scan","$recursion","$watch","$listKeys","$renderedCallback","$updatedCallback","$behavior","$features"],P=["$commandTokens","$eventTokens","$on","$errorCallback"],O=e=>`[@wcstack/state] <${u.tagNames.state} mount="${e}">`,N=new Set(["$getAll","$setAll","$resolve","$postUpdate","$dependOn","$eq","$eqIndex"]),A=e=>e?.element?.isConnected?e:void 0;function C(e){let t=w.get(e);return void 0!==t&&!Array.isArray(t)&&t.isConnected}function M(e,t){let n=w.get(t);if(w.delete(t),Array.isArray(n))for(let t of n)D(t,e)}function x(e){let t=y.get(e.root);t?.get(e.t)===e&&t.delete(e.t)}function S(e,t){$(`${O(e.t)} ${t}`),x(e),e.Ze?.(),e.Ze=null}function W(e,t){let n=e.state?.[t];if("function"==typeof n)try{let t=n.call(e.chroot);null!==t&&"object"==typeof t&&"function"==typeof t.then&&t.then(void 0,$)}catch(e){$(e)}}function D(e,t){if(v.has(t))return S(e,"will not graft: its component is wired to its host.");let n=e.state,o=t.target,l=e.t.split("."),r=o;for(let e of l)r=r?.[e];if(void 0!==r&&!0!==t.hydrated)return S(e,`will not graft: the root state already has "${e.t}".`);let i=function(e,t){let n=e=>`${t}.${e}`;return new Proxy({},{get(t,o){if("symbol"==typeof o)return;if(36!==o.charCodeAt(0))return e.proxy[n(o)];let l=e.proxy[o];return"$eqPath"===o?(e,t)=>l(n(e),n(t)):N.has(o)&&"function"==typeof l?(e,...t)=>l(n(e),...t):l},set:(t,o,l)=>"symbol"!=typeof o&&(e.proxy[n(o)]=l,!0)})}(t,e.t);e.chroot=i;let a={},s={},u=new Set;for(let t=n;null!==t&&t!==Object.prototype;t=Object.getPrototypeOf(t))for(let n of Object.getOwnPropertyNames(t)){if(u.has(n)||"constructor"===n)continue;u.add(n);let{get:o,set:l,value:r}=Object.getOwnPropertyDescriptor(t,n);if(void 0!==o||void 0!==l)s[`${e.t}.${n}`]={get:o&&function(){return o.call(i)},set:l&&function(e){l.call(i,e)}};else{if("$"===n[0])continue;if("function"==typeof r){let t=r.bind(i);s[`${e.t}.${n}`]={get:()=>t}}else a[n]=r}}if(void 0===r){for(let e=1;e<l.length;e++){let n=t.o(l.slice(0,e).join("."));null==t.st(n,null)&&t.A(n,null,{})}t.A(t.o(e.t),null,a)}t.io(Object.defineProperties({},s));for(let e in s)t.qe(t.o(e),null);e.engine=t;let c=k.get(t);void 0===c&&k.set(t,c=[]),c.push(e),e.el.isConnected&&W(e,"$connectedCallback"),e.Ze?.(),e.Ze=null}function E(e,t){let n=w.get(e);if(Array.isArray(n))for(let e of n)S(e,b);w.set(e,t)}var T=new WeakMap;function q(e,t){let n=e.getAttribute("mount");if(null===n)return null;let o={el:e,root:t,t:n,state:null,engine:null,chroot:null,Ze:null},r=null;if(function(e){return""!==e&&e.split(".").every(e=>""!==e&&!/[*$#@\s]/.test(e))}(n))if(e.hasAttribute(u.bindAttributeName))r='injections (data-wcs="state.<key>: …") are not supported — read the root path in a root getter.';else{let e=y.get(t);void 0===e&&y.set(t,e=new Map);let l=e.get(n);if(void 0!==l&&l!==o&&(null===l.engine?l.el.isConnected:A(l.engine)))r=`will not graft: another volume already holds "${n}".`;else{if(null===l?.engine){let e=w.get(t);Array.isArray(e)&&w.set(t,e.filter(e=>e!==l)),l.Ze?.(),l.Ze=null}e.set(n,o)}}else r='has an invalid mount path: it must be a static path (no "*", "$", "#", "@").';return{Ao:!0,start(e){if(null!==r)return S(o,r);if(y.get(t).get(n)===o){for(let t of j)if(void 0!==e[t])return S(o,`${t} is not run in a volume — declare it on the root state.`);for(let t of P)void 0!==e[t]&&console.warn(`${O(n)} ${t} is not run in a volume (it belongs to the root).`);return o.state=e,new Promise(e=>{o.Ze=e;let n=A(l.get(t)),r=w.get(t);void 0!==n?D(o,n):C(t)?S(o,b):w.set(t,[...Array.isArray(r)?r:[],o])})}},Qe(){null!==o.engine&&W(o,"$connectedCallback")},Mn(){null!==o.engine&&W(o,"$disconnectedCallback")},reset(){g(`re-setting a volume is not supported: its data was copied into the root tree at "${n}" — write the paths under it on the root instead.`)}}}var I=new WeakMap,L=new WeakMap;function R(e,t){let n=e[t];if(void 0===n)return[];Array.isArray(n)||g(`${t} must be an array of state member names.`);let o=new Set;for(let e of n)("string"!=typeof e||""===e)&&g(`${t} entries must be non-empty strings.`),"$"===e[0]&&g(`${t} entry "${e}": internal ($) members are never exposed.`),o.has(e)&&g(`${t} entry "${e}" is duplicated.`),o.add(e);return n}function Z(e,t){let n=t.host;if(void 0===n||!n.hasAttribute("data-wc-definition"))return null;let o=document.createDocumentFragment();for(let e of[...t.childNodes])o.appendChild(e.cloneNode(!0));let l=t.mode;return{start(t){e.hasAttribute("bind-component")&&g("DCC: a <wcs-state bind-component> cannot define a component (use one mechanism)."),function(e,t,n,o){e.includes("-")||g(`DCC: <${e} data-wc-definition> is not a valid custom element name.`),void 0!==customElements.get(e)&&g(`DCC: <${e}> is already defined.`);let l=function(e){let t=new Map;for(let n=e;null!==n&&n!==Object.prototype;n=Object.getPrototypeOf(n))for(let e of Object.getOwnPropertyNames(n))"constructor"===e||"$"===e[0]||t.has(e)||t.set(e,Object.getOwnPropertyDescriptor(n,e));let n=e.$stream;if(null!==n&&"object"==typeof n)for(let e of Object.keys(n))t.has(e)||t.set(e,{value:void 0});return t}(o),r=R(o,"$bindables"),i=R(o,"$commands");for(let e of r){let t=l.get(e);void 0===t&&g(`$bindables entry "${e}" does not exist on the state.`),"function"==typeof t.value&&g(`$bindables entry "${e}" is a method: list it in $commands.`)}for(let e of i){let t=l.get(e);void 0===t&&g(`$commands entry "${e}" does not exist on the state.`),"function"!=typeof t.value&&g(`$commands entry "${e}" is not a method: list it in $bindables.`)}let a={tag:e,bindables:r},s=new WeakMap,c=new WeakMap,d=o=>{if(s.has(o))return s.get(o);if(o.hasAttribute("data-wc-definition"))return null;let l=o.attachShadow({mode:n});l.appendChild(t.cloneNode(!0));let i=l.querySelector(u.tagNames.state);return s.set(o,i),I.set(o,a),null===i&&r.length>0&&console.warn(`[@wcstack/state] DCC: <${e}> declares $bindables but its content has no <${u.tagNames.state}>: no change events.`),i},f=e=>d(e)?.engine??null,h=e=>Promise.resolve(d(e)?.initializePromise).then(()=>{let t=c.get(e);c.delete(e);let n=f(e);if(void 0!==t&&null!==n)for(let[e,o]of t)n.proxy[e]=o});class m extends HTMLElement{static{this.wcBindable=0===r.length&&0===i.length?null:{protocol:"wc-bindable",version:1,properties:r.map(t=>({name:t,event:`${e}:${t}-changed`,getter:e=>e.target[t]})),inputs:r.map(e=>({name:e})),...0===i.length?{}:{commands:i.map(e=>({name:e,async:!0}))}}}connectedCallback(){null!==d(this)&&h(this)}get stateElement(){return d(this)}}for(let[e,t]of l)"function"==typeof t.value?Object.defineProperty(m.prototype,e,{configurable:!0,value(...t){return h(this).then(()=>{let n=f(this);return null===n?void 0:n.proxy[e].apply(n.proxy,t)})}}):Object.defineProperty(m.prototype,e,{configurable:!0,get(){return f(this)?.proxy[e]},set(t){let n=f(this);if(null!==n)n.proxy[e]=t;else{let n=c.get(this);void 0===n&&c.set(this,n=[]),n.push([e,t])}}});customElements.define(e,m)}(n.localName,o,l,t)},Qe(){},Mn(){},reset(){g("re-setting a DCC definition is not supported: each instance loads its own state.")}}}var z=new WeakMap,F=new WeakSet,X=new WeakMap,H=new WeakMap,J="data-wcs-wired",V=new WeakMap,Q=new WeakMap,U=new WeakMap,B=0,G=new Set,K={},Y=["$watch","$stream","$renderedCallback"],_=e=>e.split(".",1)[0];function ee(e,t){let n=0;return e.split(".").map(e=>"*"===e?String(t[n++]):e).join(".")}function te(e,t){let n=e.c,o=_(e.name);if(!0!==document.documentElement?.hasAttribute("data-wcs-server"))n.removeAttribute(J);else if(!oe(n,o)){let e=n.getAttribute(J)?.split(" ")??[];e.includes(o)||n.setAttribute(J,[...e,o].join(" "))}V.get(n)?.he===o&&g(`<${n.localName}>.${o} has loaded its state: the wiring "${e.name}" added afterwards cannot reach it — bind the host's wiring before the component loads.`);let l=z.get(n);void 0===l&&z.set(n,l=[]),l.push(e),t&&F.add(e);let r=X.get(n);return void 0===r&&X.set(n,r=new Map),r.has(o)||r.set(o,n[o]),H.get(n)?.(),!1}function ne(e){return e.localName.includes("-")&&null!==e.querySelector(`:scope > ${u.tagNames.state}[bind-component]`)}function oe(e,t){let n=e.getAttribute(u.bindAttributeName);return null!==n&&new RegExp(`(^|;)\\s*${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}\\s*[.#:]`).test(n)}function le(t){let n=t.parentElement;for(;null!==n&&!ne(n);)n=n.parentElement;let o=l.get(n??t.getRootNode()),r=o?.element;(r?.isConnected?r.V:null===n)&&(null===n?e(t):i(o,t))}var re=new Set;function ie(e,t){re.has(e)||(re.add(e),console.warn(`[@wcstack/state] ${t}`))}var ae=(e,t)=>t in e.state;function se(e,t){let n=ue(e.state,t);return void 0!==n&&(void 0!==n.get||void 0!==n.set||"function"==typeof n.value)}function ue(e,t){for(let n=e;null!==n&&n!==Object.prototype;n=Object.getPrototypeOf(n)){let e=Object.getOwnPropertyDescriptor(n,t);if(void 0!==e)return e}}function ce(e,t,n){if(null!==n.getter)return;let o,l=e.host,r=l.engine,i=_(n.t),s=l.entries.find(e=>e.$===n.t);if(void 0===s&&null!==n.a)return function(e,t){let n,o=t.a;for(;null!==o&&void 0===(n=e.mt.get(o));)o=o.a;if(null===o||void 0===n)return;let l=e.P,r=e.host.engine,i=r.o(n.p.t+t.t.slice(o.t.length));if(null===i.getter)return;let s=n,u=()=>{let n=de(e,s.r);return 0===t.n?n:(r.resolve(ee(i.t,[...r.H(n),...l.H(a(l.d,t.n))]),null),r.rr)};t.getter=()=>{let e=u();return i.n>0&&null===e?void 0:r.v(i,e)},null!==i.se&&(t.se=n=>{let o=u();fe(e,s.e,()=>r.A(i,o,n)),e.dn=t}),t.n>0&&(t.F=l.Le++),e.mt.set(t,{e:s.e,p:i,r:s.r})}(e,n);if(0!==n.n)return;if(void 0!==s){if(s.$!==i?ae(l,i):se(l,i))return;o=s.ye}else{if(null===t||null!==n.a||"$"===i[0]||ae(l,i))return;s=t,o=r.o(`${t.ye.t}.${i}`)}let u=s.r,c=s;n.getter=()=>r.v(o,de(e,u)),n.se=t=>{fe(e,c,()=>r.A(o,u,t)),e.dn=n},e.mt.set(n,{e:s,p:o,r:u})}function de(e,t){return null!==t&&!t.w&&g(`The host row of <${e.host.el.localName}> was removed.`),t}function fe(e,t,n){let o=G.has(e.P);G.add(e.P),e.from=t;try{n()}finally{o||G.delete(e.P),e.from=null}}var he=(e,t)=>t||!G.has(e.m.P)||null!==e.m.from&&e.m.from!==e.e;function me(e,t){if(e.fn===t||null===e.host.engine)return;e.fn=t,B+=t?1:-1;let n=e.host.engine,o=U.get(n);void 0===o&&U.set(n,o=new Map);for(let n of e.pe){let e=o.get(n.e.ye);void 0===e&&o.set(n.e.ye,e=new Map);let l=e.get(n.e.r);void 0===l&&e.set(n.e.r,l=new Set),t?l.add(n):(l.delete(n),0===l.size&&e.delete(n.e.r))}let l=e.host.entries.find(e=>""===e.$);if(void 0!==l){null===e.ie&&function(e,t){let n=e.host,o=n.engine;e.ie=new Map;let l=new Set;for(let r=n.state;null!==r&&r!==Object.prototype;r=Object.getPrototypeOf(r))for(let i of Object.getOwnPropertyNames(r)){if(l.has(i))continue;l.add(i);let a=Object.getOwnPropertyDescriptor(r,i);if(void 0===a.get&&void 0===a.set||"$"===i[0]||i.includes("*"))continue;let s=_(i);if(i!==s&&!ae(n,s))continue;let u=o.o(`${t.ye.t}.${i}`);if(null===u.getter)$e(o,u,t.ye);else if(!pe.has(u.getter))continue;e.ie.set(u,e.P.o(i))}}(e,l);for(let o of e.ie.keys())t&&null===o.getter&&$e(n,o,l.ye),n.T.ke(n,o,l.r);e.ie.size>0&&n.ve(),t&&function(e,t){let n=e.host.engine;for(let[o,l]of e.ie){let r=n.v(o.a,t.r);if(null===r||"object"!=typeof r||!(o.s in r))continue;let i=e.host.el.localName;ie(`${i}|${o.t}`,`[wcs/mount-export-shadowed] <${i}>.${e.host.he}.${l.t} is exported at "${o.t}" but the tree already has that key, so readers outside the component get the tree value. Remove the tree key or rename the accessor. See docs/state-overlay-export-design.md X1.`)}}(e,l)}}var pe=new WeakSet;function ge(e,t,n,o){let l=U.get(e)?.get(t)?.get(n);if(void 0===l)return null;let r=null;for(let e of l)""!==e.e.$||!0!==e.m.ie?.has(o)||e.m===r||(null!==r&&g(`[wcs/mount-export-ambiguous] "${o.t}" is exported by two mounted components on the same instance: <${r.host.el.localName}> and <${e.m.host.el.localName}>. Mount only one of them there, or rename one accessor. See docs/state-overlay-export-design.md X4.`),r=e.m);return r}function $e(e,t,n){let o=t.a,l=t.s,r=()=>{let t=e.read(o,e.d);return null!==t&&"object"==typeof t&&l in t?t:null};t.getter=()=>{let o=r();if(null!==o)return o[l];let i=ge(e,n,e.d,t);return null===i?void 0:i.P.read(i.ie.get(t),null)},pe.add(t.getter),t.se=i=>{let a=null===r()?ge(e,n,e.d,t):null;if(null===a){let t=e.read(o,e.d);return void(null!==t&&"object"==typeof t&&(t[l]=i))}let s=a.ie.get(t);void 0===ue(a.host.state,s.t)?.set&&g(`Cannot write to "${s.t}" on mounted <${a.host.el.localName}>: the accessor has no setter. Add a setter or write to the underlying state paths instead.`),a.P.A(s,null,i)},t.n>0&&(t.F=e.Le++)}function ye(e,t,n,o,l,r){if(null!==t.getter)return e.T.ke(e,t,n),void e.ve();if("*"===t.s){if(null===n)return;if(n.E=n.l.R[n.y],e.T.Gt(n),null!==n.u)for(let t of n.u.values())e.X(t)}else e.xn(t,n);c.re?.(e,t,n,o,l,r),e.qe(t,n)}function we(e){let t=e.m.P;for(let[n,o]of e.m.mt)o.e===e.e&&(0===n.n?t.T.ke(t,n,null):t.nn(n.h[n.n],e=>t.T.ke(t,n,e)));t.ve()}function be(e,t,n,o,l,r,i){let s=n.t.slice(t.t.length);if(""===s)return we(e);let u,c=e.m,d=c.P;if(""===e.e.$){u=s.slice(1);let e=_(u);if("$"===e[0]||ae(c.host,e)||c.host.entries.some(e=>""!==e.$&&(u===e.$||u.startsWith(`${e.$}.`))))return}else u=e.e.$+s;let f=[];for(let e=t.n+1;e<=n.n;e++)f.push(a(o,e).y);d.resolve(ee(u,f),null);let h=d.rp,m=d.rr;h.n>0&&null===m||ye(d,h,m,l,r,i)}function ve(e,t,n,o,l,r,i){if(0===B)return;let s=G.has(e);G.add(e);try{let s=Q.get(e);!i&&void 0!==s&&s.fn&&!G.has(s.host.engine)&&function(e,t,n,o,l,r){let i,a=t;for(;null!==a&&void 0===(i=e.mt.get(a));)a=a.a;if(null===a||void 0===i)return;let s=e.host.engine;if(a===t&&e.dn===t)return void(e.dn=null);if(null!==i.r&&!i.r.w)return;let u=[...s.H(i.r),...e.P.H(n)];s.resolve(ee(i.p.t+t.t.slice(a.t.length),u),null);let c=s.rp,d=s.rr;c.n>0&&null===d||fe(e,i.e,()=>ye(s,c,d,o,l,r))}(s,t,n,o,l,r),i&&void 0!==s&&s.fn&&null!==s.ie&&function(e,t){let n=e.host.entries.find(e=>""===e.$),o=e.host.engine;for(let[l,r]of e.ie)r===t&&(o.T.ke(o,l,n.r),o.ve())}(s,t);let u=U.get(e);if(void 0===u)return;for(let[e,s]of u)if(t.ee(e)){let u=s.get(0===e.n?null:a(n,e.n));if(void 0!==u)for(let a of[...u])he(a,i)&&be(a,e,t,n,o,l,r)}else if(e.ee(t))for(let[e,o]of[...s])if(0===t.n||a(e,t.n)===n)for(let e of[...o])he(e,i)&&we(e)}finally{s||G.delete(e)}}async function ke(e,o,l,i,a){if(i===K)return;let d=p(i);d&&await d;let f=V.get(o);f.engine&&i.$recursion&&g(`[wcs/mount-dollar-declaration] <${o.localName}>: $recursion is not run in a mounted component — declare it on the root state.`);let h=f.I;if(null!==h&&h.el!==e&&h.el.isConnected&&g(`<${o.localName}> already has a connected <${u.tagNames.state} bind-component="${f.he}">.`),null!==h&&me(h,!1),null!==h&&function(e,t,n){let o=e.P,l=`[${u.bindAttributeName}]`,r=e=>1===e.nodeType&&(e.matches(l)||null!==e.querySelector(l)),i=new Set,a=e=>{for(;null!==e&&e.parentNode!==n;)e=e.parentNode;null!==e&&i.add(e)},s=(e,t)=>{for(let n=e.jn(t);null!==n&&n!==t;n=n.nextSibling)a(n);a(t)};for(let e of o.ue.values())for(let t of e)if(null===t.D)a(t.c);else for(let e of t.D.q)s(t.D,e.S);for(let e of o.oe.values())for(let t of[e.N,...e.W??[]])null!==t&&s(t,t.S);for(let t of e.L)t.parentNode===n&&r(t)&&i.add(t);if(0===i.size)return!1;let c=[...n.childNodes].filter(n=>n!==t&&!i.has(n)&&!e.L.includes(n)&&(r(n)||o._t&&n.textContent.includes("{{")));return c.length>0&&console.warn(`[@wcstack/state] <${e.host.el.localName}>: ${c.length} node(s) added beside the content taken over are not bound.`),!0}(h,e,l))return async function(e,t,n,o){e.el.engine=null,e.el=t,e.P.element=t,t.engine=e.P,e.L=e.L.filter(e=>e.parentNode===n),t.isConnected?await je(e,o):o()}(h,e,l,a);let m=new s(i,new n);m.element=e,e.engine=m,function(e,t){let n=new Map;for(let e=t;null!==e&&e!==Object.prototype;e=Object.getPrototypeOf(e))for(let t of Object.getOwnPropertyNames(e)){let o=Object.getOwnPropertyDescriptor(e,t).value;"function"==typeof o&&"constructor"!==t&&n.set(o,null)}let o=(t,n,o)=>Reflect.apply(t,e.proxy,o);e.V=e=>{let t=n.get(e);return null===t&&n.set(e,t=new Proxy(e,{apply:o})),t}}(m,i);let y={el:e,L:[],P:m,host:f,pe:[],mt:new Map,fn:!1,dn:null,from:null,ie:null};for(let e of f.entries)y.pe.push({m:y,e});f.I=y,Q.set(m,y);let w=null===f.engine;w?c.element?.(m,"mounting"):(function(e){let t=e.P,n=e.host,o=n.engine,l=n.el.localName,r=n.entries.find(e=>""===e.$)??null;for(let e of n.entries){if(""===e.$)continue;let t=_(e.$);ae(n,t)&&(e.$!==t||se(n,t))&&ie(`${l}.${e.$}`,`[wcs/mount-own-key-shadow] <${l}>: "${n.he}.${e.$}" is hidden by the component's own "${t}"; rename one side.`)}if(null!==r){let e=o.v(r.ye,r.r);if(null!==e&&"object"==typeof e)for(let t of Object.keys(n.state))"$"!==t[0]&&t in e&&!n.entries.some(e=>e.$===t)&&ie(`${l}.${t}`,`[wcs/mount-own-key-shadow] <${l}>: its own "${t}" hides "${r.ye.t}.${t}" — remove the default to read the tree, or rename it to keep it private.`)}let i=Y.filter(e=>void 0!==n.state[e]);i.length>0&&ie(`${l}.${n.he}$`,`[wcs/mount-dollar-declaration] <${l}>: ${i.join(", ")} ${1===i.length?"is":"are"} not run in a mounted component — declare it on the root state.`);let a=t.rt;t.rt=n=>{ce(e,r,n),a.call(t,n)};let s=[...t.O.all()];for(let t of s)ce(e,r,t);for(let e of s)a.call(t,e);let u=t.Nt;t.Nt=()=>{(function(e){for(;void 0!==e;e=Q.get(e.host.engine))if(e.host.entries.some(e=>null!==e.r&&!e.r.w))return!1;return!0})(e)&&u.call(t)}}(y),me(y,!0),v.add(m),M(m,l));let b=c.wr?.(l,m);r(m,l),b?.(),y.L=[...l.childNodes],t(),w&&m.Pn(),Object.defineProperty(o,f.he,{configurable:!0,enumerable:!0,get:()=>m.proxy});let k=o.$stateReadyCallback;if("function"==typeof k)try{let e=k.call(o,f.he);null!==e&&"object"==typeof e&&"function"==typeof e.then&&e.then(void 0,$)}catch(e){$(e)}a(),e.isConnected?(await m.$e("$connectedCallback"),w&&e.isConnected&&c.element?.(m,"connected")):me(y,!1)}async function je(e,t){me(e,!0);for(let t of e.pe)we(t);t?.(),!e.broken&&(await e.P.$e("$connectedCallback"),null===e.host.engine&&e.el.isConnected&&c.element?.(e.P,"connected"))}function Pe(e,t){let n=e.getAttribute("bind-component");if(null===n)return function(e,t){return e.connectedCallbackPromise.catch(()=>{let n=l.get(t)?.element;if(!(e.V||void 0!==n&&n!==e&&n.isConnected)){E(t,e);for(let e of T.get(t)??[])e()}}),null}(e,t);let r=e.parentNode,i=r instanceof ShadowRoot,a=i?r.host:r instanceof Element?r:null,s=()=>{let t=null===a?void 0:V.get(a)?.I;return t?.el===e?t:null},d=t=>{throw i&&E(r,e),t},f=!1;return{St:()=>(null!==a&&a.localName.includes("-")?async function(e,t,n,l){let r=n.localName;(["state","src","json"].some(t=>e.hasAttribute(t))||null!==e.querySelector('script[type="module"]'))&&g(`<${u.tagNames.state} bind-component> takes its state from <${r}>.${t}: it cannot also load one (state / src / json / an inline script).`),await o(n).whenDefined(r);let i=V.get(n);if(void 0!==i)return i.he!==t&&g(`<${r}> already has a <${u.tagNames.state} bind-component="${i.he}">.`),i.state;let a=()=>(z.get(n)??[]).filter(e=>_(e.name)===t),s=oe(n,t);if(s&&0===a().length&&le(n),0===a().length&&(s||((e,t)=>!0===e.getAttribute(J)?.split(" ").includes(t))(n,t))){let e;await new Promise((o,l)=>{H.set(n,o),e=function(e,t){C(e)&&t();let n=T.get(e)??new Set;return T.set(e,n.add(t)),()=>n.delete(t)}(n.getRootNode(),()=>l(new Error(`[@wcstack/state] <${r}>.${t} will not mount: the root state failed to initialize.`)))}).finally(()=>e())}H.delete(n);let c=a(),d=X.get(n),f=void 0!==d&&d.has(t)?d.get(t):n[t];if((null===f||"object"!=typeof f)&&g(`"bind-component": <${r}>.${t} must be an object (the component's state), got ${null===f?"null":typeof f}.`),0===c.length&&l){let e=new Error(`[@wcstack/state] A plain (unwired) Light DOM "bind-component" is not supported. Attach a shadow root to <${r}>, or mount it from the host (${u.bindAttributeName}="${t}: path").`);return queueMicrotask(()=>{throw e}),K}let h=[];for(let e of c){let o=e.name===t?"":e.name.slice(t.length+1);h.some(e=>e.$===o)&&g(`<${r}> maps "${e.name}" twice.`);let l=o.split(".").indexOf("*");if(l>=0){let n=o.split(".").slice(0,l).join(".");g(`<${r}> maps "${e.name}": the component-side path of a mount cannot contain "*"${""===n?"":` — map the list itself ("${t}.${n}: <the host's list>")`}.`)}h.push({$:o,ye:e.o,r:e.r,ro:F.has(e)}),e.engine.Tn(e),""!==o&&delete n[e.name]}let m=0===c.length?function(e){let t=Object.getOwnPropertyDescriptors(e),n=Object.isFrozen(e);for(let e of Object.values(t))"value"in e&&!1===e.writable&&(e.writable=!0,n=!0);return n?Object.create(Object.getPrototypeOf(e),t):e}(f):f;return V.set(n,i={el:n,he:t,state:m,engine:0===c.length?null:c[0].engine,entries:h,I:null}),m}(e,n,a,!i):Promise.reject(new Error(`[@wcstack/state] "bind-component" requires <${u.tagNames.state}> to be a direct child of a custom element.`))).catch(d),async start(t,n){await ke(e,a,i?r:a,t,()=>{f=!0,n()}).catch(e=>{let t=s();return!f&&null!==t&&(t.broken=!0),d(e)})},Qe(){let t=s();null!==t?je(t).catch($):f?Promise.resolve().then(()=>{if(e.parentNode===r)return ke(e,a,i?r:a,V.get(a).state,()=>{})}).catch($):H.has(a)&&le(a)},Mn(){let e=s();null!==e&&(me(e,!1),!e.broken&&(e.P.$e("$disconnectedCallback"),null===e.host.engine&&c.element?.(e.P,"disconnected")))},reset(){g(`re-setting a component's state is not supported: write <${a?.localName}>.${n} instead.`)}}}var Oe={name:"scopes",hooks:c,install(){c.dt=f(m,c.dt,q),c.dt=f(m,c.dt,Z),c.dt=f(m,c.dt,Pe),c.dr=te,c.Po=ne,c.element=d(c.element,(e,t)=>{if("mounting"!==t)return function(e,t){let n=k.get(e)??[];if("disconnected"===t)for(let e of n)x(e);else if("reset"!==t&&A(l.get(e.root))===e){M(e,e.root);for(let e of n)y.get(e.root).has(e.t)||y.get(e.root).set(e.t,e)}}(e,t);let n=e.element.getRootNode();M(e,n),function(e,t){let n=t.host,o=void 0===n?void 0:I.get(n);void 0!==o&&L.set(e,{host:n,def:o})}(e,n)}),c.re=d(c.re,(e,t,n,o,l,r)=>{(function(e,t,n,o){let l=L.get(e);if(void 0===l)return;let r=t.t.split(".",1)[0];if(!l.def.bindables.includes(r))return;let i=o&&t.t===r;l.host.dispatchEvent(new CustomEvent(`${l.def.tag}:${r}-changed`,{detail:i?n:void 0,bubbles:!0}))})(e,t,l,r),ve(e,t,n,o,l,r,!1)}),c.Se=d(c.Se,(e,t,n)=>ve(e,t,n,void 0,void 0,!1,!0)),c.Ie=f(h,c.Ie,(e,t,n,o,l)=>(function(e,t){let n=k.get(e);if(void 0!==n&&0===t.n)for(let{t:e}of n)e.startsWith(`${t.t}.`)&&g(`writing "${t.t}" would replace the volume grafted at "${e}"; write the paths under it instead.`)}(e,t),function(e,t,n){let o,l=Q.get(e);if(void 0===l)return!1;for(let e=t;null!==e&&void 0===o;e=e.a)o=l.mt.get(e);if(void 0===o)return!1;let r=o.e;return!((null===r.r||r.r.w)&&!r.ro)&&(!!n||(de(l,r.r),void g(`[wcs/mount-readonly] <${l.host.el.localName}> cannot write "${t.t}": it is mounted read-only ("${l.host.he}${""===r.$?"":`.${r.$}`}#ro: ${r.ye.t}"). Write it on the host, or drop #ro from the mount.`)))}(e,t,l)));let e=c.fe;c.fe=(t,n)=>{let o=k.get(t);void 0!==o&&o.length>0&&g(`re-setting a root state with grafted volumes (${o.map(e=>e.t).join(", ")}) is not supported: their data is part of the tree.`),function(e){let t=U.get(e);if(void 0===t)return!1;for(let e of t.values())if(e.size>0)return!0;return!1}(t)&&g("re-setting a root state with mounted components is not supported: they read its data."),e?.(t,n)}}},Ne=Oe;export{Ne as default,Oe as scopes};
1
+ import{a as e,b as t,d as n}from"../chunks/chunk-REB4RMZ3.js";import{a as o,o as l,p as r,q as i}from"../chunks/chunk-CYMUD7KV.js";import"../chunks/chunk-JJYM4JVZ.js";import{e as a,f as s}from"../chunks/chunk-IYPAK7PO.js";import{a as u}from"../chunks/chunk-AKTGQ6TC.js";import"../chunks/chunk-NAV7FBTI.js";import"../chunks/chunk-KBQKAD3I.js";import"../chunks/chunk-E3XSBW55.js";import{a as c,b as f,c as d,d as h,f as p,h as m,j as g,k as $}from"../chunks/chunk-LPJFTEVT.js";var b=new WeakMap,v=new WeakMap,w="will not graft: the root state failed to initialize.",y=new WeakSet,k=new WeakMap,N=["$stream","$streams","$scan","$recursion","$watch","$listKeys","$renderedCallback","$updatedCallback","$behavior","$features"],j=["$commandTokens","$eventTokens","$on","$errorCallback"],O=e=>`[@wcstack/state] <${u.tagNames.state} mount="${e}">`,A=new Set(["$getAll","$setAll","$resolve","$postUpdate","$dependOn","$eq","$eqIndex"]),C=e=>e?.element?.isConnected?e:void 0;function P(e){let t=v.get(e);return void 0!==t&&!Array.isArray(t)&&t.isConnected}function x(e,t){let n=v.get(t);if(v.delete(t),Array.isArray(n))for(let t of n)E(t,e)}function M(e){let t=b.get(e.root);t?.get(e.t)===e&&t.delete(e.t)}function T(e,t){$(`${O(e.t)} ${t}`),M(e),e.Ze?.(),e.Ze=null}function S(e,t){let n=e.state?.[t];if("function"==typeof n)try{let t=n.call(e.chroot);null!==t&&"object"==typeof t&&"function"==typeof t.then&&t.then(void 0,$)}catch(e){$(e)}}function E(e,t){if(y.has(t))return T(e,"will not graft: its component is wired to its host.");let n=e.state,o=t.target,l=e.t.split("."),r=o;for(let e of l)r=r?.[e];if(void 0!==r&&!0!==t.hydrated)return T(e,`will not graft: the root state already has "${e.t}".`);let i=function(e,t){let n=e=>`${t}.${e}`;return new Proxy({},{get(t,o){if("symbol"==typeof o)return;if(36!==o.charCodeAt(0))return e.proxy[n(o)];let l=e.proxy[o];return"$eqPath"===o?(e,t)=>l(n(e),n(t)):A.has(o)&&"function"==typeof l?(e,...t)=>l(n(e),...t):l},set:(t,o,l)=>"symbol"!=typeof o&&(e.proxy[n(o)]=l,!0)})}(t,e.t);e.chroot=i;let a={},s={},u=new Set;for(let t=n;null!==t&&t!==Object.prototype;t=Object.getPrototypeOf(t))for(let n of Object.getOwnPropertyNames(t)){if(u.has(n)||"constructor"===n)continue;u.add(n);let{get:o,set:l,value:r}=Object.getOwnPropertyDescriptor(t,n);if(void 0!==o||void 0!==l)s[`${e.t}.${n}`]={get:o&&function(){return o.call(i)},set:l&&function(e){l.call(i,e)}};else{if("$"===n[0])continue;if("function"==typeof r){let t=r.bind(i);s[`${e.t}.${n}`]={get:()=>t}}else a[n]=r}}if(void 0===r){for(let e=1;e<l.length;e++){let n=t.o(l.slice(0,e).join("."));null==t.at(n,null)&&t.F(n,null,{})}t.F(t.o(e.t),null,a)}t.io(Object.defineProperties({},s));for(let e in s)t.qe(t.o(e),null);e.engine=t;let c=k.get(t);void 0===c&&k.set(t,c=[]),c.push(e),e.el.isConnected&&S(e,"$connectedCallback"),e.Ze?.(),e.Ze=null}function W(e,t){let n=v.get(e);if(Array.isArray(n))for(let e of n)T(e,w);v.set(e,t)}var D=new WeakMap;function q(e,t){let n=e.getAttribute("mount");if(null===n)return null;let o={el:e,root:t,t:n,state:null,engine:null,chroot:null,Ze:null},r=null;if(function(e){return""!==e&&e.split(".").every(e=>""!==e&&!/[*$#@\s]/.test(e))}(n))if(e.hasAttribute(u.bindAttributeName))r='injections (data-wcs="state.<key>: …") are not supported — read the root path in a root getter.';else{let e=b.get(t);void 0===e&&b.set(t,e=new Map);let l=e.get(n);if(void 0!==l&&l!==o&&(null===l.engine?l.el.isConnected:C(l.engine)))r=`will not graft: another volume already holds "${n}".`;else{if(null===l?.engine){let e=v.get(t);Array.isArray(e)&&v.set(t,e.filter(e=>e!==l)),l.Ze?.(),l.Ze=null}e.set(n,o)}}else r='has an invalid mount path: it must be a static path (no "*", "$", "#", "@").';return{Fo:!0,start(e){if(null!==r)return T(o,r);if(b.get(t).get(n)===o){for(let t of N)if(void 0!==e[t])return T(o,`${t} is not run in a volume — declare it on the root state.`);for(let t of j)void 0!==e[t]&&console.warn(`${O(n)} ${t} is not run in a volume (it belongs to the root).`);return o.state=e,new Promise(e=>{o.Ze=e;let n=C(l.get(t)),r=v.get(t);void 0!==n?E(o,n):P(t)?T(o,w):v.set(t,[...Array.isArray(r)?r:[],o])})}},Qe(){null!==o.engine&&S(o,"$connectedCallback")},Mn(){null!==o.engine&&S(o,"$disconnectedCallback")},reset(){g(`re-setting a volume is not supported: its data was copied into the root tree at "${n}" — write the paths under it on the root instead.`)}}}var R=new WeakMap,B=new WeakMap;function F(e,t){let n=e[t];if(void 0===n)return[];Array.isArray(n)||g(`${t} must be an array of state member names.`);let o=new Set;for(let e of n)("string"!=typeof e||""===e)&&g(`${t} entries must be non-empty strings.`),"$"===e[0]&&g(`${t} entry "${e}": internal ($) members are never exposed.`),o.has(e)&&g(`${t} entry "${e}" is duplicated.`),o.add(e);return n}function K(e,t){let n=t.host;if(void 0===n||!n.hasAttribute("data-wc-definition"))return null;let o=document.createDocumentFragment();for(let e of[...t.childNodes])o.appendChild(e.cloneNode(!0));let l=t.mode;return{start(t){e.hasAttribute("bind-component")&&g("DCC: a <wcs-state bind-component> cannot define a component (use one mechanism)."),function(e,t,n,o){e.includes("-")||g(`DCC: <${e} data-wc-definition> is not a valid custom element name.`),void 0!==customElements.get(e)&&g(`DCC: <${e}> is already defined.`);let l=function(e){let t=new Map;for(let n=e;null!==n&&n!==Object.prototype;n=Object.getPrototypeOf(n))for(let e of Object.getOwnPropertyNames(n))"constructor"===e||"$"===e[0]||t.has(e)||t.set(e,Object.getOwnPropertyDescriptor(n,e));let n=e.$stream;if(null!==n&&"object"==typeof n)for(let e of Object.keys(n))t.has(e)||t.set(e,{value:void 0});return t}(o),r=F(o,"$bindables"),i=F(o,"$commands");for(let e of r){let t=l.get(e);void 0===t&&g(`$bindables entry "${e}" does not exist on the state.`),"function"==typeof t.value&&g(`$bindables entry "${e}" is a method: list it in $commands.`)}for(let e of i){let t=l.get(e);void 0===t&&g(`$commands entry "${e}" does not exist on the state.`),"function"!=typeof t.value&&g(`$commands entry "${e}" is not a method: list it in $bindables.`)}let a={tag:e,bindables:r},s=new WeakMap,c=new WeakMap,f=o=>{if(s.has(o))return s.get(o);if(o.hasAttribute("data-wc-definition"))return null;let l=o.attachShadow({mode:n});l.appendChild(t.cloneNode(!0));let i=l.querySelector(u.tagNames.state);return s.set(o,i),R.set(o,a),null===i&&r.length>0&&console.warn(`[@wcstack/state] DCC: <${e}> declares $bindables but its content has no <${u.tagNames.state}>: no change events.`),i},d=e=>f(e)?.engine??null,h=e=>Promise.resolve(f(e)?.initializePromise).then(()=>{let t=c.get(e);c.delete(e);let n=d(e);if(void 0!==t&&null!==n)for(let[e,o]of t)n.proxy[e]=o});class p extends HTMLElement{static{this.wcBindable=0===r.length&&0===i.length?null:{protocol:"wc-bindable",version:1,properties:r.map(t=>({name:t,event:`${e}:${t}-changed`,getter:e=>e.target[t]})),inputs:r.map(e=>({name:e})),...0===i.length?{}:{commands:i.map(e=>({name:e,async:!0}))}}}connectedCallback(){null!==f(this)&&h(this)}get stateElement(){return f(this)}}for(let[e,t]of l)"function"==typeof t.value?Object.defineProperty(p.prototype,e,{configurable:!0,value(...t){return h(this).then(()=>{let n=d(this);return null===n?void 0:n.proxy[e].apply(n.proxy,t)})}}):Object.defineProperty(p.prototype,e,{configurable:!0,get(){return d(this)?.proxy[e]},set(t){let n=d(this);if(null!==n)n.proxy[e]=t;else{let n=c.get(this);void 0===n&&c.set(this,n=[]),n.push([e,t])}}});customElements.define(e,p)}(n.localName,o,l,t)},Qe(){},Mn(){},reset(){g("re-setting a DCC definition is not supported: each instance loads its own state.")}}}var Z=new WeakMap,I=new WeakSet,z=new WeakMap,J=new WeakMap,L="data-wcs-wired",Q=new WeakMap,U=new WeakMap,V=new WeakMap,H=0,X=new Set,Y={},G=["$watch","$stream","$renderedCallback"],_=e=>e.split(".",1)[0];function ee(e,t){let n=0;return e.split(".").map(e=>"*"===e?String(t[n++]):e).join(".")}function te(e,t){let n=e.a,o=_(e.name);if(!0!==document.documentElement?.hasAttribute("data-wcs-server"))n.removeAttribute(L);else if(!oe(n,o)){let e=n.getAttribute(L)?.split(" ")??[];e.includes(o)||n.setAttribute(L,[...e,o].join(" "))}Q.get(n)?.be===o&&g(`<${n.localName}>.${o} has loaded its state: the wiring "${e.name}" added afterwards cannot reach it — bind the host's wiring before the component loads.`);let l=Z.get(n);void 0===l&&Z.set(n,l=[]),l.push(e),t&&I.add(e);let r=z.get(n);return void 0===r&&z.set(n,r=new Map),r.has(o)||r.set(o,n[o]),J.get(n)?.(),!1}function ne(e){return e.localName.includes("-")&&null!==e.querySelector(`:scope > ${u.tagNames.state}[bind-component]`)}function oe(e,t){let n=e.getAttribute(u.bindAttributeName);return null!==n&&new RegExp(`(^|;)\\s*${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}\\s*[.#:]`).test(n)}function le(t){let n=t.parentElement;for(;null!==n&&!ne(n);)n=n.parentElement;let o=l.get(n??t.getRootNode()),r=o?.element;(r?.isConnected?r.U:null===n)&&(null===n?e(t):i(o,t))}var re=new Set;function ie(e,t){re.has(e)||(re.add(e),console.warn(`[@wcstack/state] ${t}`))}var ae=(e,t)=>t in e.state;function se(e,t){let n=ue(e.state,t);return void 0!==n&&(void 0!==n.get||void 0!==n.set||"function"==typeof n.value)}function ue(e,t){for(let n=e;null!==n&&n!==Object.prototype;n=Object.getPrototypeOf(n)){let e=Object.getOwnPropertyDescriptor(n,t);if(void 0!==e)return e}}function ce(e,t,n){if(null!==n.getter)return;let o,l=e.host,r=l.engine,i=_(n.t),s=l.entries.find(e=>e.N===n.t);if(void 0===s&&null!==n.c)return function(e,t){let n,o=t.c;for(;null!==o&&void 0===(n=e.ht.get(o));)o=o.c;if(null===o||void 0===n)return;let l=e.T,r=e.host.engine,i=r.o(n.p.t+t.t.slice(o.t.length));if(null===i.getter)return;let s=n,u=()=>{let n=fe(e,s.r);return 0===t.n?n:(r.resolve(ee(i.t,[...r.K(n),...l.K(a(l.f,t.n))]),null),r.rr)};t.getter=()=>{let e=u();return i.n>0&&null===e?void 0:r.v(i,e)},null!==i.ce&&(t.ce=n=>{let o=u();de(e,s.e,()=>r.F(i,o,n)),e.fn=t}),t.n>0&&(t.O=l.Le++),e.ht.set(t,{e:s.e,p:i,r:s.r})}(e,n);if(0!==n.n)return;if(void 0!==s){if(s.N!==i?ae(l,i):se(l,i))return;o=s.ve}else{if(null===t||null!==n.c||"$"===i[0]||ae(l,i))return;s=t,o=r.o(`${t.ve.t}.${i}`)}let u=s.r,c=s;n.getter=()=>r.v(o,fe(e,u)),n.ce=t=>{de(e,c,()=>r.F(o,u,t)),e.fn=n},e.ht.set(n,{e:s,p:o,r:u})}function fe(e,t){return null!==t&&!t.w&&g(`The host row of <${e.host.el.localName}> was removed.`),t}function de(e,t,n){let o=X.has(e.T);X.add(e.T),e.from=t;try{n()}finally{o||X.delete(e.T),e.from=null}}var he=(e,t)=>t||!X.has(e.m.T)||null!==e.m.from&&e.m.from!==e.e;function pe(e,t){if(e.pn===t||null===e.host.engine)return;e.pn=t,H+=t?1:-1;let n=e.host.engine,o=V.get(n);void 0===o&&V.set(n,o=new Map);for(let n of e.pe){let e=o.get(n.e.ve);void 0===e&&o.set(n.e.ve,e=new Map);let l=e.get(n.e.r);void 0===l&&e.set(n.e.r,l=new Set),t?l.add(n):(l.delete(n),0===l.size&&e.delete(n.e.r))}let l=e.host.entries.find(e=>""===e.N);if(void 0!==l){null===e.ae&&function(e,t){let n=e.host,o=n.engine;e.ae=new Map;let l=new Set;for(let r=n.state;null!==r&&r!==Object.prototype;r=Object.getPrototypeOf(r))for(let i of Object.getOwnPropertyNames(r)){if(l.has(i))continue;l.add(i);let a=Object.getOwnPropertyDescriptor(r,i);if(void 0===a.get&&void 0===a.set||"$"===i[0]||i.includes("*"))continue;let s=_(i);if(i!==s&&!ae(n,s))continue;let u=o.o(`${t.ve.t}.${i}`);if(null===u.getter)$e(o,u,t.ve);else if(!me.has(u.getter))continue;e.ae.set(u,e.T.o(i))}}(e,l);for(let o of e.ae.keys())t&&null===o.getter&&$e(n,o,l.ve),n.A.Ee(n,o,l.r);e.ae.size>0&&n.$e(),t&&function(e,t){let n=e.host.engine;for(let[o,l]of e.ae){let r=n.v(o.c,t.r);if(null===r||"object"!=typeof r||!(o.s in r))continue;let i=e.host.el.localName;ie(`${i}|${o.t}`,`[wcs/mount-export-shadowed] <${i}>.${e.host.be}.${l.t} is exported at "${o.t}" but the tree already has that key, so readers outside the component get the tree value. Remove the tree key or rename the accessor. See docs/state-overlay-export-design.md X1.`)}}(e,l)}}var me=new WeakSet;function ge(e,t,n,o){let l=V.get(e)?.get(t)?.get(n);if(void 0===l)return null;let r=null;for(let e of l)""!==e.e.N||!0!==e.m.ae?.has(o)||e.m===r||(null!==r&&g(`[wcs/mount-export-ambiguous] "${o.t}" is exported by two mounted components on the same instance: <${r.host.el.localName}> and <${e.m.host.el.localName}>. Mount only one of them there, or rename one accessor. See docs/state-overlay-export-design.md X4.`),r=e.m);return r}function $e(e,t,n){let o=t.c,l=t.s,r=()=>{let t=e.read(o,e.f);return null!==t&&"object"==typeof t&&l in t?t:null};t.getter=()=>{let o=r();if(null!==o)return o[l];let i=ge(e,n,e.f,t);return null===i?void 0:i.T.read(i.ae.get(t),null)},me.add(t.getter),t.ce=i=>{let a=null===r()?ge(e,n,e.f,t):null;if(null===a){let t=e.read(o,e.f);return void(null!==t&&"object"==typeof t&&(t[l]=i))}let s=a.ae.get(t);void 0===ue(a.host.state,s.t)?.set&&g(`Cannot write to "${s.t}" on mounted <${a.host.el.localName}>: the accessor has no setter. Add a setter or write to the underlying state paths instead.`),a.T.F(s,null,i)},t.n>0&&(t.O=e.Le++)}function be(e,t,n,o,l,r){if(null!==t.getter)return e.A.Ee(e,t,n),void e.$e();if("*"===t.s){if(null===n)return;if(n.x=n.l.R[n.y],e.A.Jt(n),null!==n.u)for(let t of n.u.values())e.J(t)}else e.Rn(t,n);c.se?.(e,t,n,o,l,r),e.qe(t,n)}function ve(e){let t=e.m.T;for(let[n,o]of e.m.ht)o.e===e.e&&(0===n.n?t.A.Ee(t,n,null):t.on(n.h[n.n],e=>t.A.Ee(t,n,e)));t.$e()}function we(e,t,n,o,l,r,i){let s=n.t.slice(t.t.length);if(""===s)return ve(e);let u,c=e.m,f=c.T;if(""===e.e.N){u=s.slice(1);let e=_(u);if("$"===e[0]||ae(c.host,e)||c.host.entries.some(e=>""!==e.N&&(u===e.N||u.startsWith(`${e.N}.`))))return}else u=e.e.N+s;let d=[];for(let e=t.n+1;e<=n.n;e++)d.push(a(o,e).y);f.resolve(ee(u,d),null);let h=f.rp,p=f.rr;h.n>0&&null===p||be(f,h,p,l,r,i)}function ye(e,t,n,o,l,r,i){if(0===H)return;let s=X.has(e);X.add(e);try{let s=U.get(e);!i&&void 0!==s&&s.pn&&!X.has(s.host.engine)&&function(e,t,n,o,l,r){let i,a=t;for(;null!==a&&void 0===(i=e.ht.get(a));)a=a.c;if(null===a||void 0===i)return;let s=e.host.engine;if(a===t&&e.fn===t)return void(e.fn=null);if(null!==i.r&&!i.r.w)return;let u=[...s.K(i.r),...e.T.K(n)];s.resolve(ee(i.p.t+t.t.slice(a.t.length),u),null);let c=s.rp,f=s.rr;c.n>0&&null===f||de(e,i.e,()=>be(s,c,f,o,l,r))}(s,t,n,o,l,r),i&&void 0!==s&&s.pn&&null!==s.ae&&function(e,t){let n=e.host.entries.find(e=>""===e.N),o=e.host.engine;for(let[l,r]of e.ae)r===t&&(o.A.Ee(o,l,n.r),o.$e())}(s,t);let u=V.get(e);if(void 0===u)return;for(let[e,s]of u)if(t.te(e)){let u=s.get(0===e.n?null:a(n,e.n));if(void 0!==u)for(let a of[...u])he(a,i)&&we(a,e,t,n,o,l,r)}else if(e.te(t))for(let[e,o]of[...s])if(0===t.n||a(e,t.n)===n)for(let e of[...o])he(e,i)&&ve(e)}finally{s||X.delete(e)}}async function ke(e,o,l,i,a){if(i===Y)return;let f=m(i);f&&await f;let d=Q.get(o);d.engine&&i.$recursion&&g(`[wcs/mount-dollar-declaration] <${o.localName}>: $recursion is not run in a mounted component — declare it on the root state.`);let h=d.D;if(null!==h&&h.el!==e&&h.el.isConnected&&g(`<${o.localName}> already has a connected <${u.tagNames.state} bind-component="${d.be}">.`),null!==h&&pe(h,!1),null!==h&&function(e,t,n){let o=e.T,l=`[${u.bindAttributeName}]`,r=e=>1===e.nodeType&&(e.matches(l)||null!==e.querySelector(l)),i=new Set,a=e=>{for(;null!==e&&e.parentNode!==n;)e=e.parentNode;null!==e&&i.add(e)},s=(e,t)=>{for(let n=e.jn(t);null!==n&&n!==t;n=n.nextSibling)a(n);a(t)};for(let e of o.ie.values())for(let t of e)if(null===t.I)a(t.a);else for(let e of t.I.W)s(t.I,e.$);for(let e of o.H.values())for(let t of[e.S,...e.j??[]])null!==t&&s(t,t.$);for(let t of e.B)t.parentNode===n&&r(t)&&i.add(t);if(0===i.size)return!1;let c=[...n.childNodes].filter(n=>n!==t&&!i.has(n)&&!e.B.includes(n)&&(r(n)||o.Ht&&n.textContent.includes("{{")));return c.length>0&&console.warn(`[@wcstack/state] <${e.host.el.localName}>: ${c.length} node(s) added beside the content taken over are not bound.`),!0}(h,e,l))return async function(e,t,n,o){e.el.engine=null,e.el=t,e.T.element=t,t.engine=e.T,e.B=e.B.filter(e=>e.parentNode===n),t.isConnected?await Ne(e,o):o()}(h,e,l,a);let p=new s(i,new n);p.element=e,e.engine=p,function(e,t){let n=new Map;for(let e=t;null!==e&&e!==Object.prototype;e=Object.getPrototypeOf(e))for(let t of Object.getOwnPropertyNames(e)){let o=Object.getOwnPropertyDescriptor(e,t).value;"function"==typeof o&&"constructor"!==t&&n.set(o,null)}let o=(t,n,o)=>Reflect.apply(t,e.proxy,o);e.U=e=>{let t=n.get(e);return null===t&&n.set(e,t=new Proxy(e,{apply:o})),t}}(p,i);let b={el:e,B:[],T:p,host:d,pe:[],ht:new Map,pn:!1,fn:null,from:null,ae:null};for(let e of d.entries)b.pe.push({m:b,e});d.D=b,U.set(p,b);let v=null===d.engine;v?c.element?.(p,"mounting"):(function(e){let t=e.T,n=e.host,o=n.engine,l=n.el.localName,r=n.entries.find(e=>""===e.N)??null;for(let e of n.entries){if(""===e.N)continue;let t=_(e.N);ae(n,t)&&(e.N!==t||se(n,t))&&ie(`${l}.${e.N}`,`[wcs/mount-own-key-shadow] <${l}>: "${n.be}.${e.N}" is hidden by the component's own "${t}"; rename one side.`)}if(null!==r){let e=o.v(r.ve,r.r);if(null!==e&&"object"==typeof e)for(let t of Object.keys(n.state))"$"!==t[0]&&t in e&&!n.entries.some(e=>e.N===t)&&ie(`${l}.${t}`,`[wcs/mount-own-key-shadow] <${l}>: its own "${t}" hides "${r.ve.t}.${t}" — remove the default to read the tree, or rename it to keep it private.`)}let i=G.filter(e=>void 0!==n.state[e]);i.length>0&&ie(`${l}.${n.be}$`,`[wcs/mount-dollar-declaration] <${l}>: ${i.join(", ")} ${1===i.length?"is":"are"} not run in a mounted component — declare it on the root state.`);let a=t.rt;t.rt=n=>{ce(e,r,n),a.call(t,n)};let s=[...t.C.all()];for(let t of s)ce(e,r,t);for(let e of s)a.call(t,e);let u=t.xt;t.xt=()=>{(function(e){for(;void 0!==e;e=U.get(e.host.engine))if(e.host.entries.some(e=>null!==e.r&&!e.r.w))return!1;return!0})(e)&&u.call(t)}}(b),pe(b,!0),y.add(p),x(p,l));let w=c.wr?.(l,p);r(p,l),w?.(),b.B=[...l.childNodes],t(),v&&p.Pn(),Object.defineProperty(o,d.be,{configurable:!0,enumerable:!0,get:()=>p.proxy});let k=o.$stateReadyCallback;if("function"==typeof k)try{let e=k.call(o,d.be);null!==e&&"object"==typeof e&&"function"==typeof e.then&&e.then(void 0,$)}catch(e){$(e)}a(),e.isConnected?(await p.xe("$connectedCallback"),v&&e.isConnected&&c.element?.(p,"connected")):pe(b,!1)}async function Ne(e,t){pe(e,!0);for(let t of e.pe)ve(t);t?.(),!e.broken&&(await e.T.xe("$connectedCallback"),null===e.host.engine&&e.el.isConnected&&c.element?.(e.T,"connected"))}function je(e,t){let n=e.getAttribute("bind-component");if(null===n)return function(e,t){return e.connectedCallbackPromise.catch(()=>{let n=l.get(t)?.element;if(!(e.U||void 0!==n&&n!==e&&n.isConnected)){W(t,e);for(let e of D.get(t)??[])e()}}),null}(e,t);let r=e.parentNode,i=r instanceof ShadowRoot,a=i?r.host:r instanceof Element?r:null,s=()=>{let t=null===a?void 0:Q.get(a)?.D;return t?.el===e?t:null},f=t=>{throw i&&W(r,e),t},d=!1;return{$t:()=>(null!==a&&a.localName.includes("-")?async function(e,t,n,l){let r=n.localName;(["state","src","json"].some(t=>e.hasAttribute(t))||null!==e.querySelector('script[type="module"]'))&&g(`<${u.tagNames.state} bind-component> takes its state from <${r}>.${t}: it cannot also load one (state / src / json / an inline script).`),await o(n).whenDefined(r);let i=Q.get(n);if(void 0!==i)return i.be!==t&&g(`<${r}> already has a <${u.tagNames.state} bind-component="${i.be}">.`),i.state;let a=()=>(Z.get(n)??[]).filter(e=>_(e.name)===t),s=oe(n,t);if(s&&0===a().length&&le(n),0===a().length&&(s||((e,t)=>!0===e.getAttribute(L)?.split(" ").includes(t))(n,t))){let e;await new Promise((o,l)=>{J.set(n,o),e=function(e,t){P(e)&&t();let n=D.get(e)??new Set;return D.set(e,n.add(t)),()=>n.delete(t)}(n.getRootNode(),()=>l(new Error(`[@wcstack/state] <${r}>.${t} will not mount: the root state failed to initialize.`)))}).finally(()=>e())}J.delete(n);let c=a(),f=z.get(n),d=void 0!==f&&f.has(t)?f.get(t):n[t];if((null===d||"object"!=typeof d)&&g(`"bind-component": <${r}>.${t} must be an object (the component's state), got ${null===d?"null":typeof d}.`),0===c.length&&l){let e=new Error(`[@wcstack/state] A plain (unwired) Light DOM "bind-component" is not supported. Attach a shadow root to <${r}>, or mount it from the host (${u.bindAttributeName}="${t}: path").`);return queueMicrotask(()=>{throw e}),Y}let h=[];for(let e of c){let o=e.name===t?"":e.name.slice(t.length+1);h.some(e=>e.N===o)&&g(`<${r}> maps "${e.name}" twice.`);let l=o.split(".").indexOf("*");if(l>=0){let n=o.split(".").slice(0,l).join(".");g(`<${r}> maps "${e.name}": the component-side path of a mount cannot contain "*"${""===n?"":` — map the list itself ("${t}.${n}: <the host's list>")`}.`)}h.push({N:o,ve:e.o,r:e.r,ro:I.has(e)}),e.engine.An(e),""!==o&&delete n[e.name]}let p=0===c.length?function(e){let t=Object.getOwnPropertyDescriptors(e),n=Object.isFrozen(e);for(let e of Object.values(t))"value"in e&&!1===e.writable&&(e.writable=!0,n=!0);return n?Object.create(Object.getPrototypeOf(e),t):e}(d):d;return Q.set(n,i={el:n,be:t,state:p,engine:0===c.length?null:c[0].engine,entries:h,D:null}),p}(e,n,a,!i):Promise.reject(new Error(`[@wcstack/state] "bind-component" requires <${u.tagNames.state}> to be a direct child of a custom element.`))).catch(f),async start(t,n){await ke(e,a,i?r:a,t,()=>{d=!0,n()}).catch(e=>{let t=s();return!d&&null!==t&&(t.broken=!0),f(e)})},Qe(){let t=s();null!==t?Ne(t).catch($):d?Promise.resolve().then(()=>{if(e.parentNode===r)return ke(e,a,i?r:a,Q.get(a).state,()=>{})}).catch($):J.has(a)&&le(a)},Mn(){let e=s();null!==e&&(pe(e,!1),!e.broken&&(e.T.xe("$disconnectedCallback"),null===e.host.engine&&c.element?.(e.T,"disconnected")))},reset(){g(`re-setting a component's state is not supported: write <${a?.localName}>.${n} instead.`)}}}var Oe={name:"scopes",hooks:c,install(){c.ft=d(p,c.ft,q),c.ft=d(p,c.ft,K),c.ft=d(p,c.ft,je),c.dr=te,c.Po=ne,c.element=f(c.element,(e,t)=>{if("mounting"!==t)return function(e,t){let n=k.get(e)??[];if("disconnected"===t)for(let e of n)M(e);else if("reset"!==t&&C(l.get(e.root))===e){x(e,e.root);for(let e of n)b.get(e.root).has(e.t)||b.get(e.root).set(e.t,e)}}(e,t);let n=e.element.getRootNode();x(e,n),function(e,t){let n=t.host,o=void 0===n?void 0:R.get(n);void 0!==o&&B.set(e,{host:n,def:o})}(e,n)}),c.se=f(c.se,(e,t,n,o,l,r)=>{(function(e,t,n,o){let l=B.get(e);if(void 0===l)return;let r=t.t.split(".",1)[0];if(!l.def.bindables.includes(r))return;let i=o&&t.t===r;l.host.dispatchEvent(new CustomEvent(`${l.def.tag}:${r}-changed`,{detail:i?n:void 0,bubbles:!0}))})(e,t,l,r),ye(e,t,n,o,l,r,!1)}),c.Ne=f(c.Ne,(e,t,n)=>ye(e,t,n,void 0,void 0,!1,!0)),c.Ie=d(h,c.Ie,(e,t,n,o,l)=>(function(e,t){let n=k.get(e);if(void 0!==n&&0===t.n)for(let{t:e}of n)e.startsWith(`${t.t}.`)&&g(`writing "${t.t}" would replace the volume grafted at "${e}"; write the paths under it instead.`)}(e,t),function(e,t,n){let o,l=U.get(e);if(void 0===l)return!1;for(let e=t;null!==e&&void 0===o;e=e.c)o=l.ht.get(e);if(void 0===o)return!1;let r=o.e;return!((null===r.r||r.r.w)&&!r.ro)&&(!!n||(fe(l,r.r),void g(`[wcs/mount-readonly] <${l.host.el.localName}> cannot write "${t.t}": it is mounted read-only ("${l.host.be}${""===r.N?"":`.${r.N}`}#ro: ${r.ve.t}"). Write it on the host, or drop #ro from the mount.`)))}(e,t,l)));let e=c.fe;c.fe=(t,n)=>{let o=k.get(t);void 0!==o&&o.length>0&&g(`re-setting a root state with grafted volumes (${o.map(e=>e.t).join(", ")}) is not supported: their data is part of the tree.`),function(e){let t=V.get(e);if(void 0===t)return!1;for(let e of t.values())if(e.size>0)return!0;return!1}(t)&&g("re-setting a root state with mounted components is not supported: they read its data."),e?.(t,n)}}},Ae=Oe;export{Ae as default,Oe as scopes};
2
2
  //# sourceMappingURL=scopes.js.map