import "./symbols.js";
import w from "./map-generator.js";
import E from "./stringify.js";
import v from "./container.js";
import x from "./document.js";
import S from "./warn-once.js";
import P from "./result.js";
import O from "./parse.js";
import R from "./root.js";
import { __exports as I } from "../../../../../../_virtual/symbols.js";
let { isClean: l, my: C } = I, b = w, A = E, D = v, L = x, V = S, m = P, _ = O, k = R;
const N = {
  document: "Document",
  root: "Root",
  atrule: "AtRule",
  rule: "Rule",
  decl: "Declaration",
  comment: "Comment"
}, T = {
  postcssPlugin: !0,
  prepare: !0,
  Once: !0,
  Document: !0,
  Root: !0,
  Declaration: !0,
  Rule: !0,
  AtRule: !0,
  Comment: !0,
  DeclarationExit: !0,
  RuleExit: !0,
  AtRuleExit: !0,
  CommentExit: !0,
  RootExit: !0,
  DocumentExit: !0,
  OnceExit: !0
}, j = {
  postcssPlugin: !0,
  prepare: !0,
  Once: !0
}, a = 0;
function f(o) {
  return typeof o == "object" && typeof o.then == "function";
}
function d(o) {
  let e = !1, t = N[o.type];
  return o.type === "decl" ? e = o.prop.toLowerCase() : o.type === "atrule" && (e = o.name.toLowerCase()), e && o.append ? [
    t,
    t + "-" + e,
    a,
    t + "Exit",
    t + "Exit-" + e
  ] : e ? [t, t + "-" + e, t + "Exit", t + "Exit-" + e] : o.append ? [t, a, t + "Exit"] : [t, t + "Exit"];
}
function g(o) {
  let e;
  return o.type === "document" ? e = ["Document", a, "DocumentExit"] : o.type === "root" ? e = ["Root", a, "RootExit"] : e = d(o), {
    node: o,
    events: e,
    eventIndex: 0,
    visitors: [],
    visitorIndex: 0,
    iterator: 0
  };
}
function c(o) {
  return o[l] = !1, o.nodes && o.nodes.forEach((e) => c(e)), o;
}
let y = {};
class u {
  constructor(e, t, r) {
    this.stringified = !1, this.processed = !1;
    let s;
    if (typeof t == "object" && t !== null && (t.type === "root" || t.type === "document"))
      s = c(t);
    else if (t instanceof u || t instanceof m)
      s = c(t.root), t.map && (typeof r.map > "u" && (r.map = {}), r.map.inline || (r.map.inline = !1), r.map.prev = t.map);
    else {
      let i = _;
      r.syntax && (i = r.syntax.parse), r.parser && (i = r.parser), i.parse && (i = i.parse);
      try {
        s = i(t, r);
      } catch (n) {
        this.processed = !0, this.error = n;
      }
      s && !s[C] && D.rebuild(s);
    }
    this.result = new m(e, s, r), this.helpers = { ...y, result: this.result, postcss: y }, this.plugins = this.processor.plugins.map((i) => typeof i == "object" && i.prepare ? { ...i, ...i.prepare(this.result) } : i);
  }
  get [Symbol.toStringTag]() {
    return "LazyResult";
  }
  get processor() {
    return this.result.processor;
  }
  get opts() {
    return this.result.opts;
  }
  get css() {
    return this.stringify().css;
  }
  get content() {
    return this.stringify().content;
  }
  get map() {
    return this.stringify().map;
  }
  get root() {
    return this.sync().root;
  }
  get messages() {
    return this.sync().messages;
  }
  warnings() {
    return this.sync().warnings();
  }
  toString() {
    return this.css;
  }
  then(e, t) {
    return process.env.NODE_ENV !== "production" && ("from" in this.opts || V(
      "Without `from` option PostCSS could generate wrong source map and will not find Browserslist config. Set it to CSS file path or to `undefined` to prevent this warning."
    )), this.async().then(e, t);
  }
  catch(e) {
    return this.async().catch(e);
  }
  finally(e) {
    return this.async().then(e, e);
  }
  async() {
    return this.error ? Promise.reject(this.error) : this.processed ? Promise.resolve(this.result) : (this.processing || (this.processing = this.runAsync()), this.processing);
  }
  sync() {
    if (this.error)
      throw this.error;
    if (this.processed)
      return this.result;
    if (this.processed = !0, this.processing)
      throw this.getAsyncError();
    for (let e of this.plugins) {
      let t = this.runOnRoot(e);
      if (f(t))
        throw this.getAsyncError();
    }
    if (this.prepareVisitors(), this.hasListener) {
      let e = this.result.root;
      for (; !e[l]; )
        e[l] = !0, this.walkSync(e);
      if (this.listeners.OnceExit)
        if (e.type === "document")
          for (let t of e.nodes)
            this.visitSync(this.listeners.OnceExit, t);
        else
          this.visitSync(this.listeners.OnceExit, e);
    }
    return this.result;
  }
  stringify() {
    if (this.error)
      throw this.error;
    if (this.stringified)
      return this.result;
    this.stringified = !0, this.sync();
    let e = this.result.opts, t = A;
    e.syntax && (t = e.syntax.stringify), e.stringifier && (t = e.stringifier), t.stringify && (t = t.stringify);
    let s = new b(t, this.result.root, this.result.opts).generate();
    return this.result.css = s[0], this.result.map = s[1], this.result;
  }
  walkSync(e) {
    e[l] = !0;
    let t = d(e);
    for (let r of t)
      if (r === a)
        e.nodes && e.each((s) => {
          s[l] || this.walkSync(s);
        });
      else {
        let s = this.listeners[r];
        if (s && this.visitSync(s, e.toProxy()))
          return;
      }
  }
  visitSync(e, t) {
    for (let [r, s] of e) {
      this.result.lastPlugin = r;
      let i;
      try {
        i = s(t, this.helpers);
      } catch (n) {
        throw this.handleError(n, t.proxyOf);
      }
      if (t.type !== "root" && t.type !== "document" && !t.parent)
        return !0;
      if (f(i))
        throw this.getAsyncError();
    }
  }
  runOnRoot(e) {
    this.result.lastPlugin = e;
    try {
      if (typeof e == "object" && e.Once) {
        if (this.result.root.type === "document") {
          let t = this.result.root.nodes.map(
            (r) => e.Once(r, this.helpers)
          );
          return f(t[0]) ? Promise.all(t) : t;
        }
        return e.Once(this.result.root, this.helpers);
      } else if (typeof e == "function")
        return e(this.result.root, this.result);
    } catch (t) {
      throw this.handleError(t);
    }
  }
  getAsyncError() {
    throw new Error("Use process(css).then(cb) to work with async plugins");
  }
  handleError(e, t) {
    let r = this.result.lastPlugin;
    try {
      if (t && t.addToError(e), this.error = e, e.name === "CssSyntaxError" && !e.plugin)
        e.plugin = r.postcssPlugin, e.setMessage();
      else if (r.postcssVersion && process.env.NODE_ENV !== "production") {
        let s = r.postcssPlugin, i = r.postcssVersion, n = this.result.processor.version, h = i.split("."), p = n.split(".");
        (h[0] !== p[0] || parseInt(h[1]) > parseInt(p[1])) && console.error(
          "Unknown error from PostCSS plugin. Your current PostCSS version is " + n + ", but " + s + " uses " + i + ". Perhaps this is the source of the error below."
        );
      }
    } catch (s) {
      console && console.error && console.error(s);
    }
    return e;
  }
  async runAsync() {
    this.plugin = 0;
    for (let e = 0; e < this.plugins.length; e++) {
      let t = this.plugins[e], r = this.runOnRoot(t);
      if (f(r))
        try {
          await r;
        } catch (s) {
          throw this.handleError(s);
        }
    }
    if (this.prepareVisitors(), this.hasListener) {
      let e = this.result.root;
      for (; !e[l]; ) {
        e[l] = !0;
        let t = [g(e)];
        for (; t.length > 0; ) {
          let r = this.visitTick(t);
          if (f(r))
            try {
              await r;
            } catch (s) {
              let i = t[t.length - 1].node;
              throw this.handleError(s, i);
            }
        }
      }
      if (this.listeners.OnceExit)
        for (let [t, r] of this.listeners.OnceExit) {
          this.result.lastPlugin = t;
          try {
            if (e.type === "document") {
              let s = e.nodes.map(
                (i) => r(i, this.helpers)
              );
              await Promise.all(s);
            } else
              await r(e, this.helpers);
          } catch (s) {
            throw this.handleError(s);
          }
        }
    }
    return this.processed = !0, this.stringify();
  }
  prepareVisitors() {
    this.listeners = {};
    let e = (t, r, s) => {
      this.listeners[r] || (this.listeners[r] = []), this.listeners[r].push([t, s]);
    };
    for (let t of this.plugins)
      if (typeof t == "object")
        for (let r in t) {
          if (!T[r] && /^[A-Z]/.test(r))
            throw new Error(
              `Unknown event ${r} in ${t.postcssPlugin}. Try to update PostCSS (${this.processor.version} now).`
            );
          if (!j[r])
            if (typeof t[r] == "object")
              for (let s in t[r])
                s === "*" ? e(t, r, t[r][s]) : e(
                  t,
                  r + "-" + s.toLowerCase(),
                  t[r][s]
                );
            else
              typeof t[r] == "function" && e(t, r, t[r]);
        }
    this.hasListener = Object.keys(this.listeners).length > 0;
  }
  visitTick(e) {
    let t = e[e.length - 1], { node: r, visitors: s } = t;
    if (r.type !== "root" && r.type !== "document" && !r.parent) {
      e.pop();
      return;
    }
    if (s.length > 0 && t.visitorIndex < s.length) {
      let [n, h] = s[t.visitorIndex];
      t.visitorIndex += 1, t.visitorIndex === s.length && (t.visitors = [], t.visitorIndex = 0), this.result.lastPlugin = n;
      try {
        return h(r.toProxy(), this.helpers);
      } catch (p) {
        throw this.handleError(p, r);
      }
    }
    if (t.iterator !== 0) {
      let n = t.iterator, h;
      for (; h = r.nodes[r.indexes[n]]; )
        if (r.indexes[n] += 1, !h[l]) {
          h[l] = !0, e.push(g(h));
          return;
        }
      t.iterator = 0, delete r.indexes[n];
    }
    let i = t.events;
    for (; t.eventIndex < i.length; ) {
      let n = i[t.eventIndex];
      if (t.eventIndex += 1, n === a) {
        r.nodes && r.nodes.length && (r[l] = !0, t.iterator = r.getIterator());
        return;
      } else if (this.listeners[n]) {
        t.visitors = this.listeners[n];
        return;
      }
    }
    e.pop();
  }
}
u.registerPostcss = (o) => {
  y = o;
};
var q = u;
u.default = u;
k.registerLazyResult(u);
L.registerLazyResult(u);
export {
  q as default
};
//# sourceMappingURL=lazy-result.js.map
