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