"use strict";var e=require("postcss-selector-parser"),t=require("@csstools/selector-specificity"),s=require("postcss-value-parser");function encodeCSS(e){if(""===e)return"";let t,s="";for(let r=0;r<e.length;r++)t=e.charCodeAt(r).toString(36),s+=0===r?t:"-"+t;return"csstools-has-"+s}function isGuardedByAtSupportsFromAtRuleParams(e){if(!e.toLowerCase().includes(":has("))return!1;let t=!1;try{const r=new Set;s(e).walk((e=>{if("function"===e.type&&"selector"===e.value.toLowerCase())return r.add(s.stringify(e.nodes)),!1})),r.forEach((e=>{selectorContainsHasPseudo(e)&&(t=!0)}))}catch(e){}return t}function selectorContainsHasPseudo(t){if(!t.toLowerCase().includes(":has("))return!1;let s=!1;try{e().astSync(t).walk((e=>{if("pseudo"===e.type&&":has"===e.value.toLowerCase()&&e.nodes&&e.nodes.length>0)return s=!0,!1}))}catch(e){}return s}const creator=s=>{const r={preserve:!0,specificityMatchingName:"does-not-exist",...s||{}},o=":not(#"+r.specificityMatchingName+")",n=":not(."+r.specificityMatchingName+")",a=":not("+r.specificityMatchingName+")";return{postcssPlugin:"css-has-pseudo",prepare(){const s=new WeakSet;return{RuleExit:(c,{result:i})=>{if(s.has(c))return;if(!c.selector.toLowerCase().includes(":has(")||isWithinSupportCheck(c))return;const l=c.selectors.map((s=>{if(!s.toLowerCase().includes(":has("))return s;let l;try{l=e().astSync(s)}catch(e){return c.warn(i,`Failed to parse selector : "${s}" with message: "${e.message}"`),s}if(void 0===l)return s;l.walkPseudos((t=>{let s=t.parent,o=!1;for(;s;)e.isPseudoClass(s)&&":has"===s.value.toLowerCase()&&(o=!0),s=s.parent;o&&(":visited"===t.value.toLowerCase()&&t.replaceWith(e.className({value:r.specificityMatchingName})),":any-link"===t.value.toLowerCase()&&(t.value=":link"))})),l.walkPseudos((s=>{if(":has"!==s.value.toLowerCase()||!s.nodes)return;let r=s.parent??s;if(r!==s){let t=r.nodes.length;e:for(let s=0;s<r.nodes.length;s++){const o=r.nodes[s];if(e.isPseudoElement(o))for(let e=s-1;e>=0;e--)if("combinator"!==r.nodes[s].type&&"comment"!==r.nodes[s].type){t=e+1;break e}}if(t<r.nodes.length){const s=e.selector({value:"",nodes:[]});r.nodes.slice(0,t).forEach((e=>{delete e.parent,s.append(e)}));const o=e.selector({value:"",nodes:[]});r.nodes.slice(t).forEach((e=>{delete e.parent,o.append(e)}));const n=e.selector({value:"",nodes:[]});n.append(s),n.append(o),r.replaceWith(n),r=s}}const c="["+encodeCSS(r.toString())+"]",i=t.selectorSpecificity(r);let l=c;for(let e=0;e<i.a;e++)l+=o;const u=Math.max(1,i.b)-1;for(let e=0;e<u;e++)l+=n;for(let e=0;e<i.c;e++)l+=a;const p=e().astSync(l);r.replaceWith(p.nodes[0])}));const u=l.toString();return u!==s?".js-has-pseudo "+u:s}));l.join(",")!==c.selectors.join(",")&&(s.add(c),c.cloneBefore({selectors:l}),r.preserve||c.remove())}}}}};function isWithinSupportCheck(e){let t=e.parent;for(;t;){if("atrule"===t.type&&isGuardedByAtSupportsFromAtRuleParams(t.params))return!0;t=t.parent}return!1}creator.postcss=!0,module.exports=creator;
