import { __exports as v } from "../../../../../../../../_virtual/scope.js";
import { __require as V } from "./code.js";
var m;
function G() {
  return m ? v : (m = 1, function(r) {
    Object.defineProperty(r, "__esModule", { value: !0 }), r.ValueScope = r.ValueScopeName = r.Scope = r.varKinds = r.UsedValueState = void 0;
    const n = V();
    class w extends Error {
      constructor(e) {
        super(`CodeGen: "code" for ${e} not defined`), this.value = e.value;
      }
    }
    var _;
    (function(u) {
      u[u.Started = 0] = "Started", u[u.Completed = 1] = "Completed";
    })(_ || (r.UsedValueState = _ = {})), r.varKinds = {
      const: new n.Name("const"),
      let: new n.Name("let"),
      var: new n.Name("var")
    };
    class p {
      constructor({ prefixes: e, parent: t } = {}) {
        this._names = {}, this._prefixes = e, this._parent = t;
      }
      toName(e) {
        return e instanceof n.Name ? e : this.name(e);
      }
      name(e) {
        return new n.Name(this._newName(e));
      }
      _newName(e) {
        const t = this._names[e] || this._nameGroup(e);
        return `${e}${t.index++}`;
      }
      _nameGroup(e) {
        var t, s;
        if (!((s = (t = this._parent) === null || t === void 0 ? void 0 : t._prefixes) === null || s === void 0) && s.has(e) || this._prefixes && !this._prefixes.has(e))
          throw new Error(`CodeGen: prefix "${e}" is not allowed in this scope`);
        return this._names[e] = { prefix: e, index: 0 };
      }
    }
    r.Scope = p;
    class f extends n.Name {
      constructor(e, t) {
        super(t), this.prefix = e;
      }
      setValue(e, { property: t, itemIndex: s }) {
        this.value = e, this.scopePath = (0, n._)`.${new n.Name(t)}[${s}]`;
      }
    }
    r.ValueScopeName = f;
    const $ = (0, n._)`\n`;
    class N extends p {
      constructor(e) {
        super(e), this._values = {}, this._scope = e.scope, this.opts = { ...e, _n: e.lines ? $ : n.nil };
      }
      get() {
        return this._scope;
      }
      name(e) {
        return new f(e, this._newName(e));
      }
      value(e, t) {
        var s;
        if (t.ref === void 0)
          throw new Error("CodeGen: ref must be passed in value");
        const o = this.toName(e), { prefix: i } = o, h = (s = t.key) !== null && s !== void 0 ? s : t.ref;
        let c = this._values[i];
        if (c) {
          const l = c.get(h);
          if (l)
            return l;
        } else
          c = this._values[i] = /* @__PURE__ */ new Map();
        c.set(h, o);
        const d = this._scope[i] || (this._scope[i] = []), a = d.length;
        return d[a] = t.ref, o.setValue(t, { property: i, itemIndex: a }), o;
      }
      getValue(e, t) {
        const s = this._values[e];
        if (s)
          return s.get(t);
      }
      scopeRefs(e, t = this._values) {
        return this._reduceValues(t, (s) => {
          if (s.scopePath === void 0)
            throw new Error(`CodeGen: name "${s}" has no value`);
          return (0, n._)`${e}${s.scopePath}`;
        });
      }
      scopeCode(e = this._values, t, s) {
        return this._reduceValues(e, (o) => {
          if (o.value === void 0)
            throw new Error(`CodeGen: name "${o}" has no value`);
          return o.value.code;
        }, t, s);
      }
      _reduceValues(e, t, s = {}, o) {
        let i = n.nil;
        for (const h in e) {
          const c = e[h];
          if (!c)
            continue;
          const d = s[h] = s[h] || /* @__PURE__ */ new Map();
          c.forEach((a) => {
            if (d.has(a))
              return;
            d.set(a, _.Started);
            let l = t(a);
            if (l) {
              const S = this.opts.es5 ? r.varKinds.var : r.varKinds.const;
              i = (0, n._)`${i}${S} ${a} = ${l};${this.opts._n}`;
            } else if (l = o?.(a))
              i = (0, n._)`${i}${l}${this.opts._n}`;
            else
              throw new w(a);
            d.set(a, _.Completed);
          });
        }
        return i;
      }
    }
    r.ValueScope = N;
  }(v), v);
}
export {
  G as __require
};
//# sourceMappingURL=scope.js.map
