import e from"postcss-selector-parser";const t=e().astSync(":link").nodes[0],s=e().astSync(":visited").nodes[0],n=e().astSync("area[href]").nodes[0],o=e().astSync("[href]").nodes[0];function replaceAnyLink(e,t,s,n){const o=[],r=[];try{for(let t=0;t<e.selectors.length;t++){const s=e.selectors[t],l=modifiedSelector(s,n);l.length?o.push(...l):r.push(s)}}catch(s){return e.warn(t,`Failed to parse selector : "${e.selector}" with message: "${s instanceof Error?s.message:s}"`),!1}return!!o.length&&(e.cloneBefore({selectors:o}),r.length&&e.cloneBefore({selectors:r}),s||e.remove(),!0)}function modifiedSelector(r,l){const c=[];return e(e=>{const r=[];if(e.walkPseudos(e=>{if(":not"===e.value.toLowerCase())return!1;if(":any-link"!==e.value.toLowerCase()||e.nodes&&e.nodes.length)return;if(!l)return void r.push([t.clone(),s.clone()]);const c=getTagElementsNextToPseudo(e);c.includes("area")?r.push([t.clone(),s.clone(),o.clone()]):c.length?r.push([t.clone(),s.clone()]):r.push([t.clone(),s.clone(),n.clone()])}),!r.length)return;cartesianProduct(...r).forEach(t=>{const s=e.clone();s.walkPseudos(e=>{":any-link"!==e.value.toLowerCase()||e.nodes&&e.nodes.length||(insertNode(e.parent,e,t.shift()),e.remove())}),c.push(s.toString())})}).processSync(r),c}function cartesianProduct(...e){const t=[],s=e.length-1;return function helper(n,o){for(let r=0,l=e[o].length;r<l;r++){const l=n.slice(0);l.push(e[o][r]),o===s?t.push(l):helper(l,o+1)}}([],0),t}function getTagElementsNextToPseudo(t){const s=[];let n=t.prev();for(;n&&"combinator"!==n.type&&!e.isPseudoElement(n);)"tag"===n.type&&s.push(n.value.toLowerCase()),n=n.prev();let o=t.next();for(;o&&"combinator"!==o.type&&!e.isPseudoElement(o);)"tag"===o.type&&s.push(o.value.toLowerCase()),o=o.next();return s}function insertNode(t,s,n){if(!t||!n)return;let o=n.type;"selector"===n.type&&n.nodes&&n.nodes.length&&(o=n.nodes[0].type);let r=-1,l=-1;const c=t.index(s);for(let s=c;s>=0&&("combinator"!==t.nodes[s].type&&!e.isPseudoElement(t.nodes[s].type));s--)r=s;if("tag"!==o){for(let s=c;s<t.nodes.length&&("combinator"!==t.nodes[s].type&&!e.isPseudoElement(t.nodes[s].type));s++)l=s;for(let e=r;e<=l;e++)if(t.nodes[e].type===o)return void t.insertAfter(t.at(e),n);t.insertAfter(t.at(r),n)}else t.insertBefore(t.at(r),n)}const creator=e=>{const t={preserve:e?.preserve??!0,subFeatures:{areaHrefNeedsFixing:e?.subFeatures?.areaHrefNeedsFixing??!1}};return{postcssPlugin:"postcss-pseudo-class-any-link",prepare(){const e=new WeakSet;return{postcssPlugin:"postcss-pseudo-class-any-link",Rule(s,{result:n}){if(!s.selector.toLowerCase().includes(":any-link"))return;if(e.has(s))return;if((s.raws.selector&&s.raws.selector.raw||s.selector).endsWith(":"))return;replaceAnyLink(s,n,t.preserve,t.subFeatures.areaHrefNeedsFixing)&&e.add(s)}}}}};creator.postcss=!0;export{creator as default,creator as"module.exports"};
