import e from"node:path";import s from"node:fs";import r from"node:module";function parseImport(t,o,n,c){let a;try{if(n.startsWith("node_modules://")){a=r.createRequire(process.cwd()).resolve(n.slice(15))}else if(n.startsWith("node_modules:")){a=r.createRequire(process.cwd()).resolve(n.slice(13))}else a=e.resolve(n)}catch(e){throw new Error(`Failed to read ${n} with error ${e instanceof Error?e.message:e}`,{cause:e})}if(c.has(a))return!1;c.add(a),o.result.messages.push({type:"dependency",plugin:"postcss-global-data",file:a,parent:t.source?.input?.file});const p=s.readFileSync(a,"utf8");return o.postcss.parse(p,{from:a})}const t="postcss-global-data",creator=e=>{const s=Object.assign({files:[],lateRemover:!1,prepend:!1},e);function insert(e,r,t){if(!s.prepend)return void r.each(s=>{e.append(s),t.add(s)});const o=Array.from(r.nodes);o.reverse(),o.forEach(s=>{e.prepend(s),t.add(s)})}if(!s.lateRemover)return{postcssPlugin:t,prepare(){const e=new Set,r=new Set;return{postcssPlugin:t,Once(t,o){s.files.forEach(s=>{const n=parseImport(t,o,s,r);n&&insert(t,n,e)})},OnceExit(){e.forEach(e=>{e.remove()}),r.clear(),e.clear()}}}};const r=new WeakSet;return{postcssPlugin:t,plugins:[{postcssPlugin:t,prepare(){const e=new Set;return{postcssPlugin:t,Once(t,o){s.files.forEach(s=>{const n=parseImport(t,o,s,e);n&&insert(t,n,r)})},OnceExit(){e.clear()}}}},{postcssPlugin:t+"/late-remover",OnceExit(e){e.each(e=>{r.has(e)&&e.remove()})}}]}};creator.postcss=!0;export{creator as default,creator as"module.exports"};
