"use strict";const u=require("../../../../../../_virtual/___vite-browser-external.js"),M=require("./input.js");let{SourceMapConsumer:m,SourceMapGenerator:r}=u,{dirname:a,resolve:g,relative:d,sep:v}=u,{pathToFileURL:f}=u,O=M,S=Boolean(m&&r),C=Boolean(a&&g&&d&&v);class y{constructor(t,e,o,s){this.stringify=t,this.mapOpts=o.map||{},this.root=e,this.opts=o,this.css=s}isMap(){return typeof this.opts.map<"u"?!!this.opts.map:this.previous().length>0}previous(){if(!this.previousMaps)if(this.previousMaps=[],this.root)this.root.walk(t=>{if(t.source&&t.source.input.map){let e=t.source.input.map;this.previousMaps.includes(e)||this.previousMaps.push(e)}});else{let t=new O(this.css,this.opts);t.map&&this.previousMaps.push(t.map)}return this.previousMaps}isInline(){if(typeof this.mapOpts.inline<"u")return this.mapOpts.inline;let t=this.mapOpts.annotation;return typeof t<"u"&&t!==!0?!1:this.previous().length?this.previous().some(e=>e.inline):!0}isSourcesContent(){return typeof this.mapOpts.sourcesContent<"u"?this.mapOpts.sourcesContent:this.previous().length?this.previous().some(t=>t.withContent()):!0}clearAnnotation(){if(this.mapOpts.annotation!==!1)if(this.root){let t;for(let e=this.root.nodes.length-1;e>=0;e--)t=this.root.nodes[e],t.type==="comment"&&t.text.indexOf("# sourceMappingURL=")===0&&this.root.removeChild(e)}else this.css&&(this.css=this.css.replace(/(\n)?\/\*#[\S\s]*?\*\/$/gm,""))}setSourcesContent(){let t={};if(this.root)this.root.walk(e=>{if(e.source){let o=e.source.input.from;o&&!t[o]&&(t[o]=!0,this.map.setSourceContent(this.toUrl(this.path(o)),e.source.input.css))}});else if(this.css){let e=this.opts.from?this.toUrl(this.path(this.opts.from)):"<no source>";this.map.setSourceContent(e,this.css)}}applyPrevMaps(){for(let t of this.previous()){let e=this.toUrl(this.path(t.file)),o=t.root||a(t.file),s;this.mapOpts.sourcesContent===!1?(s=new m(t.text),s.sourcesContent&&(s.sourcesContent=s.sourcesContent.map(()=>null))):s=t.consumer(),this.map.applySourceMap(s,e,this.toUrl(this.path(o)))}}isAnnotation(){return this.isInline()?!0:typeof this.mapOpts.annotation<"u"?this.mapOpts.annotation:this.previous().length?this.previous().some(t=>t.annotation):!0}toBase64(t){return Buffer?Buffer.from(t).toString("base64"):window.btoa(unescape(encodeURIComponent(t)))}addAnnotation(){let t;this.isInline()?t="data:application/json;base64,"+this.toBase64(this.map.toString()):typeof this.mapOpts.annotation=="string"?t=this.mapOpts.annotation:typeof this.mapOpts.annotation=="function"?t=this.mapOpts.annotation(this.opts.to,this.root):t=this.outputFile()+".map";let e=`
`;this.css.includes(`\r
`)&&(e=`\r
`),this.css+=e+"/*# sourceMappingURL="+t+" */"}outputFile(){return this.opts.to?this.path(this.opts.to):this.opts.from?this.path(this.opts.from):"to.css"}generateMap(){if(this.root)this.generateString();else if(this.previous().length===1){let t=this.previous()[0].consumer();t.file=this.outputFile(),this.map=r.fromSourceMap(t)}else this.map=new r({file:this.outputFile()}),this.map.addMapping({source:this.opts.from?this.toUrl(this.path(this.opts.from)):"<no source>",generated:{line:1,column:0},original:{line:1,column:0}});return this.isSourcesContent()&&this.setSourcesContent(),this.root&&this.previous().length>0&&this.applyPrevMaps(),this.isAnnotation()&&this.addAnnotation(),this.isInline()?[this.css]:[this.css,this.map]}path(t){if(t.indexOf("<")===0||/^\w+:\/\//.test(t)||this.mapOpts.absolute)return t;let e=this.opts.to?a(this.opts.to):".";return typeof this.mapOpts.annotation=="string"&&(e=a(g(e,this.mapOpts.annotation))),t=d(e,t),t}toUrl(t){return v==="\\"&&(t=t.replace(/\\/g,"/")),encodeURI(t).replace(/[#?]/g,encodeURIComponent)}sourcePath(t){if(this.mapOpts.from)return this.toUrl(this.mapOpts.from);if(this.mapOpts.absolute){if(f)return f(t.source.input.from).toString();throw new Error("`map.absolute` option is not available in this PostCSS build")}else return this.toUrl(this.path(t.source.input.from))}generateString(){this.css="",this.map=new r({file:this.outputFile()});let t=1,e=1,o="<no source>",s={source:"",generated:{line:0,column:0},original:{line:0,column:0}},p,l;this.stringify(this.root,(n,i,h)=>{if(this.css+=n,i&&h!=="end"&&(s.generated.line=t,s.generated.column=e-1,i.source&&i.source.start?(s.source=this.sourcePath(i),s.original.line=i.source.start.line,s.original.column=i.source.start.column-1,this.map.addMapping(s)):(s.source=o,s.original.line=1,s.original.column=0,this.map.addMapping(s))),p=n.match(/\n/g),p?(t+=p.length,l=n.lastIndexOf(`
`),e=n.length-l):e+=n.length,i&&h!=="start"){let c=i.parent||{raws:{}};(i.type!=="decl"||i!==c.last||c.raws.semicolon)&&(i.source&&i.source.end?(s.source=this.sourcePath(i),s.original.line=i.source.end.line,s.original.column=i.source.end.column-1,s.generated.line=t,s.generated.column=e-2,this.map.addMapping(s)):(s.source=o,s.original.line=1,s.original.column=0,s.generated.line=t,s.generated.column=e-1,this.map.addMapping(s)))}})}generate(){if(this.clearAnnotation(),C&&S&&this.isMap())return this.generateMap();{let t="";return this.stringify(this.root,e=>{t+=e}),[t]}}}var w=y;module.exports=w;
//# sourceMappingURL=map-generator.js.map
