import { __require as L } from "./container.js";
import { __require as D } from "./document.js";
import { __require as A } from "./map-generator.js";
import { __require as V } from "./parse.js";
import { __require as k } from "./result.js";
import { __require as N } from "./root.js";
import { __require as T } from "./stringify.js";
import { __require as j } from "./symbols.js";
import { __require as z } from "./warn-once.js";
var g, v;
function J() {
  if (v) return g;
  v = 1;
  let E = L(), x = D(), S = A(), R = V(), m = k(), P = N(), _ = T(), { isClean: l, my: O } = j(), q = z();
  const C = {
    atrule: "AtRule",
    comment: "Comment",
    decl: "Declaration",
    document: "Document",
    root: "Root",
    rule: "Rule"
  }, I = {
    AtRule: !0,
    AtRuleExit: !0,
    Comment: !0,
    CommentExit: !0,
    Declaration: !0,
    DeclarationExit: !0,
    Document: !0,
    DocumentExit: !0,
    Once: !0,
    OnceExit: !0,
    postcssPlugin: !0,
    prepare: !0,
    Root: !0,
    RootExit: !0,
    Rule: !0,
    RuleExit: !0
  }, b = {
    Once: !0,
    postcssPlugin: !0,
    prepare: !0
  }, a = 0;
  function f(o) {
    return typeof o == "object" && typeof o.then == "function";
  }
  function d(o) {
    let e = !1, t = C[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 w(o) {
    let e;
    return o.type === "document" ? e = ["Document", a, "DocumentExit"] : o.type === "root" ? e = ["Root", a, "RootExit"] : e = d(o), {
      eventIndex: 0,
      events: e,
      iterator: 0,
      node: o,
      visitorIndex: 0,
      visitors: []
    };
  }
  function p(o) {
    return o[l] = !1, o.nodes && o.nodes.forEach((e) => p(e)), o;
  }
  let y = {};
  class h {
    get content() {
      return this.stringify().content;
    }
    get css() {
      return this.stringify().css;
    }
    get map() {
      return this.stringify().map;
    }
    get messages() {
      return this.sync().messages;
    }
    get opts() {
      return this.result.opts;
    }
    get processor() {
      return this.result.processor;
    }
    get root() {
      return this.sync().root;
    }
    get [Symbol.toStringTag]() {
      return "LazyResult";
    }
    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 = p(t);
      else if (t instanceof h || t instanceof m)
        s = p(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;
        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[O] && E.rebuild(s);
      }
      this.result = new m(e, s, r), this.helpers = { ...y, postcss: y, result: this.result }, this.plugins = this.processor.plugins.map((i) => typeof i == "object" && i.prepare ? { ...i, ...i.prepare(this.result) } : i);
    }
    async() {
      return this.error ? Promise.reject(this.error) : this.processed ? Promise.resolve(this.result) : (this.processing || (this.processing = this.runAsync()), this.processing);
    }
    catch(e) {
      return this.async().catch(e);
    }
    finally(e) {
      return this.async().then(e, e);
    }
    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, u = i.split("."), c = n.split(".");
          (u[0] !== c[0] || parseInt(u[1]) > parseInt(c[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;
    }
    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 (!I[r] && /^[A-Z]/.test(r))
              throw new Error(
                `Unknown event ${r} in ${t.postcssPlugin}. Try to update PostCSS (${this.processor.version} now).`
              );
            if (!b[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;
    }
    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 = [w(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();
    }
    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);
      }
    }
    stringify() {
      if (this.error) throw this.error;
      if (this.stringified) return this.result;
      this.stringified = !0, this.sync();
      let e = this.result.opts, t = _;
      e.syntax && (t = e.syntax.stringify), e.stringifier && (t = e.stringifier), t.stringify && (t = t.stringify);
      let s = new S(t, this.result.root, this.result.opts).generate();
      return this.result.css = s[0], this.result.map = s[1], this.result;
    }
    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;
    }
    then(e, t) {
      return process.env.NODE_ENV !== "production" && ("from" in this.opts || q(
        "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);
    }
    toString() {
      return this.css;
    }
    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();
      }
    }
    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, u] = s[t.visitorIndex];
        t.visitorIndex += 1, t.visitorIndex === s.length && (t.visitors = [], t.visitorIndex = 0), this.result.lastPlugin = n;
        try {
          return u(r.toProxy(), this.helpers);
        } catch (c) {
          throw this.handleError(c, r);
        }
      }
      if (t.iterator !== 0) {
        let n = t.iterator, u;
        for (; u = r.nodes[r.indexes[n]]; )
          if (r.indexes[n] += 1, !u[l]) {
            u[l] = !0, e.push(w(u));
            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();
    }
    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;
        }
    }
    warnings() {
      return this.sync().warnings();
    }
  }
  return h.registerPostcss = (o) => {
    y = o;
  }, g = h, h.default = h, P.registerLazyResult(h), x.registerLazyResult(h), g;
}
export {
  J as __require
};
//# sourceMappingURL=lazy-result.js.map
