import{walkSync as ue,ELEMENT_NODE as le,TEXT_NODE as xe}from"../index.js";import{querySelectorAll as me,specificity as fe}from"../selector.js";var Y="comm",q="rule",B="decl";var G=Math.abs,C=String.fromCharCode;function D(e){return e.trim()}function P(e,r,a){return e.replace(r,a)}function X(e,r){return e.indexOf(r)}function M(e,r){return e.charCodeAt(r)|0}function A(e,r,a){return e.slice(r,a)}function O(e){return e.length}function Z(e){return e.length}function R(e,r){return r.push(e),e}var _=1,S=1,J=0,h=0,c=0,j="";function z(e,r,a,p,u,E,w){return{value:e,root:r,parent:a,type:p,props:u,children:E,line:_,column:S,length:w,return:""}}function Q(){return c}function ee(){return c=h>0?M(j,--h):0,S--,c===10&&(S=1,_--),c}function d(){return c=h<J?M(j,h++):0,S++,c===10&&(S=1,_++),c}function T(){return M(j,h)}function I(){return h}function F(e,r){return A(j,e,r)}function H(e){switch(e){case 0:case 9:case 10:case 13:case 32:return 5;case 33:case 43:case 44:case 47:case 62:case 64:case 126:case 59:case 123:case 125:return 4;case 58:return 3;case 34:case 39:case 40:case 91:return 2;case 41:case 93:return 1}return 0}function re(e){return _=S=1,J=O(j=e),h=0,[]}function te(e){return j="",e}function $(e){return D(F(h-1,K(e===91?e+2:e===40?e+1:e)))}function ne(e){for(;(c=T())&&c<33;)d();return H(e)>2||H(c)>3?"":" "}function oe(e,r){for(;--r&&d()&&!(c<48||c>102||c>57&&c<65||c>70&&c<97););return F(e,I()+(r<6&&T()==32&&d()==32))}function K(e){for(;d();)switch(c){case e:return h;case 34:case 39:e!==34&&e!==39&&K(c);break;case 40:e===41&&K(e);break;case 92:d();break}return h}function ae(e,r){for(;d()&&e+c!==47+10;)if(e+c===42+42&&T()===47)break;return"/*"+F(r,h-1)+"*"+C(e===47?e:d())}function ce(e){for(;!H(T());)d();return F(e,h)}function V(e){return te(W("",null,null,null,[""],e=re(e),0,[0],e))}function W(e,r,a,p,u,E,w,m,k){for(var n=0,f=0,t=w,s=0,i=0,l=0,b=1,L=1,v=1,x=0,N="",U=u,y=E,g=p,o=N;L;)switch(l=x,x=d()){case 40:if(l!=108&&M(o,t-1)==58){X(o+=P($(x),"&","&\f"),"&\f")!=-1&&(v=-1);break}case 34:case 39:case 91:o+=$(x);break;case 9:case 10:case 13:case 32:o+=ne(l);break;case 92:o+=oe(I()-1,7);continue;case 47:switch(T()){case 42:case 47:R(pe(ae(d(),I()),r,a),k);break;default:o+="/"}break;case 123*b:m[n++]=O(o)*v;case 125*b:case 59:case 0:switch(x){case 0:case 125:L=0;case 59+f:i>0&&O(o)-t&&R(i>32?ie(o+";",p,a,t-1):ie(P(o," ","")+";",p,a,t-2),k);break;case 59:o+=";";default:if(R(g=se(o,r,a,n,f,u,m,N,U=[],y=[],t),E),x===123)if(f===0)W(o,r,g,g,U,E,t,m,y);else switch(s){case 100:case 109:case 115:W(e,g,g,p&&R(se(e,g,g,0,0,u,m,N,u,U=[],t),y),u,y,t,m,p?U:y);break;default:W(o,g,g,g,[""],y,0,m,y)}}n=f=i=0,b=v=1,N=o="",t=w;break;case 58:t=1+O(o),i=l;default:if(b<1){if(x==123)--b;else if(x==125&&b++==0&&ee()==125)continue}switch(o+=C(x),x*b){case 38:v=f>0?1:(o+="\f",-1);break;case 44:m[n++]=(O(o)-1)*v,v=1;break;case 64:T()===45&&(o+=$(d())),s=T(),f=t=O(N=o+=ce(I())),x++;break;case 45:l===45&&O(o)==2&&(b=0)}}return E}function se(e,r,a,p,u,E,w,m,k,n,f){for(var t=u-1,s=u===0?E:[""],i=Z(s),l=0,b=0,L=0;l<p;++l)for(var v=0,x=A(e,t+1,t=G(b=w[l])),N=e;v<i;++v)(N=D(b>0?s[v]+" "+x:P(x,/&\f/g,s[v])))&&(k[L++]=N);return z(e,r,a,u===0?q:m,k,n,f)}function pe(e,r,a){return z(e,r,a,Y,C(Q()),A(e,2,-2),0)}function ie(e,r,a,p){return z(e,r,a,B,A(e,0,p),A(e,p+1,-1),p)}function he(e){let{useObjectSyntax:r=!1}=e??{};return a=>{let p=r?[":where([style]) {}"]:[],u=[];ue(a,(n,f)=>{n.type===le&&n.name==="style"&&(p.push(n.children.map(t=>t.type===xe?t.value:"").join("")),u.push(()=>{f.children=f.children.filter(t=>t!==n)}))});for(let n of u)n();let E=p.join(`
`),w=V(E),m=new Map;for(let n of w)if(n.type==="rule"){let f=Object.fromEntries(n.children.map(t=>[t.props,t.children]));for(let t of n.props){let s=Object.assign(m.get(t)??{},f);m.set(t,s)}}let k=new Map;for(let[n,f]of Array.from(m).sort(([t],[s])=>{let i=fe(t),l=fe(s);return i>l?1:l>i?-1:0})){let t=me(a,n);for(let s of t){let i=k.get(s)??{};k.set(s,Object.assign(i,f))}}for(let[n,f]of k){let t=n.attributes.style??"",s={};for(let i of V(t))i.type==="decl"&&typeof i.props=="string"&&typeof i.children=="string"&&(s[i.props]=i.children);s=Object.assign({},f,s),r?n.attributes.style=s:n.attributes.style=`${Object.entries(s).map(([i,l])=>`${i}:${l.replace("!important","")};`).join("")}`}return a}}export{he as default};
