var E={REF_ATTR:"data-ref",SCOPE_ATTR:"data-scope",AUTO_PREFIX:"unnamed-scope"},a=class{constructor(e={}){this.refAttribute=e.refAttribute??E.REF_ATTR,this.scopeAttribute=e.scopeAttribute??E.SCOPE_ATTR,this.window=e.window??(typeof globalThis<"u"?globalThis.window:void 0),this.isScopeElement=e.isScopeElement??null,this.scopeAutoNamePrefix=e.scopeAutoNamePrefix??E.AUTO_PREFIX}},h=new a;function w(s,e=h){if(e.isScopeElement)return e.isScopeElement(s,e);let n=Array.isArray(e.scopeAttribute)?e.scopeAttribute:[e.scopeAttribute];for(let r of n){let t=s.getAttribute(r);if(t!==null)return t}return null}function b(s={}){return h=new a({...h,...s}),h}function A(s={}){return s instanceof a?s:new a({...h,...s})}function g(s,e=null,n={}){let r=A(n),t={},o={},c=[],f=Array.isArray(s)?s:[s];m(s,i=>{let l=i.getAttribute(r.refAttribute);if(l&&(t[l]?console.warn(`[Scope] Duplicate ref #${l} found during multi-root scan.`):t[l]=i),!f.includes(i)){let d=w(i,r);typeof d=="string"&&(d!==""&&!o[d]?o[d]=i:c.push(i))}e&&e(i)},r);let u=0,p=r.scopeAutoNamePrefix;for(let i of c){for(;o[p+u];)u++;o[p+u]=i}return{refs:t,scopeRefs:o}}function R(s,e=null,n={}){let r=A(n),t={};return m(s,c=>{let f=c.getAttribute(r.refAttribute);f&&(t[f]=c)},r),e&&S(t,e),t}function m(s,e,n){let r=A(n),t=r.window,o=Array.isArray(s)?s:[s];for(let c of o){let f=p=>{let i=p;if(o.includes(i))return t.NodeFilter.FILTER_ACCEPT;let l=i.parentElement;return l&&!o.includes(l)&&w(l,r)!==null?t.NodeFilter.FILTER_REJECT:t.NodeFilter.FILTER_ACCEPT},T=t.document.createTreeWalker(c,t.NodeFilter.SHOW_ELEMENT,{acceptNode:f}),u;for(;u=T.nextNode();)e(u)}}function S(s,e){for(let[n,r]of Object.entries(e)){let t=s[n];if(!t)throw new Error(`[Scope] Missing required data-ref: "${n}"`);let o=typeof r=="function"?r.prototype:r;if(!o.isPrototypeOf(t)){let c=t.constructor?.name||"Unknown",f=o.constructor?.name||"ExpectedType";throw new Error(`[Scope] Type mismatch for "${n}": expected ${f}, got ${c}`)}}}var y=class s{#r=!1;#e;#s=!1;#n={};#o={};config;constructor(e,n){if(!e)throw new Error("[DomScope] rootElement is required");this.#e=e,this.config=A(n)}get root(){return this.#e}get refs(){return this.#s||this.update(),this.#n}get scopes(){return this.#s||this.update(),this.#o}update(e){this.#t();let{refs:n,scopeRefs:r}=g(this.#e,e,this.config);this.#n=n;let t={};for(let[o,c]of Object.entries(r))t[o]=new s(c,this.config);this.#o=t,this.#s=!0}querySelector(e){this.#t();let n=this.querySelectorAll(e);return n.length>0?n[0]:null}querySelectorAll(e){this.#t();let n=this.#e.querySelectorAll(e);if(n.length===0)return[];let r=[];for(let t of n){let o=t;this.contains(o,!0)&&r.push(o)}return r}contains(e,n=!1){if(this.#t(),!n&&!this.#e.contains(e))return!1;let r=this.scopes;for(let t in r){let o=r[t].root;if(o===e)return!0;if(o.contains(e))return!1}return!0}walk(e){this.#t(),m(this.#e,e,this.config)}destroy(){this.#r=!0,this.#s=!1,this.#e=null,this.#n={},this.#o={},this.config=null}get isDestroyed(){return this.#r}isScopeElement(e){return w(e,this.config)!==null}checkRefs(e){this.#t(),S(this.refs,e)}#t(){if(this.#r)throw new Error("[DomScope] Instance is destroyed")}};export{y as DomScope,S as checkRefs,R as selectRefs,g as selectRefsExtended,b as setDefaults,m as walkDomScope};
