import{p as v,n as P,s as S,c as m}from"./parseley.Pl35HTLt.js";class T{constructor(t){this.branches=l(C(t))}build(t){return t(this.branches)}}function C(r){const t=r.length,e=new Array(t);for(let n=0;n<t;n++){const[s,o]=r[n],c=K(v(s));e[n]={ast:c,terminal:{type:"terminal",valueContainer:{index:n,value:o,specificity:c.specificity}}}}return e}function K(r){return d(r),P(r),r}function d(r){const t=[];r.list.forEach(e=>{switch(e.type){case"class":t.push({matcher:"~=",modifier:null,name:"class",namespace:null,specificity:e.specificity,type:"attrValue",value:e.name});break;case"id":t.push({matcher:"=",modifier:null,name:"id",namespace:null,specificity:e.specificity,type:"attrValue",value:e.name});break;case"combinator":d(e.left),t.push(e);break;case"universal":break;default:t.push(e);break}}),r.list=t}function l(r){const t=[];for(;r.length;){const e=g(r,c=>!0,y),{matches:n,nonmatches:s,empty:o}=V(r,e);r=s,n.length&&t.push($(e,n)),o.length&&t.push(...L(o))}return t}function L(r){const t=[];for(const e of r){const n=e.terminal;if(n.type==="terminal")t.push(n);else{const{matches:s,rest:o}=O(n.cont,c=>c.type==="terminal");s.forEach(c=>t.push(c)),o.length&&(n.cont=o,t.push(n))}}return t}function V(r,t){const e=[],n=[],s=[];for(const o of r){const c=o.ast.list;c.length?(c.some(a=>y(a)===t)?e:n).push(o):s.push(o)}return{matches:e,nonmatches:n,empty:s}}function y(r){switch(r.type){case"attrPresence":return`attrPresence ${r.name}`;case"attrValue":return`attrValue ${r.name}`;case"combinator":return`combinator ${r.combinator}`;default:return r.type}}function $(r,t){if(r==="tag")return W(t);if(r.startsWith("attrValue "))return E(r.substring(10),t);if(r.startsWith("attrPresence "))return A(r.substring(13),t);if(r==="combinator >")return h(">",t);if(r==="combinator +")return h("+",t);throw new Error(`Unsupported selector kind: ${r}`)}function W(r){const t=p(r,n=>n.type==="tag",n=>n.name);return{type:"tagName",variants:Object.entries(t).map(([n,s])=>({type:"variant",value:n,cont:l(s.items)}))}}function A(r,t){for(const e of t)b(e,n=>n.type==="attrPresence"&&n.name===r);return{type:"attrPresence",name:r,cont:l(t)}}function E(r,t){const e=p(t,s=>s.type==="attrValue"&&s.name===r,s=>`${s.matcher} ${s.modifier||""} ${s.value}`),n=[];for(const s of Object.values(e)){const o=s.oneSimpleSelector,c=B(o),i=l(s.items);n.push({type:"matcher",matcher:o.matcher,modifier:o.modifier,value:o.value,predicate:c,cont:i})}return{type:"attrValue",name:r,matchers:n}}function B(r){if(r.modifier==="i"){const t=r.value.toLowerCase();switch(r.matcher){case"=":return e=>t===e.toLowerCase();case"~=":return e=>e.toLowerCase().split(/[ \t]+/).includes(t);case"^=":return e=>e.toLowerCase().startsWith(t);case"$=":return e=>e.toLowerCase().endsWith(t);case"*=":return e=>e.toLowerCase().includes(t);case"|=":return e=>{const n=e.toLowerCase();return t===n||n.startsWith(t)&&n[t.length]==="-"}}}else{const t=r.value;switch(r.matcher){case"=":return e=>t===e;case"~=":return e=>e.split(/[ \t]+/).includes(t);case"^=":return e=>e.startsWith(t);case"$=":return e=>e.endsWith(t);case"*=":return e=>e.includes(t);case"|=":return e=>t===e||e.startsWith(t)&&e[t.length]==="-"}}}function h(r,t){const e=p(t,s=>s.type==="combinator"&&s.combinator===r,s=>S(s.left)),n=[];for(const s of Object.values(e)){const o=l(s.items),c=s.oneSimpleSelector.left;n.push({ast:c,terminal:{type:"popElement",cont:o}})}return{type:"pushElement",combinator:r,cont:l(n)}}function p(r,t,e){const n={};for(;r.length;){const s=g(r,t,e),o=u=>t(u)&&e(u)===s,c=u=>u.ast.list.some(o),{matches:i,rest:a}=j(r,c);let f=null;for(const u of i){const w=b(u,o);f||(f=w)}if(f==null)throw new Error("No simple selector is found.");n[s]={oneSimpleSelector:f,items:i},r=a}return n}function b(r,t){const e=r.ast.list,n=new Array(e.length);let s=-1;for(let c=e.length;c-- >0;)t(e[c])&&(n[c]=!0,s=c);if(s==-1)throw new Error("Couldn't find the required simple selector.");const o=e[s];return r.ast.list=e.filter((c,i)=>!n[i]),o}function g(r,t,e){const n={};for(const c of r){const i={};for(const a of c.ast.list.filter(t))i[e(a)]=!0;for(const a of Object.keys(i))n[a]?n[a]++:n[a]=1}let s="",o=0;for(const c of Object.entries(n))c[1]>o&&(s=c[0],o=c[1]);return s}function O(r,t){const e=[],n=[];for(const s of r)t(s)?e.push(s):n.push(s);return{matches:e,rest:n}}function j(r,t){const e=[],n=[];for(const s of r)t(s)?e.push(s):n.push(s);return{matches:e,rest:n}}class z{constructor(t){this.f=t}pickAll(t){return this.f(t)}pick1(t,e=!1){const n=this.f(t),s=n.length;if(s===0)return null;if(s===1)return n[0].value;const o=e?x:N;let c=n[0];for(let i=1;i<s;i++){const a=n[i];o(c,a)&&(c=a)}return c.value}}function x(r,t){const e=m(t.specificity,r.specificity);return e>0||e===0&&t.index<r.index}function N(r,t){const e=m(t.specificity,r.specificity);return e>0||e===0&&t.index>r.index}export{T as D,z as P};
