import c from "../../../../../../_virtual/___vite-browser-external.js";
import v from "../../../../../../_virtual/index29.js";
import F from "./css-syntax-error.js";
import y from "./previous-map.js";
let { SourceMapConsumer: O, SourceMapGenerator: L } = c, { fileURLToPath: C, pathToFileURL: a } = c, { resolve: g, isAbsolute: S } = c, { nanoid: M } = v, p = c, b = F, x = y, d = Symbol("fromOffsetCache"), R = Boolean(O && L), w = Boolean(g && S);
class m {
  constructor(t, e = {}) {
    if (t === null || typeof t > "u" || typeof t == "object" && !t.toString)
      throw new Error(`PostCSS received ${t} instead of CSS string`);
    if (this.css = t.toString(), this.css[0] === "\uFEFF" || this.css[0] === "\uFFFE" ? (this.hasBOM = !0, this.css = this.css.slice(1)) : this.hasBOM = !1, e.from && (!w || /^\w+:\/\//.test(e.from) || S(e.from) ? this.file = e.from : this.file = g(e.from)), w && R) {
      let i = new x(this.css, e);
      if (i.text) {
        this.map = i;
        let o = i.consumer().file;
        !this.file && o && (this.file = this.mapResolve(o));
      }
    }
    this.file || (this.id = "<input css " + M(6) + ">"), this.map && (this.map.file = this.from);
  }
  fromOffset(t) {
    let e, i;
    if (this[d])
      i = this[d];
    else {
      let l = this.css.split(`
`);
      i = new Array(l.length);
      let s = 0;
      for (let n = 0, r = l.length; n < r; n++)
        i[n] = s, s += l[n].length + 1;
      this[d] = i;
    }
    e = i[i.length - 1];
    let o = 0;
    if (t >= e)
      o = i.length - 1;
    else {
      let l = i.length - 2, s;
      for (; o < l; )
        if (s = o + (l - o >> 1), t < i[s])
          l = s - 1;
        else if (t >= i[s + 1])
          o = s + 1;
        else {
          o = s;
          break;
        }
    }
    return {
      line: o + 1,
      col: t - i[o] + 1
    };
  }
  error(t, e, i, o = {}) {
    let l, s, n;
    if (e && typeof e == "object") {
      let f = e, u = i;
      if (typeof e.offset == "number") {
        let h = this.fromOffset(f.offset);
        e = h.line, i = h.col;
      } else
        e = f.line, i = f.column;
      if (typeof u.offset == "number") {
        let h = this.fromOffset(u.offset);
        s = h.line, n = h.col;
      } else
        s = u.line, n = u.column;
    } else if (!i) {
      let f = this.fromOffset(e);
      e = f.line, i = f.col;
    }
    let r = this.origin(e, i, s, n);
    return r ? l = new b(
      t,
      r.endLine === void 0 ? r.line : { line: r.line, column: r.column },
      r.endLine === void 0 ? r.column : { line: r.endLine, column: r.endColumn },
      r.source,
      r.file,
      o.plugin
    ) : l = new b(
      t,
      s === void 0 ? e : { line: e, column: i },
      s === void 0 ? i : { line: s, column: n },
      this.css,
      this.file,
      o.plugin
    ), l.input = { line: e, column: i, endLine: s, endColumn: n, source: this.css }, this.file && (a && (l.input.url = a(this.file).toString()), l.input.file = this.file), l;
  }
  origin(t, e, i, o) {
    if (!this.map)
      return !1;
    let l = this.map.consumer(), s = l.originalPositionFor({ line: t, column: e });
    if (!s.source)
      return !1;
    let n;
    typeof i == "number" && (n = l.originalPositionFor({ line: i, column: o }));
    let r;
    S(s.source) ? r = a(s.source) : r = new URL(
      s.source,
      this.map.consumer().sourceRoot || a(this.map.mapFile)
    );
    let f = {
      url: r.toString(),
      line: s.line,
      column: s.column,
      endLine: n && n.line,
      endColumn: n && n.column
    };
    if (r.protocol === "file:")
      if (C)
        f.file = C(r);
      else
        throw new Error("file: protocol is not available in this PostCSS build");
    let u = l.sourceContentFor(s.source);
    return u && (f.source = u), f;
  }
  mapResolve(t) {
    return /^\w+:\/\//.test(t) ? t : g(this.map.consumer().sourceRoot || this.map.root || ".", t);
  }
  get from() {
    return this.file || this.id;
  }
  toJSON() {
    let t = {};
    for (let e of ["hasBOM", "css", "file", "id"])
      this[e] != null && (t[e] = this[e]);
    return this.map && (t.map = { ...this.map }, t.map.consumerCache && (t.map.consumerCache = void 0)), t;
  }
}
var A = m;
m.default = m;
p && p.registerInput && p.registerInput(m);
export {
  A as default
};
//# sourceMappingURL=input.js.map
