import l from "../../../../../../_virtual/___vite-browser-external.js";
import { __require as y } from "./input.js";
var h, U;
function L() {
  if (U) return h;
  U = 1;
  let { dirname: n, relative: u, resolve: m, sep: c } = l, { SourceMapConsumer: f, SourceMapGenerator: a } = l, { pathToFileURL: g } = l, O = y(), S = !!(f && a), C = !!(n && m && u && c);
  class w {
    constructor(t, e, s, i) {
      this.stringify = t, this.mapOpts = s.map || {}, this.root = e, this.opts = s, this.css = i, this.originalCSS = i, this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute, this.memoizedFileURLs = /* @__PURE__ */ new Map(), this.memoizedPaths = /* @__PURE__ */ new Map(), this.memoizedURLs = /* @__PURE__ */ new Map();
    }
    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 + " */";
    }
    applyPrevMaps() {
      for (let t of this.previous()) {
        let e = this.toUrl(this.path(t.file)), s = t.root || n(t.file), i;
        this.mapOpts.sourcesContent === !1 ? (i = new f(t.text), i.sourcesContent && (i.sourcesContent = null)) : i = t.consumer(), this.map.applySourceMap(i, e, this.toUrl(this.path(s)));
      }
    }
    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.startsWith("# sourceMappingURL=") && this.root.removeChild(e);
        } else this.css && (this.css = this.css.replace(/\n*\/\*#[\S\s]*?\*\/$/gm, ""));
    }
    generate() {
      if (this.clearAnnotation(), C && S && this.isMap())
        return this.generateMap();
      {
        let t = "";
        return this.stringify(this.root, (e) => {
          t += e;
        }), [t];
      }
    }
    generateMap() {
      if (this.root)
        this.generateString();
      else if (this.previous().length === 1) {
        let t = this.previous()[0].consumer();
        t.file = this.outputFile(), this.map = a.fromSourceMap(t, {
          ignoreInvalidMapping: !0
        });
      } else
        this.map = new a({
          file: this.outputFile(),
          ignoreInvalidMapping: !0
        }), this.map.addMapping({
          generated: { column: 0, line: 1 },
          original: { column: 0, line: 1 },
          source: this.opts.from ? this.toUrl(this.path(this.opts.from)) : "<no source>"
        });
      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];
    }
    generateString() {
      this.css = "", this.map = new a({
        file: this.outputFile(),
        ignoreInvalidMapping: !0
      });
      let t = 1, e = 1, s = "<no source>", i = {
        generated: { column: 0, line: 0 },
        original: { column: 0, line: 0 },
        source: ""
      }, d, p;
      this.stringify(this.root, (o, r, v) => {
        if (this.css += o, r && v !== "end" && (i.generated.line = t, i.generated.column = e - 1, r.source && r.source.start ? (i.source = this.sourcePath(r), i.original.line = r.source.start.line, i.original.column = r.source.start.column - 1, this.map.addMapping(i)) : (i.source = s, i.original.line = 1, i.original.column = 0, this.map.addMapping(i))), p = o.match(/\n/g), p ? (t += p.length, d = o.lastIndexOf(`
`), e = o.length - d) : e += o.length, r && v !== "start") {
          let M = r.parent || { raws: {} };
          (!(r.type === "decl" || r.type === "atrule" && !r.nodes) || r !== M.last || M.raws.semicolon) && (r.source && r.source.end ? (i.source = this.sourcePath(r), i.original.line = r.source.end.line, i.original.column = r.source.end.column - 1, i.generated.line = t, i.generated.column = e - 2, this.map.addMapping(i)) : (i.source = s, i.original.line = 1, i.original.column = 0, i.generated.line = t, i.generated.column = e - 1, this.map.addMapping(i)));
        }
      });
    }
    isAnnotation() {
      return this.isInline() ? !0 : typeof this.mapOpts.annotation < "u" ? this.mapOpts.annotation : this.previous().length ? this.previous().some((t) => t.annotation) : !0;
    }
    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;
    }
    isMap() {
      return typeof this.opts.map < "u" ? !!this.opts.map : this.previous().length > 0;
    }
    isSourcesContent() {
      return typeof this.mapOpts.sourcesContent < "u" ? this.mapOpts.sourcesContent : this.previous().length ? this.previous().some((t) => t.withContent()) : !0;
    }
    outputFile() {
      return this.opts.to ? this.path(this.opts.to) : this.opts.from ? this.path(this.opts.from) : "to.css";
    }
    path(t) {
      if (this.mapOpts.absolute || t.charCodeAt(0) === 60 || /^\w+:\/\//.test(t)) return t;
      let e = this.memoizedPaths.get(t);
      if (e) return e;
      let s = this.opts.to ? n(this.opts.to) : ".";
      typeof this.mapOpts.annotation == "string" && (s = n(m(s, this.mapOpts.annotation)));
      let i = u(s, t);
      return this.memoizedPaths.set(t, i), i;
    }
    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.originalCSS, this.opts);
          t.map && this.previousMaps.push(t.map);
        }
      return this.previousMaps;
    }
    setSourcesContent() {
      let t = {};
      if (this.root)
        this.root.walk((e) => {
          if (e.source) {
            let s = e.source.input.from;
            if (s && !t[s]) {
              t[s] = !0;
              let i = this.usesFileUrls ? this.toFileUrl(s) : this.toUrl(this.path(s));
              this.map.setSourceContent(i, 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);
      }
    }
    sourcePath(t) {
      return this.mapOpts.from ? this.toUrl(this.mapOpts.from) : this.usesFileUrls ? this.toFileUrl(t.source.input.from) : this.toUrl(this.path(t.source.input.from));
    }
    toBase64(t) {
      return Buffer ? Buffer.from(t).toString("base64") : window.btoa(unescape(encodeURIComponent(t)));
    }
    toFileUrl(t) {
      let e = this.memoizedFileURLs.get(t);
      if (e) return e;
      if (g) {
        let s = g(t).toString();
        return this.memoizedFileURLs.set(t, s), s;
      } else
        throw new Error(
          "`map.absolute` option is not available in this PostCSS build"
        );
    }
    toUrl(t) {
      let e = this.memoizedURLs.get(t);
      if (e) return e;
      c === "\\" && (t = t.replace(/\\/g, "/"));
      let s = encodeURI(t).replace(/[#?]/g, encodeURIComponent);
      return this.memoizedURLs.set(t, s), s;
    }
  }
  return h = w, h;
}
export {
  L as __require
};
//# sourceMappingURL=map-generator.js.map
