import { j as h, i as C, Q as d, o as k, L as y, c as U, C as q } from "./css-D6ZBcSwh.js";
import { z as _ } from "zod";
import { j as x, C as z, B as K, k as W, l as H, L as V } from "./provider-C8In649C.js";
import { a as G } from "./dimensions-BFCMI7yN.js";
const Z = _.object({
  include: _.string().array().optional(),
  exclude: _.string().array().optional(),
  level: _.enum(x.LOG_LEVELS).optional().default("info")
}), L = "alamos-provider";
class v extends z {
  static TYPE = "alamos.Provider";
  schema = Z;
  afterUpdate(t) {
    if (t.getOptional(L) != null) return;
    const s = [], { include: i, exclude: r, level: a } = this.state;
    (i != null || r != null) && s.push(x.logLevelKeyFiler({ include: i, exclude: r })), a != null && s.push(x.logThresholdFilter(a)), s.length === 0 && s.push(() => !1), this.internal.ins = new x.Instrumentation({
      key: "aether",
      logger: new x.Logger({ filters: s })
    }), t.set(L, this.internal.ins);
  }
}
const J = (l, t) => {
  const e = l.get(L);
  if (e == null) throw new Error("No instrumentation provider");
  return t == null ? e : e.child(t);
}, pt = {
  [v.TYPE]: v
}, T = (l, t) => h.construct(
  h.left(l) - t.x,
  h.top(l) - t.y,
  h.width(l) + t.x * 2,
  h.height(l) + t.y * 2
);
class R {
  scale_;
  wrapped;
  atlasRegistry;
  cachedFont = null;
  cachedFillStyle = null;
  cachedStrokeStyle = null;
  cachedLineWidth = null;
  cachedGlobalAlpha = null;
  cachedTextAlign = null;
  cachedTextBaseline = null;
  cachedLineCap = null;
  cachedLineJoin = null;
  cachedMiterLimit = null;
  dpr;
  constructor(t, e, s, i = C.XY.IDENTITY) {
    this.wrapped = t, this.scale_ = i, this.atlasRegistry = e, this.dpr = s;
  }
  get fontStretch() {
    return this.wrapped.fontStretch;
  }
  set fontStretch(t) {
    this.wrapped.fontStretch = t;
  }
  get fontVariantCaps() {
    return this.wrapped.fontVariantCaps;
  }
  set fontVariantCaps(t) {
    this.wrapped.fontVariantCaps = t;
  }
  isContextLost() {
    return this.wrapped.isContextLost();
  }
  get wordSpacing() {
    return this.wrapped.wordSpacing;
  }
  set wordSpacing(t) {
    this.wrapped.wordSpacing = t;
  }
  get letterSpacing() {
    return this.wrapped.letterSpacing;
  }
  set letterSpacing(t) {
    this.wrapped.letterSpacing = t;
  }
  get textRendering() {
    return this.wrapped.textRendering;
  }
  set textRendering(t) {
    this.wrapped.textRendering = t;
  }
  checkAtlasFillStyle(t = !1) {
    return t && typeof this.fillStyle == "string" ? [!0, this.fillStyle] : (t && console.warn(
      "attempted to use a text atlas with a gradient fill style. This is not supported. Falling back to default canvas fill."
    ), [!1, null]);
  }
  reset() {
    this.wrapped.reset();
  }
  applyScale(t) {
    return new R(
      this,
      this.atlasRegistry,
      this.dpr,
      t
    );
  }
  get canvas() {
    return this.wrapped.canvas;
  }
  get miterLimit() {
    return this.cachedMiterLimit ?? this.wrapped.miterLimit;
  }
  set miterLimit(t) {
    const e = this.scale_.x.dim(t);
    e !== this.cachedMiterLimit && (this.cachedMiterLimit = e, this.wrapped.miterLimit = e);
  }
  get globalAlpha() {
    return this.cachedGlobalAlpha ?? this.wrapped.globalAlpha;
  }
  set globalAlpha(t) {
    t !== this.cachedGlobalAlpha && (this.cachedGlobalAlpha = t, this.wrapped.globalAlpha = t);
  }
  get globalCompositeOperation() {
    return this.wrapped.globalCompositeOperation;
  }
  set globalCompositeOperation(t) {
    this.wrapped.globalCompositeOperation = t;
  }
  get fillStyle() {
    return this.cachedFillStyle ?? this.wrapped.fillStyle;
  }
  set fillStyle(t) {
    t !== this.cachedFillStyle && (this.cachedFillStyle = t, this.wrapped.fillStyle = t);
  }
  get strokeStyle() {
    return this.cachedStrokeStyle ?? this.wrapped.strokeStyle;
  }
  set strokeStyle(t) {
    t !== this.cachedStrokeStyle && (this.cachedStrokeStyle = t, this.wrapped.strokeStyle = t);
  }
  drawImage(t, e, s, i, r, a, n, o, p) {
    this.wrapped.drawImage(
      t,
      e,
      s,
      i,
      r,
      a != null ? this.scale_.x.pos(a) : a,
      n != null ? this.scale_.y.pos(n) : n,
      o != null ? this.scale_.x.dim(o) : o,
      p != null ? this.scale_.y.dim(p) : p
    );
  }
  beginPath() {
    this.wrapped.beginPath();
  }
  clip(t, e) {
    this.wrapped.clip(t, e);
  }
  fill(t, e) {
    if (t == null) return this.wrapped.fill();
    this.wrapped.fill(t, e);
  }
  isPointInPath(t, e, s, i) {
    return this.wrapped.isPointInPath(t, e, s, i);
  }
  isPointInStroke(t, e, s) {
    return this.wrapped.isPointInStroke(t, e, s);
  }
  stroke(t) {
    if (t == null) return this.wrapped.stroke();
    this.wrapped.stroke(t);
  }
  createConicGradient(t, e, s) {
    return this.wrapped.createConicGradient(t, e, s);
  }
  createLinearGradient(t, e, s, i) {
    return this.wrapped.createLinearGradient(t, e, s, i);
  }
  createPattern(t, e) {
    return this.wrapped.createPattern(t, e);
  }
  createRadialGradient(t, e, s, i, r, a) {
    return this.wrapped.createRadialGradient(t, e, s, i, r, a);
  }
  get filter() {
    return this.wrapped.filter;
  }
  set filter(t) {
    this.wrapped.filter = t;
  }
  createImageData(t, e, s) {
    return this.wrapped.createImageData(t, e, s);
  }
  getImageData(t, e, s, i, r) {
    return this.wrapped.getImageData(t, e, s, i, r);
  }
  putImageData(t, e, s, i, r, a, n) {
    this.wrapped.putImageData(
      // @ts-expect-error - typescript overloads cause issues here
      t,
      e,
      s,
      i,
      r,
      a,
      n
    );
  }
  get imageSmoothingEnabled() {
    return this.wrapped.imageSmoothingEnabled;
  }
  set imageSmoothingEnabled(t) {
    this.wrapped.imageSmoothingEnabled = t;
  }
  get imageSmoothingQuality() {
    return this.wrapped.imageSmoothingQuality;
  }
  set imageSmoothingQuality(t) {
    this.wrapped.imageSmoothingQuality = t;
  }
  arc(t, e, s, i, r, a) {
    this.wrapped.arc(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      i,
      r,
      a
    );
  }
  arcTo(t, e, s, i, r) {
    this.wrapped.arcTo(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.pos(s),
      this.scale_.y.pos(i),
      this.scale_.x.dim(r)
    );
  }
  bezierCurveTo(t, e, s, i, r, a) {
    this.wrapped.bezierCurveTo(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.pos(s),
      this.scale_.y.pos(i),
      this.scale_.x.pos(r),
      this.scale_.y.pos(a)
    );
  }
  closePath() {
    this.wrapped.closePath();
  }
  ellipse(t, e, s, i, r, a, n, o) {
    this.wrapped.ellipse(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i),
      r,
      a,
      n,
      o
    );
  }
  lineTo(t, e) {
    this.wrapped.lineTo(this.scale_.x.pos(t), this.scale_.y.pos(e));
  }
  moveTo(t, e) {
    this.wrapped.moveTo(this.scale_.x.pos(t), this.scale_.y.pos(e));
  }
  quadraticCurveTo(t, e, s, i) {
    this.wrapped.quadraticCurveTo(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.pos(s),
      this.scale_.y.pos(i)
    );
  }
  rect(t, e, s, i) {
    this.wrapped.rect(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i)
    );
  }
  roundRect(t, e, s, i, r) {
    this.wrapped.roundRect(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i),
      typeof r == "number" ? this.scale_.x.dim(r) : r
    );
  }
  get lineCap() {
    return this.cachedLineCap ?? this.wrapped.lineCap;
  }
  set lineCap(t) {
    t !== this.cachedLineCap && (this.cachedLineCap = t, this.wrapped.lineCap = t);
  }
  get lineDashOffset() {
    return this.wrapped.lineDashOffset;
  }
  set lineDashOffset(t) {
    this.wrapped.lineDashOffset = this.scale_.x.dim(t);
  }
  get lineJoin() {
    return this.cachedLineJoin ?? this.wrapped.lineJoin;
  }
  set lineJoin(t) {
    t !== this.cachedLineJoin && (this.cachedLineJoin = t, this.wrapped.lineJoin = t);
  }
  get lineWidth() {
    return this.cachedLineWidth ?? this.wrapped.lineWidth;
  }
  set lineWidth(t) {
    const e = this.scale_.x.dim(t);
    e !== this.cachedLineWidth && (this.cachedLineWidth = e, this.wrapped.lineWidth = e);
  }
  getLineDash() {
    return this.wrapped.getLineDash();
  }
  setLineDash(t) {
    const e = Array.from(t).map((s) => this.scale_.x.dim(s));
    this.wrapped.setLineDash(e);
  }
  clearRect(t, e, s, i) {
    this.wrapped.clearRect(t, e, s, i);
  }
  fillRect(t, e, s, i) {
    this.wrapped.fillRect(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i)
    );
  }
  strokeRect(t, e, s, i) {
    this.wrapped.strokeRect(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i)
    );
  }
  get shadowBlur() {
    return this.wrapped.shadowBlur;
  }
  set shadowBlur(t) {
    this.wrapped.shadowBlur = t;
  }
  get shadowColor() {
    return this.wrapped.shadowColor;
  }
  set shadowColor(t) {
    this.wrapped.shadowColor = t;
  }
  get shadowOffsetX() {
    return this.wrapped.shadowOffsetX;
  }
  set shadowOffsetX(t) {
    this.wrapped.shadowOffsetX = t;
  }
  get shadowOffsetY() {
    return this.wrapped.shadowOffsetY;
  }
  restore() {
    this.cachedFillStyle = null, this.cachedStrokeStyle = null, this.cachedLineWidth = null, this.cachedGlobalAlpha = null, this.cachedTextAlign = null, this.cachedTextBaseline = null, this.cachedLineCap = null, this.cachedLineJoin = null, this.cachedMiterLimit = null, this.cachedFont = null, this.wrapped.restore();
  }
  save() {
    this.wrapped.save();
  }
  fillText(t, e, s, i, r = {}) {
    const [a, n] = this.checkAtlasFillStyle(r.useAtlas);
    if (a) {
      this.atlasRegistry.get({ font: this.font, textColor: n }).fillText(this, t, e, s);
      return;
    }
    this.wrapped.fillText(
      t,
      this.scale_.x.pos(e),
      this.scale_.y.pos(s),
      i != null ? this.scale_.x.dim(i) : void 0
    );
  }
  measureText(t) {
    return this.wrapped.measureText(t);
  }
  textDimensions(t, e = {}) {
    const [s, i] = this.checkAtlasFillStyle(e.useAtlas);
    return s ? this.atlasRegistry.get({ font: this.font, textColor: i }).measureText(t) : G(this.measureText(t));
  }
  strokeText(t, e, s, i) {
    this.wrapped.strokeText(
      t,
      this.scale_.x.pos(e),
      this.scale_.y.pos(s),
      i != null ? this.scale_.x.dim(i) : void 0
    );
  }
  get direction() {
    return this.wrapped.direction;
  }
  set direction(t) {
    this.wrapped.direction = t;
  }
  get font() {
    return this.cachedFont != null ? this.cachedFont : this.wrapped.font;
  }
  set font(t) {
    t !== this.cachedFont && (this.cachedFont = t, this.wrapped.font = this.cachedFont);
  }
  get fontKerning() {
    return this.wrapped.fontKerning;
  }
  set fontKerning(t) {
    this.wrapped.fontKerning = t;
  }
  get textAlign() {
    return this.cachedTextAlign ?? this.wrapped.textAlign;
  }
  set textAlign(t) {
    t !== this.cachedTextAlign && (this.cachedTextAlign = t, this.wrapped.textAlign = t);
  }
  get textBaseline() {
    return this.cachedTextBaseline ?? this.wrapped.textBaseline;
  }
  set textBaseline(t) {
    t !== this.cachedTextBaseline && (this.cachedTextBaseline = t, this.wrapped.textBaseline = t);
  }
  getTransform() {
    return this.wrapped.getTransform();
  }
  resetTransform() {
    this.wrapped.resetTransform();
  }
  rotate(t) {
    this.wrapped.rotate(t);
  }
  scale(t, e) {
    this.wrapped.scale(t, e), this.dpr = t;
  }
  scissor(t, e = d.ZERO) {
    const s = new $(this.scale_);
    return t = T(t, e), s.rect(...d.couple(h.topLeft(t)), ...k.couple(h.dims(t))), this.save(), this.clip(s.getPath()), () => this.restore();
  }
  setTransform(t, e, s, i, r, a) {
    this.wrapped.setTransform(t, e, s, i, r, a);
  }
  transform(t, e, s, i, r, a) {
    this.wrapped.transform(t, e, s, i, r, a);
  }
  translate(t, e) {
    this.wrapped.translate(t, e);
  }
}
class $ {
  scale_;
  path;
  constructor(t = C.XY.IDENTITY, e) {
    this.scale_ = t, e instanceof Path2D || typeof e == "string" ? this.path = new Path2D(e) : this.path = new Path2D();
  }
  addPath(t, e) {
    this.path.addPath(t, e);
  }
  arc(t, e, s, i, r, a) {
    this.path.arc(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      i,
      r,
      a
    );
  }
  arcTo(t, e, s, i, r) {
    this.path.arcTo(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.pos(s),
      this.scale_.y.pos(i),
      this.scale_.x.dim(r)
    );
  }
  bezierCurveTo(t, e, s, i, r, a) {
    this.path.bezierCurveTo(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.pos(s),
      this.scale_.y.pos(i),
      this.scale_.x.pos(r),
      this.scale_.y.pos(a)
    );
  }
  closePath() {
    this.path.closePath();
  }
  ellipse(t, e, s, i, r, a, n, o) {
    this.path.ellipse(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i),
      r,
      a,
      n,
      o
    );
  }
  lineTo(t, e) {
    this.path.lineTo(this.scale_.x.pos(t), this.scale_.y.pos(e));
  }
  moveTo(t, e) {
    this.path.moveTo(this.scale_.x.pos(t), this.scale_.y.pos(e));
  }
  quadraticCurveTo(t, e, s, i) {
    this.path.quadraticCurveTo(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.pos(s),
      this.scale_.y.pos(i)
    );
  }
  rect(t, e, s, i) {
    this.path.rect(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i)
    );
  }
  roundRect(t, e, s, i, r) {
    const a = this.scaleRadii(r);
    this.path.roundRect(
      this.scale_.x.pos(t),
      this.scale_.y.pos(e),
      this.scale_.x.dim(s),
      this.scale_.y.dim(i),
      a
    );
  }
  scaleRadii(t) {
    return t == null ? t : typeof t == "number" ? this.scale_.x.dim(t) : Array.isArray(t) ? t.map((e) => this.scaleRadius(e)) : this.scaleRadius(t);
  }
  scaleRadius(t) {
    return typeof t == "number" ? this.scale_.x.dim(t) : {
      x: this.scale_.x.dim(t.x ?? 0),
      y: this.scale_.y.dim(t.y ?? 0)
    };
  }
  getPath() {
    return this.path;
  }
}
const j = `precision mediump float;

void main(void) {
    gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);    
}
`, Q = `attribute vec4 a_position;

void main(void) {
  gl_Position = a_position;
}
`, D = (l) => new Error(`failed to compile webgl program: ${l}`), tt = new Error("null shader encountered");
let et = class {
  /** The render context used by this program. */
  renderCtx;
  /** The underlying webgl program. */
  prog;
  /** The code for the vertex shader. */
  vertShader;
  /** The code for the fragment shader. */
  fragShader;
  uniformLocCache = /* @__PURE__ */ new Map();
  /**
   * @constructor compiles the given vertex and fragment shaders under the given
   * render context into a program.
   *
   * @param ctx - The render context to use.
   * @param vertShader - The vertex shader code.
   * @param fragShader - The fragment shader code.
   */
  constructor(t, e, s) {
    this.renderCtx = t;
    const i = t.gl.createProgram();
    if (i == null) throw D("failed to create program");
    this.prog = i, this.vertShader = e, this.fragShader = s, this.compile();
  }
  /** Sets the current program as the active program used by the context. */
  setAsActive() {
    return this.renderCtx.gl.useProgram(this.prog), () => this.renderCtx.gl.useProgram(null);
  }
  /**
   * Sets a uniform XY coordinate value.
   *
   * @param name - The name of the uniform.
   * @param value - The value to set.
   */
  uniformXY(t, e) {
    this.renderCtx.gl.uniform2fv(this.getUniformLoc(t), d.couple(e));
  }
  /**
   * Sets a uniform color value.
   *
   * @param name - The name of the uniform.
   * @param value - The value to set.
   */
  uniformColor(t, e) {
    this.renderCtx.gl.uniform4fv(this.getUniformLoc(t), y.rgba1(e));
  }
  getUniformLoc(t) {
    const e = this.uniformLocCache.get(t);
    if (e != null) return e;
    const s = this.renderCtx.gl.getUniformLocation(this.prog, t);
    if (s == null) throw new Error(`unexpected missing uniform ${t}`);
    return this.uniformLocCache.set(t, s), s;
  }
  compile() {
    const t = this.renderCtx.gl;
    this.compileShader(this.vertShader, t.VERTEX_SHADER), this.compileShader(this.fragShader, t.FRAGMENT_SHADER), t.linkProgram(this.prog);
  }
  compileShader(t, e) {
    const s = this.renderCtx.gl, i = s.createShader(e);
    if (i == null) throw tt;
    if (s.shaderSource(i, t), s.compileShader(i), !s.getShaderParameter(i, s.COMPILE_STATUS)) {
      const a = s.getShaderInfoLog(i);
      throw s.deleteShader(i), D(a ?? "unknown");
    }
    s.attachShader(this.prog, i);
  }
};
const st = new Float32Array([0, 0, 0, 0, 0, 0]);
class it extends et {
  positionBuffer;
  constructor(t) {
    super(t, Q, j);
    const e = t.gl.createBuffer();
    if (e == null) throw new K("webgl: failed to create buffer");
    this.positionBuffer = e, t.gl.bindBuffer(t.gl.ARRAY_BUFFER, this.positionBuffer), t.gl.bufferData(t.gl.ARRAY_BUFFER, st, t.gl.STATIC_DRAW);
  }
  exec() {
    const { gl: t } = this.renderCtx, e = t.getAttribLocation(this.prog, "a_position");
    t.bindBuffer(t.ARRAY_BUFFER, this.positionBuffer);
    const s = t.createVertexArray();
    t.bindVertexArray(s), t.enableVertexAttribArray(e), t.vertexAttribPointer(e, 2, t.FLOAT, !1, 0, 0), this.setAsActive(), t.drawArrays(t.TRIANGLES, 0, 3);
  }
}
const P = { high: 1, low: 0 };
class rt {
  /** Stores the current requests for rendering. */
  requests = /* @__PURE__ */ new Map();
  /** Stores render cleanup functions for clearing canvases and other resources. */
  cleanup = /* @__PURE__ */ new Map();
  /** A callback to run after each render call. */
  afterRender;
  /** Instrumentation for logging, tracing, metrics, etc. */
  instrumentation;
  /** A function to add status to the status bar. */
  handleError;
  constructor({
    afterRender: t,
    instrumentation: e = x.Instrumentation.NOOP,
    handleError: s
  }) {
    this.afterRender = t, this.instrumentation = e, this.handleError = s, this.start();
  }
  /**
   * Sets a new request in the queue according to a set of rules:
   *
   * 1. If no request with the same key exists, add the request to the queue.
   * 2. If a request with the same key exists, replace it if the new request has a
   * greater or equal priority and a greater or equal number of canvases that are
   * being rendered to.
   *
   * @param req - The request to set.
   */
  set(t) {
    const e = this.requests.get(t.key);
    if (e == null) this.requests.set(t.key, t);
    else {
      const s = P[t.priority] >= P[e.priority], i = t.canvases.length >= e.canvases.length;
      s && i && this.requests.set(t.key, t);
    }
  }
  /** Execute the render. */
  render() {
    const { requests: t } = this;
    t.size !== 0 && (this.runCleanupsSync(), this.renderSync(), this.requests.clear(), this.afterRender?.());
  }
  runCleanupsSync() {
    const { cleanup: t, requests: e } = this;
    t.forEach((s, i) => {
      const r = e.get(i);
      r != null && (s(r), t.delete(i));
    });
  }
  renderSync() {
    const { requests: t } = this;
    t.forEach((e) => {
      try {
        const s = e.render();
        s != null && this.cleanup.set(e.key, s);
      } catch (s) {
        this.handleError(s, "render loop failed");
      }
    });
  }
  /** Starts the rendering loop. */
  start() {
    const t = () => {
      try {
        this.render();
      } catch (e) {
        this.handleError(e, "render loop failed");
      }
      requestAnimationFrame(t);
    };
    requestAnimationFrame(t);
  }
}
const S = 2, g = 2;
class b {
  // A canvas buffer that holds rendered characters.
  atlas;
  // Cached dimensions of a character.
  charDims;
  // A map of characters to their index in the atlas.
  charMap;
  // The default characters to include in the atlas.
  static DEFAULT_CHARS = "0123456789.:-µmsNa∞ᴇABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%";
  constructor(t) {
    const { font: e, characters: s = b.DEFAULT_CHARS, textColor: i } = t;
    this.charMap = /* @__PURE__ */ new Map();
    const r = U.unique(Array.from(s)), n = new OffscreenCanvas(1, 1).getContext("2d");
    n.font = e;
    const o = n.measureText("0");
    this.charDims = G(o), this.charDims.width += S, this.charDims.height += S;
    const p = r.length, u = this.charDims.width, m = this.charDims.height, c = Math.ceil(Math.sqrt(p)), E = Math.ceil(p / c);
    this.atlas = new OffscreenCanvas(
      u * c * g,
      m * (E + 1) * g
    );
    const f = this.atlas.getContext("2d");
    f.scale(g, g), f.font = e, f.textBaseline = "alphabetic", f.textAlign = "left", f.fillStyle = y.hex(i), f.clearRect(0, 0, this.atlas.width, this.atlas.height), r.forEach((O, A) => {
      const M = A % c, Y = Math.floor(A / c), N = M * u, X = (Y + 1) * m;
      f.fillText(O, N, X), this.charMap.set(O, A);
    });
  }
  fillText(t, e, s, i) {
    const { width: r, height: a } = this.charDims, n = Math.ceil(Math.sqrt(this.charMap.size));
    t.textAlign === "center" && (s -= r * e.length / 2), t.textBaseline === "middle" && (i += a / 2);
    for (let o = 0; o < e.length; o++) {
      const p = e[o], u = this.charMap.get(p);
      if (u === void 0) continue;
      const m = u % n, c = Math.floor(u / n);
      t.drawImage(
        this.atlas,
        m * r * g,
        c * a * g + S,
        r * g,
        a * g,
        s + o * r,
        i - a - S / g,
        r,
        a
      );
    }
  }
  measureText(t) {
    return { width: t.length * this.charDims.width, height: this.charDims.height };
  }
}
class at {
  atlases;
  constructor() {
    this.atlases = /* @__PURE__ */ new Map();
  }
  /**
   * @returns at atlas from the registry compatible with the given props. If the
   * atlas does not exist in the registry, it is created and added to the registry.
   */
  get(t) {
    const e = `${t.font}-${y.hex(t.textColor)}-${t.characters}`;
    if (this.atlases.has(e)) return this.atlases.get(e);
    const s = new b(t);
    return this.atlases.set(e, s), s;
  }
}
const F = (l) => (l.textRendering = "optimizeSpeed", l.imageSmoothingEnabled = !1, l);
class w {
  /* The canvas element used by WebGL. */
  glCanvas;
  /** The canvas element used by the 2D canvas. */
  upper2dCanvas;
  /** The canvas element used by the 2D canvas. */
  lower2dCanvas;
  /** The WebGL rendering context.  */
  gl;
  /** A 2D canvas that sits below the WebGL canvas. */
  lower2d;
  /** A 2D canvas that sits above the WebGL canvas. */
  upper2d;
  /** The region the canvas occupies in pixel space */
  region;
  /** The device pixel ratio of the canvas */
  dpr;
  /** queue render transitions onto the stack */
  loop;
  /** See the @link{clear.Program} for why this is necessary. */
  clearProgram;
  os;
  static CONTEXT_KEY = q.B("render-context");
  instrumentation;
  static create(t, e, s, i, r) {
    const a = J(t, "render_context"), n = new w(
      e,
      s,
      i,
      r,
      a,
      W(t),
      H(t)
    );
    return t.set(w.CONTEXT_KEY, n), n;
  }
  constructor(t, e, s, i, r, a, n) {
    this.upper2dCanvas = s, this.lower2dCanvas = e, this.glCanvas = t, this.os = i, this.instrumentation = r, this.dpr = 1;
    const o = this.lower2dCanvas.getContext("2d");
    if (o == null) throw new Error("Could not get 2D context");
    const p = new at();
    this.lower2d = F(
      new R(o, p, this.dpr)
    );
    const u = this.upper2dCanvas.getContext("2d");
    if (u == null) throw new Error("Could not get 2D context");
    this.upper2d = F(
      new R(u, p, this.dpr)
    );
    const m = {
      preserveDrawingBuffer: !0,
      powerPreference: "high-performance",
      stencil: !1,
      depth: !1
    }, c = this.glCanvas.getContext("webgl2", m);
    if (c == null) throw new Error("Could not get WebGL context");
    c.disable(c.DEPTH_TEST), c.disable(c.STENCIL_TEST), c.disable(c.CULL_FACE), c.disable(c.DITHER), this.gl = c, this.glCanvas.oncontextlost = () => a({
      variant: "error",
      message: "WebGL context lost"
    });
    const E = () => {
      this.gl.flush(), this.gl.finish();
    };
    this.loop = new rt({
      afterRender: E,
      instrumentation: this.instrumentation,
      handleError: n
    }), this.region = h.ZERO, this.os === "Windows" && (this.clearProgram = new it(this));
  }
  static useOptional(t) {
    return t.getOptional(w.CONTEXT_KEY);
  }
  static use(t) {
    return t.get(w.CONTEXT_KEY);
  }
  update(t) {
    t.set(w.CONTEXT_KEY, this);
  }
  /**
   * Resizes the canvas to the given region and device pixel ratio. Ensuring
   * that all drawing operations and viewports are scaled correctly.
   */
  resize(t, e) {
    h.equals(this.region, t) && this.dpr === e || (this.region = t, this.dpr = e, this.resizeCanvas(this.glCanvas), this.resizeCanvas(this.upper2dCanvas), this.resizeCanvas(this.lower2dCanvas), this.lower2d.scale(this.dpr, this.dpr), this.upper2d.scale(this.dpr, this.dpr), this.gl.viewport(0, 0, h.width(t) * e, h.height(t) * e));
  }
  resizeCanvas(t) {
    t.width = h.width(this.region) * this.dpr, t.height = h.height(this.region) * this.dpr;
  }
  /** @returns the aspect ratio of the canvas. */
  get aspect() {
    return h.aspect(this.region);
  }
  /**
   * Takes the given box in PIXEL space and produces a transform
   * in CLIP space representing the sub-region represented by the box
   * in the canvas.
   */
  scaleRegion(t) {
    return new C.XY(
      // Accept a value in decimal.
      C.Scale.scale(0, 1).scale(h.width(t)).translate(h.left(t)).reBound(h.width(this.region)).scale(-1, 1),
      // Accept a value in decimal.
      C.Scale.scale(0, 1).scale(h.height(t)).invert().translate(h.top(t)).reBound(h.height(this.region)).scale(-1, 1).invert()
    );
  }
  scissor(t, e = d.ZERO, s) {
    const i = [];
    return s.includes("upper2d") && i.push(this.upper2d.scissor(t, e)), s.includes("lower2d") && i.push(this.lower2d.scissor(t, e)), s.includes("gl") && i.push(this.scissorGL(t, e)), () => i.forEach((r) => r());
  }
  scissorGL(t, e = d.ZERO) {
    return this.gl.enable(this.gl.SCISSOR_TEST), t = T(t, e), this.gl.scissor(
      h.left(t) * this.dpr,
      (h.height(this.region) - h.bottom(t)) * this.dpr,
      h.width(t) * this.dpr,
      h.height(t) * this.dpr
    ), () => this.gl.disable(this.gl.SCISSOR_TEST);
  }
  erase(t, e = d.ZERO, ...s) {
    e = d.construct(e), s.length === 0 && (s = ["upper2d", "lower2d", "gl"]), s.includes("upper2d") && this.eraseCanvas(this.upper2d, t, e), s.includes("lower2d") && this.eraseCanvas(this.lower2d, t, e), s.includes("gl") && this.eraseGL(t, e);
  }
  eraseGL(t, e = d.ZERO) {
    const { gl: s } = this, i = this.scissorGL(T(t, e));
    s.clearColor(...y.rgba1(y.ZERO)), s.clear(s.COLOR_BUFFER_BIT), this.os === "Windows" && this.clearProgram?.exec(), i();
  }
  eraseCanvas(t, e, s = d.ZERO) {
    const i = T(e, s);
    t.clearRect(...d.couple(h.topLeft(i)), ...k.couple(h.dims(i)));
  }
}
const ht = _.object({
  region: h.box,
  enabled: _.boolean().optional().default(!1)
}), I = ["gl", "lower2d", "upper2d"];
class B extends V {
  static TYPE = "eraser";
  schema = ht;
  afterUpdate(t) {
    this.deleted || (this.internal.renderCtx = w.use(t), this.renderOnLifecycleChange());
  }
  afterDelete() {
    this.renderOnLifecycleChange();
  }
  renderOnLifecycleChange() {
    this.internal.renderCtx.loop.set({
      key: `${this.type}-${this.key}`,
      render: this.render.bind(this),
      priority: "high",
      canvases: I
    });
  }
  render() {
    this.deleted || !this.state.enabled || this.internal.renderCtx.erase(this.state.region, d.construct(0), ...I);
  }
}
const ut = {
  [B.TYPE]: B
};
export {
  w as C,
  B as E,
  et as G,
  rt as L,
  v as P,
  ut as R,
  pt as a,
  ht as e,
  Z as p,
  J as u
};
