import { __exports as z } from "../../../../../../../../_virtual/index19.js";
import { __require as E } from "./code.js";
import { __require as j } from "./scope.js";
var A;
function ee() {
  return A ? z : (A = 1, function(s) {
    Object.defineProperty(s, "__esModule", { value: !0 }), s.or = s.and = s.not = s.CodeGen = s.operators = s.varKinds = s.ValueScopeName = s.ValueScope = s.Scope = s.Name = s.regexpCode = s.stringify = s.getProperty = s.nil = s.strConcat = s.str = s._ = void 0;
    const o = E(), h = j();
    var c = E();
    Object.defineProperty(s, "_", { enumerable: !0, get: function() {
      return c._;
    } }), Object.defineProperty(s, "str", { enumerable: !0, get: function() {
      return c.str;
    } }), Object.defineProperty(s, "strConcat", { enumerable: !0, get: function() {
      return c.strConcat;
    } }), Object.defineProperty(s, "nil", { enumerable: !0, get: function() {
      return c.nil;
    } }), Object.defineProperty(s, "getProperty", { enumerable: !0, get: function() {
      return c.getProperty;
    } }), Object.defineProperty(s, "stringify", { enumerable: !0, get: function() {
      return c.stringify;
    } }), Object.defineProperty(s, "regexpCode", { enumerable: !0, get: function() {
      return c.regexpCode;
    } }), Object.defineProperty(s, "Name", { enumerable: !0, get: function() {
      return c.Name;
    } });
    var N = j();
    Object.defineProperty(s, "Scope", { enumerable: !0, get: function() {
      return N.Scope;
    } }), Object.defineProperty(s, "ValueScope", { enumerable: !0, get: function() {
      return N.ValueScope;
    } }), Object.defineProperty(s, "ValueScopeName", { enumerable: !0, get: function() {
      return N.ValueScopeName;
    } }), Object.defineProperty(s, "varKinds", { enumerable: !0, get: function() {
      return N.varKinds;
    } }), s.operators = {
      GT: new o._Code(">"),
      GTE: new o._Code(">="),
      LT: new o._Code("<"),
      LTE: new o._Code("<="),
      EQ: new o._Code("==="),
      NEQ: new o._Code("!=="),
      NOT: new o._Code("!"),
      OR: new o._Code("||"),
      AND: new o._Code("&&"),
      ADD: new o._Code("+")
    };
    class l {
      optimizeNodes() {
        return this;
      }
      optimizeNames(e, t) {
        return this;
      }
    }
    class G extends l {
      constructor(e, t, r) {
        super(), this.varKind = e, this.name = t, this.rhs = r;
      }
      render({ es5: e, _n: t }) {
        const r = e ? h.varKinds.var : this.varKind, i = this.rhs === void 0 ? "" : ` = ${this.rhs}`;
        return `${r} ${this.name}${i};` + t;
      }
      optimizeNames(e, t) {
        if (e[this.name.str])
          return this.rhs && (this.rhs = _(this.rhs, e, t)), this;
      }
      get names() {
        return this.rhs instanceof o._CodeOrName ? this.rhs.names : {};
      }
    }
    class O extends l {
      constructor(e, t, r) {
        super(), this.lhs = e, this.rhs = t, this.sideEffects = r;
      }
      render({ _n: e }) {
        return `${this.lhs} = ${this.rhs};` + e;
      }
      optimizeNames(e, t) {
        if (!(this.lhs instanceof o.Name && !e[this.lhs.str] && !this.sideEffects))
          return this.rhs = _(this.rhs, e, t), this;
      }
      get names() {
        const e = this.lhs instanceof o.Name ? {} : { ...this.lhs.names };
        return w(e, this.rhs);
      }
    }
    class F extends O {
      constructor(e, t, r, i) {
        super(e, r, i), this.op = t;
      }
      render({ _n: e }) {
        return `${this.lhs} ${this.op}= ${this.rhs};` + e;
      }
    }
    class R extends l {
      constructor(e) {
        super(), this.label = e, this.names = {};
      }
      render({ _n: e }) {
        return `${this.label}:` + e;
      }
    }
    class V extends l {
      constructor(e) {
        super(), this.label = e, this.names = {};
      }
      render({ _n: e }) {
        return `break${this.label ? ` ${this.label}` : ""};` + e;
      }
    }
    class q extends l {
      constructor(e) {
        super(), this.error = e;
      }
      render({ _n: e }) {
        return `throw ${this.error};` + e;
      }
      get names() {
        return this.error.names;
      }
    }
    class D extends l {
      constructor(e) {
        super(), this.code = e;
      }
      render({ _n: e }) {
        return `${this.code};` + e;
      }
      optimizeNodes() {
        return `${this.code}` ? this : void 0;
      }
      optimizeNames(e, t) {
        return this.code = _(this.code, e, t), this;
      }
      get names() {
        return this.code instanceof o._CodeOrName ? this.code.names : {};
      }
    }
    class b extends l {
      constructor(e = []) {
        super(), this.nodes = e;
      }
      render(e) {
        return this.nodes.reduce((t, r) => t + r.render(e), "");
      }
      optimizeNodes() {
        const { nodes: e } = this;
        let t = e.length;
        for (; t--; ) {
          const r = e[t].optimizeNodes();
          Array.isArray(r) ? e.splice(t, 1, ...r) : r ? e[t] = r : e.splice(t, 1);
        }
        return e.length > 0 ? this : void 0;
      }
      optimizeNames(e, t) {
        const { nodes: r } = this;
        let i = r.length;
        for (; i--; ) {
          const u = r[i];
          u.optimizeNames(e, t) || (M(e, u.names), r.splice(i, 1));
        }
        return r.length > 0 ? this : void 0;
      }
      get names() {
        return this.nodes.reduce((e, t) => m(e, t.names), {});
      }
    }
    class f extends b {
      render(e) {
        return "{" + e._n + super.render(e) + "}" + e._n;
      }
    }
    class T extends b {
    }
    class g extends f {
    }
    g.kind = "else";
    class a extends f {
      constructor(e, t) {
        super(t), this.condition = e;
      }
      render(e) {
        let t = `if(${this.condition})` + super.render(e);
        return this.else && (t += "else " + this.else.render(e)), t;
      }
      optimizeNodes() {
        super.optimizeNodes();
        const e = this.condition;
        if (e === !0)
          return this.nodes;
        let t = this.else;
        if (t) {
          const r = t.optimizeNodes();
          t = this.else = Array.isArray(r) ? new g(r) : r;
        }
        if (t)
          return e === !1 ? t instanceof a ? t : t.nodes : this.nodes.length ? this : new a(K(e), t instanceof a ? [t] : t.nodes);
        if (!(e === !1 || !this.nodes.length))
          return this;
      }
      optimizeNames(e, t) {
        var r;
        if (this.else = (r = this.else) === null || r === void 0 ? void 0 : r.optimizeNames(e, t), !!(super.optimizeNames(e, t) || this.else))
          return this.condition = _(this.condition, e, t), this;
      }
      get names() {
        const e = super.names;
        return w(e, this.condition), this.else && m(e, this.else.names), e;
      }
    }
    a.kind = "if";
    class p extends f {
    }
    p.kind = "for";
    class I extends p {
      constructor(e) {
        super(), this.iteration = e;
      }
      render(e) {
        return `for(${this.iteration})` + super.render(e);
      }
      optimizeNames(e, t) {
        if (super.optimizeNames(e, t))
          return this.iteration = _(this.iteration, e, t), this;
      }
      get names() {
        return m(super.names, this.iteration.names);
      }
    }
    class L extends p {
      constructor(e, t, r, i) {
        super(), this.varKind = e, this.name = t, this.from = r, this.to = i;
      }
      render(e) {
        const t = e.es5 ? h.varKinds.var : this.varKind, { name: r, from: i, to: u } = this;
        return `for(${t} ${r}=${i}; ${r}<${u}; ${r}++)` + super.render(e);
      }
      get names() {
        const e = w(super.names, this.from);
        return w(e, this.to);
      }
    }
    class S extends p {
      constructor(e, t, r, i) {
        super(), this.loop = e, this.varKind = t, this.name = r, this.iterable = i;
      }
      render(e) {
        return `for(${this.varKind} ${this.name} ${this.loop} ${this.iterable})` + super.render(e);
      }
      optimizeNames(e, t) {
        if (super.optimizeNames(e, t))
          return this.iterable = _(this.iterable, e, t), this;
      }
      get names() {
        return m(super.names, this.iterable.names);
      }
    }
    class v extends f {
      constructor(e, t, r) {
        super(), this.name = e, this.args = t, this.async = r;
      }
      render(e) {
        return `${this.async ? "async " : ""}function ${this.name}(${this.args})` + super.render(e);
      }
    }
    v.kind = "func";
    class $ extends b {
      render(e) {
        return "return " + super.render(e);
      }
    }
    $.kind = "return";
    class Q extends f {
      render(e) {
        let t = "try" + super.render(e);
        return this.catch && (t += this.catch.render(e)), this.finally && (t += this.finally.render(e)), t;
      }
      optimizeNodes() {
        var e, t;
        return super.optimizeNodes(), (e = this.catch) === null || e === void 0 || e.optimizeNodes(), (t = this.finally) === null || t === void 0 || t.optimizeNodes(), this;
      }
      optimizeNames(e, t) {
        var r, i;
        return super.optimizeNames(e, t), (r = this.catch) === null || r === void 0 || r.optimizeNames(e, t), (i = this.finally) === null || i === void 0 || i.optimizeNames(e, t), this;
      }
      get names() {
        const e = super.names;
        return this.catch && m(e, this.catch.names), this.finally && m(e, this.finally.names), e;
      }
    }
    class C extends f {
      constructor(e) {
        super(), this.error = e;
      }
      render(e) {
        return `catch(${this.error})` + super.render(e);
      }
    }
    C.kind = "catch";
    class y extends f {
      render(e) {
        return "finally" + super.render(e);
      }
    }
    y.kind = "finally";
    class B {
      constructor(e, t = {}) {
        this._values = {}, this._blockStarts = [], this._constants = {}, this.opts = { ...t, _n: t.lines ? `
` : "" }, this._extScope = e, this._scope = new h.Scope({ parent: e }), this._nodes = [new T()];
      }
      toString() {
        return this._root.render(this.opts);
      }
      // returns unique name in the internal scope
      name(e) {
        return this._scope.name(e);
      }
      // reserves unique name in the external scope
      scopeName(e) {
        return this._extScope.name(e);
      }
      // reserves unique name in the external scope and assigns value to it
      scopeValue(e, t) {
        const r = this._extScope.value(e, t);
        return (this._values[r.prefix] || (this._values[r.prefix] = /* @__PURE__ */ new Set())).add(r), r;
      }
      getScopeValue(e, t) {
        return this._extScope.getValue(e, t);
      }
      // return code that assigns values in the external scope to the names that are used internally
      // (same names that were returned by gen.scopeName or gen.scopeValue)
      scopeRefs(e) {
        return this._extScope.scopeRefs(e, this._values);
      }
      scopeCode() {
        return this._extScope.scopeCode(this._values);
      }
      _def(e, t, r, i) {
        const u = this._scope.toName(t);
        return r !== void 0 && i && (this._constants[u.str] = r), this._leafNode(new G(e, u, r)), u;
      }
      // `const` declaration (`var` in es5 mode)
      const(e, t, r) {
        return this._def(h.varKinds.const, e, t, r);
      }
      // `let` declaration with optional assignment (`var` in es5 mode)
      let(e, t, r) {
        return this._def(h.varKinds.let, e, t, r);
      }
      // `var` declaration with optional assignment
      var(e, t, r) {
        return this._def(h.varKinds.var, e, t, r);
      }
      // assignment code
      assign(e, t, r) {
        return this._leafNode(new O(e, t, r));
      }
      // `+=` code
      add(e, t) {
        return this._leafNode(new F(e, s.operators.ADD, t));
      }
      // appends passed SafeExpr to code or executes Block
      code(e) {
        return typeof e == "function" ? e() : e !== o.nil && this._leafNode(new D(e)), this;
      }
      // returns code for object literal for the passed argument list of key-value pairs
      object(...e) {
        const t = ["{"];
        for (const [r, i] of e)
          t.length > 1 && t.push(","), t.push(r), (r !== i || this.opts.es5) && (t.push(":"), (0, o.addCodeArg)(t, i));
        return t.push("}"), new o._Code(t);
      }
      // `if` clause (or statement if `thenBody` and, optionally, `elseBody` are passed)
      if(e, t, r) {
        if (this._blockNode(new a(e)), t && r)
          this.code(t).else().code(r).endIf();
        else if (t)
          this.code(t).endIf();
        else if (r)
          throw new Error('CodeGen: "else" body without "then" body');
        return this;
      }
      // `else if` clause - invalid without `if` or after `else` clauses
      elseIf(e) {
        return this._elseNode(new a(e));
      }
      // `else` clause - only valid after `if` or `else if` clauses
      else() {
        return this._elseNode(new g());
      }
      // end `if` statement (needed if gen.if was used only with condition)
      endIf() {
        return this._endBlockNode(a, g);
      }
      _for(e, t) {
        return this._blockNode(e), t && this.code(t).endFor(), this;
      }
      // a generic `for` clause (or statement if `forBody` is passed)
      for(e, t) {
        return this._for(new I(e), t);
      }
      // `for` statement for a range of values
      forRange(e, t, r, i, u = this.opts.es5 ? h.varKinds.var : h.varKinds.let) {
        const d = this._scope.toName(e);
        return this._for(new L(u, d, t, r), () => i(d));
      }
      // `for-of` statement (in es5 mode replace with a normal for loop)
      forOf(e, t, r, i = h.varKinds.const) {
        const u = this._scope.toName(e);
        if (this.opts.es5) {
          const d = t instanceof o.Name ? t : this.var("_arr", t);
          return this.forRange("_i", 0, (0, o._)`${d}.length`, (X) => {
            this.var(u, (0, o._)`${d}[${X}]`), r(u);
          });
        }
        return this._for(new S("of", i, u, t), () => r(u));
      }
      // `for-in` statement.
      // With option `ownProperties` replaced with a `for-of` loop for object keys
      forIn(e, t, r, i = this.opts.es5 ? h.varKinds.var : h.varKinds.const) {
        if (this.opts.ownProperties)
          return this.forOf(e, (0, o._)`Object.keys(${t})`, r);
        const u = this._scope.toName(e);
        return this._for(new S("in", i, u, t), () => r(u));
      }
      // end `for` loop
      endFor() {
        return this._endBlockNode(p);
      }
      // `label` statement
      label(e) {
        return this._leafNode(new R(e));
      }
      // `break` statement
      break(e) {
        return this._leafNode(new V(e));
      }
      // `return` statement
      return(e) {
        const t = new $();
        if (this._blockNode(t), this.code(e), t.nodes.length !== 1)
          throw new Error('CodeGen: "return" should have one node');
        return this._endBlockNode($);
      }
      // `try` statement
      try(e, t, r) {
        if (!t && !r)
          throw new Error('CodeGen: "try" without "catch" and "finally"');
        const i = new Q();
        if (this._blockNode(i), this.code(e), t) {
          const u = this.name("e");
          this._currNode = i.catch = new C(u), t(u);
        }
        return r && (this._currNode = i.finally = new y(), this.code(r)), this._endBlockNode(C, y);
      }
      // `throw` statement
      throw(e) {
        return this._leafNode(new q(e));
      }
      // start self-balancing block
      block(e, t) {
        return this._blockStarts.push(this._nodes.length), e && this.code(e).endBlock(t), this;
      }
      // end the current self-balancing block
      endBlock(e) {
        const t = this._blockStarts.pop();
        if (t === void 0)
          throw new Error("CodeGen: not in self-balancing block");
        const r = this._nodes.length - t;
        if (r < 0 || e !== void 0 && r !== e)
          throw new Error(`CodeGen: wrong number of nodes: ${r} vs ${e} expected`);
        return this._nodes.length = t, this;
      }
      // `function` heading (or definition if funcBody is passed)
      func(e, t = o.nil, r, i) {
        return this._blockNode(new v(e, t, r)), i && this.code(i).endFunc(), this;
      }
      // end function definition
      endFunc() {
        return this._endBlockNode(v);
      }
      optimize(e = 1) {
        for (; e-- > 0; )
          this._root.optimizeNodes(), this._root.optimizeNames(this._root.names, this._constants);
      }
      _leafNode(e) {
        return this._currNode.nodes.push(e), this;
      }
      _blockNode(e) {
        this._currNode.nodes.push(e), this._nodes.push(e);
      }
      _endBlockNode(e, t) {
        const r = this._currNode;
        if (r instanceof e || t && r instanceof t)
          return this._nodes.pop(), this;
        throw new Error(`CodeGen: not in block "${t ? `${e.kind}/${t.kind}` : e.kind}"`);
      }
      _elseNode(e) {
        const t = this._currNode;
        if (!(t instanceof a))
          throw new Error('CodeGen: "else" without "if"');
        return this._currNode = t.else = e, this;
      }
      get _root() {
        return this._nodes[0];
      }
      get _currNode() {
        const e = this._nodes;
        return e[e.length - 1];
      }
      set _currNode(e) {
        const t = this._nodes;
        t[t.length - 1] = e;
      }
    }
    s.CodeGen = B;
    function m(n, e) {
      for (const t in e)
        n[t] = (n[t] || 0) + (e[t] || 0);
      return n;
    }
    function w(n, e) {
      return e instanceof o._CodeOrName ? m(n, e.names) : n;
    }
    function _(n, e, t) {
      if (n instanceof o.Name)
        return r(n);
      if (!i(n))
        return n;
      return new o._Code(n._items.reduce((u, d) => (d instanceof o.Name && (d = r(d)), d instanceof o._Code ? u.push(...d._items) : u.push(d), u), []));
      function r(u) {
        const d = t[u.str];
        return d === void 0 || e[u.str] !== 1 ? u : (delete e[u.str], d);
      }
      function i(u) {
        return u instanceof o._Code && u._items.some((d) => d instanceof o.Name && e[d.str] === 1 && t[d.str] !== void 0);
      }
    }
    function M(n, e) {
      for (const t in e)
        n[t] = (n[t] || 0) - (e[t] || 0);
    }
    function K(n) {
      return typeof n == "boolean" || typeof n == "number" || n === null ? !n : (0, o._)`!${k(n)}`;
    }
    s.not = K;
    const H = P(s.operators.AND);
    function J(...n) {
      return n.reduce(H);
    }
    s.and = J;
    const U = P(s.operators.OR);
    function W(...n) {
      return n.reduce(U);
    }
    s.or = W;
    function P(n) {
      return (e, t) => e === o.nil ? t : t === o.nil ? e : (0, o._)`${k(e)} ${n} ${k(t)}`;
    }
    function k(n) {
      return n instanceof o.Name ? n : (0, o._)`(${n})`;
    }
  }(z), z);
}
export {
  ee as __require
};
//# sourceMappingURL=index.js.map
