var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { t as __commonJSMin } from "./chunk-BNovOVIE.js";
import { t as require_compiler_runtime } from "./compiler-runtime-B_OLMU9S.js";
import { l as createLucideIcon } from "./dist-D_UjpfOY.js";
import { g as Logger } from "./button-DNlNlZY_.js";
import { r as KnownQueryParams } from "./constants-CvyfaCvs.js";
import { f as waitFor, p as isIslands, u as store, y as atom } from "./useTheme-MWfxn4oz.js";
var CircleQuestionMark = createLucideIcon("circle-question-mark", [
  ["circle", {
    cx: "12",
    cy: "12",
    r: "10",
    key: "1mglay"
  }],
  ["path", {
    d: "M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3",
    key: "1u773s"
  }],
  ["path", {
    d: "M12 17h.01",
    key: "p32p05"
  }]
]);
function isWasm() {
  return typeof document < "u" && document.querySelector("marimo-wasm") !== null;
}
var Deferred = class {
  constructor() {
    __publicField(this, "status", "pending");
    __publicField(this, "value");
    this.promise = new Promise((e, m) => {
      this.reject = (e2) => {
        this.status = "rejected", m(e2);
      }, this.resolve = (m2) => {
        this.status = "resolved", isPromiseLike(m2) || (this.value = m2), e(m2);
      };
    });
  }
};
function isPromiseLike(e) {
  return typeof e == "object" && !!e && "then" in e && typeof e.then == "function";
}
const WebSocketState = {
  NOT_STARTED: "NOT_STARTED",
  CONNECTING: "CONNECTING",
  OPEN: "OPEN",
  CLOSING: "CLOSING",
  CLOSED: "CLOSED"
}, connectionAtom = atom({ state: WebSocketState.NOT_STARTED });
function waitForConnectionOpen() {
  return waitFor(connectionAtom, (e) => e.state === WebSocketState.OPEN);
}
atom((e) => e(connectionAtom).state === WebSocketState.CONNECTING), atom((e) => e(connectionAtom).state === WebSocketState.OPEN), atom((e) => {
  let m = e(connectionAtom);
  return m.state === WebSocketState.OPEN || m.state === WebSocketState.NOT_STARTED;
}), atom((e) => e(connectionAtom).state === WebSocketState.CLOSED), atom((e) => e(connectionAtom).state === WebSocketState.NOT_STARTED);
var require__u64 = /* @__PURE__ */ __commonJSMin(((e) => {
  Object.defineProperty(e, "__esModule", { value: true }), e.toBig = e.shrSL = e.shrSH = e.rotrSL = e.rotrSH = e.rotrBL = e.rotrBH = e.rotr32L = e.rotr32H = e.rotlSL = e.rotlSH = e.rotlBL = e.rotlBH = e.add5L = e.add5H = e.add4L = e.add4H = e.add3L = e.add3H = void 0, e.add = j, e.fromBig = g, e.split = _;
  var m = /* @__PURE__ */ BigInt(2 ** 32 - 1), h = /* @__PURE__ */ BigInt(32);
  function g(e2, g2 = false) {
    return g2 ? {
      h: Number(e2 & m),
      l: Number(e2 >> h & m)
    } : {
      h: Number(e2 >> h & m) | 0,
      l: Number(e2 & m) | 0
    };
  }
  function _(e2, m2 = false) {
    let h2 = e2.length, _2 = new Uint32Array(h2), v2 = new Uint32Array(h2);
    for (let y2 = 0; y2 < h2; y2++) {
      let { h: h3, l: b2 } = g(e2[y2], m2);
      [_2[y2], v2[y2]] = [h3, b2];
    }
    return [_2, v2];
  }
  var v = (e2, m2) => BigInt(e2 >>> 0) << h | BigInt(m2 >>> 0);
  e.toBig = v;
  var y = (e2, m2, h2) => e2 >>> h2;
  e.shrSH = y;
  var b = (e2, m2, h2) => e2 << 32 - h2 | m2 >>> h2;
  e.shrSL = b;
  var x = (e2, m2, h2) => e2 >>> h2 | m2 << 32 - h2;
  e.rotrSH = x;
  var S = (e2, m2, h2) => e2 << 32 - h2 | m2 >>> h2;
  e.rotrSL = S;
  var C = (e2, m2, h2) => e2 << 64 - h2 | m2 >>> h2 - 32;
  e.rotrBH = C;
  var w = (e2, m2, h2) => e2 >>> h2 - 32 | m2 << 64 - h2;
  e.rotrBL = w;
  var T = (e2, m2) => m2;
  e.rotr32H = T;
  var E = (e2, m2) => e2;
  e.rotr32L = E;
  var D = (e2, m2, h2) => e2 << h2 | m2 >>> 32 - h2;
  e.rotlSH = D;
  var O = (e2, m2, h2) => m2 << h2 | e2 >>> 32 - h2;
  e.rotlSL = O;
  var k = (e2, m2, h2) => m2 << h2 - 32 | e2 >>> 64 - h2;
  e.rotlBH = k;
  var A = (e2, m2, h2) => e2 << h2 - 32 | m2 >>> 64 - h2;
  e.rotlBL = A;
  function j(e2, m2, h2, g2) {
    let _2 = (m2 >>> 0) + (g2 >>> 0);
    return {
      h: e2 + h2 + (_2 / 2 ** 32 | 0) | 0,
      l: _2 | 0
    };
  }
  var M = (e2, m2, h2) => (e2 >>> 0) + (m2 >>> 0) + (h2 >>> 0);
  e.add3L = M;
  var N = (e2, m2, h2, g2) => m2 + h2 + g2 + (e2 / 2 ** 32 | 0) | 0;
  e.add3H = N;
  var P = (e2, m2, h2, g2) => (e2 >>> 0) + (m2 >>> 0) + (h2 >>> 0) + (g2 >>> 0);
  e.add4L = P;
  var F = (e2, m2, h2, g2, _2) => m2 + h2 + g2 + _2 + (e2 / 2 ** 32 | 0) | 0;
  e.add4H = F;
  var I = (e2, m2, h2, g2, _2) => (e2 >>> 0) + (m2 >>> 0) + (h2 >>> 0) + (g2 >>> 0) + (_2 >>> 0);
  e.add5L = I;
  var L = (e2, m2, h2, g2, _2, v2) => m2 + h2 + g2 + _2 + v2 + (e2 / 2 ** 32 | 0) | 0;
  e.add5H = L, e.default = {
    fromBig: g,
    split: _,
    toBig: v,
    shrSH: y,
    shrSL: b,
    rotrSH: x,
    rotrSL: S,
    rotrBH: C,
    rotrBL: w,
    rotr32H: T,
    rotr32L: E,
    rotlSH: D,
    rotlSL: O,
    rotlBH: k,
    rotlBL: A,
    add: j,
    add3L: M,
    add3H: N,
    add4L: P,
    add4H: F,
    add5H: L,
    add5L: I
  };
})), require_crypto = /* @__PURE__ */ __commonJSMin(((e) => {
  Object.defineProperty(e, "__esModule", { value: true }), e.crypto = void 0, e.crypto = typeof globalThis == "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
})), require_utils = /* @__PURE__ */ __commonJSMin(((e) => {
  Object.defineProperty(e, "__esModule", { value: true }), e.wrapXOFConstructorWithOpts = e.wrapConstructorWithOpts = e.wrapConstructor = e.Hash = e.nextTick = e.swap32IfBE = e.byteSwapIfBE = e.swap8IfBE = e.isLE = void 0, e.isBytes = h, e.anumber = g, e.abytes = _, e.ahash = v, e.aexists = y, e.aoutput = b, e.u8 = x, e.u32 = S, e.clean = C, e.createView = w, e.rotr = T, e.rotl = E, e.byteSwap = D, e.byteSwap32 = O, e.bytesToHex = M, e.hexToBytes = F, e.asyncLoop = I, e.utf8ToBytes = L, e.bytesToUtf8 = R, e.toBytes = z, e.kdfInputToBytes = B, e.concatBytes = V, e.checkOpts = H, e.createHasher = U, e.createOptHasher = W, e.createXOFer = G, e.randomBytes = K;
  var m = require_crypto();
  function h(e2) {
    return e2 instanceof Uint8Array || ArrayBuffer.isView(e2) && e2.constructor.name === "Uint8Array";
  }
  function g(e2) {
    if (!Number.isSafeInteger(e2) || e2 < 0) throw Error("positive integer expected, got " + e2);
  }
  function _(e2, ...m2) {
    if (!h(e2)) throw Error("Uint8Array expected");
    if (m2.length > 0 && !m2.includes(e2.length)) throw Error("Uint8Array expected of length " + m2 + ", got length=" + e2.length);
  }
  function v(e2) {
    if (typeof e2 != "function" || typeof e2.create != "function") throw Error("Hash should be wrapped by utils.createHasher");
    g(e2.outputLen), g(e2.blockLen);
  }
  function y(e2, m2 = true) {
    if (e2.destroyed) throw Error("Hash instance has been destroyed");
    if (m2 && e2.finished) throw Error("Hash#digest() has already been called");
  }
  function b(e2, m2) {
    _(e2);
    let h2 = m2.outputLen;
    if (e2.length < h2) throw Error("digestInto() expects output buffer of length at least " + h2);
  }
  function x(e2) {
    return new Uint8Array(e2.buffer, e2.byteOffset, e2.byteLength);
  }
  function S(e2) {
    return new Uint32Array(e2.buffer, e2.byteOffset, Math.floor(e2.byteLength / 4));
  }
  function C(...e2) {
    for (let m2 = 0; m2 < e2.length; m2++) e2[m2].fill(0);
  }
  function w(e2) {
    return new DataView(e2.buffer, e2.byteOffset, e2.byteLength);
  }
  function T(e2, m2) {
    return e2 << 32 - m2 | e2 >>> m2;
  }
  function E(e2, m2) {
    return e2 << m2 | e2 >>> 32 - m2 >>> 0;
  }
  e.isLE = (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
  function D(e2) {
    return e2 << 24 & 4278190080 | e2 << 8 & 16711680 | e2 >>> 8 & 65280 | e2 >>> 24 & 255;
  }
  e.swap8IfBE = e.isLE ? (e2) => e2 : (e2) => D(e2), e.byteSwapIfBE = e.swap8IfBE;
  function O(e2) {
    for (let m2 = 0; m2 < e2.length; m2++) e2[m2] = D(e2[m2]);
    return e2;
  }
  e.swap32IfBE = e.isLE ? (e2) => e2 : O;
  var k = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function")(), j = /* @__PURE__ */ Array.from({ length: 256 }, (e2, m2) => m2.toString(16).padStart(2, "0"));
  function M(e2) {
    if (_(e2), k) return e2.toHex();
    let m2 = "";
    for (let h2 = 0; h2 < e2.length; h2++) m2 += j[e2[h2]];
    return m2;
  }
  var N = {
    _0: 48,
    _9: 57,
    A: 65,
    F: 70,
    a: 97,
    f: 102
  };
  function P(e2) {
    if (e2 >= N._0 && e2 <= N._9) return e2 - N._0;
    if (e2 >= N.A && e2 <= N.F) return e2 - (N.A - 10);
    if (e2 >= N.a && e2 <= N.f) return e2 - (N.a - 10);
  }
  function F(e2) {
    if (typeof e2 != "string") throw Error("hex string expected, got " + typeof e2);
    if (k) return Uint8Array.fromHex(e2);
    let m2 = e2.length, h2 = m2 / 2;
    if (m2 % 2) throw Error("hex string expected, got unpadded hex of length " + m2);
    let g2 = new Uint8Array(h2);
    for (let m3 = 0, _2 = 0; m3 < h2; m3++, _2 += 2) {
      let h3 = P(e2.charCodeAt(_2)), v2 = P(e2.charCodeAt(_2 + 1));
      if (h3 === void 0 || v2 === void 0) {
        let m4 = e2[_2] + e2[_2 + 1];
        throw Error('hex string expected, got non-hex character "' + m4 + '" at index ' + _2);
      }
      g2[m3] = h3 * 16 + v2;
    }
    return g2;
  }
  e.nextTick = async () => {
  };
  async function I(m2, h2, g2) {
    let _2 = Date.now();
    for (let v2 = 0; v2 < m2; v2++) {
      g2(v2);
      let m3 = Date.now() - _2;
      m3 >= 0 && m3 < h2 || (await (0, e.nextTick)(), _2 += m3);
    }
  }
  function L(e2) {
    if (typeof e2 != "string") throw Error("string expected");
    return new Uint8Array(new TextEncoder().encode(e2));
  }
  function R(e2) {
    return new TextDecoder().decode(e2);
  }
  function z(e2) {
    return typeof e2 == "string" && (e2 = L(e2)), _(e2), e2;
  }
  function B(e2) {
    return typeof e2 == "string" && (e2 = L(e2)), _(e2), e2;
  }
  function V(...e2) {
    let m2 = 0;
    for (let h3 = 0; h3 < e2.length; h3++) {
      let g2 = e2[h3];
      _(g2), m2 += g2.length;
    }
    let h2 = new Uint8Array(m2);
    for (let m3 = 0, g2 = 0; m3 < e2.length; m3++) {
      let _2 = e2[m3];
      h2.set(_2, g2), g2 += _2.length;
    }
    return h2;
  }
  function H(e2, m2) {
    if (m2 !== void 0 && {}.toString.call(m2) !== "[object Object]") throw Error("options should be object or undefined");
    return Object.assign(e2, m2);
  }
  e.Hash = class {
  };
  function U(e2) {
    let m2 = (m3) => e2().update(z(m3)).digest(), h2 = e2();
    return m2.outputLen = h2.outputLen, m2.blockLen = h2.blockLen, m2.create = () => e2(), m2;
  }
  function W(e2) {
    let m2 = (m3, h3) => e2(h3).update(z(m3)).digest(), h2 = e2({});
    return m2.outputLen = h2.outputLen, m2.blockLen = h2.blockLen, m2.create = (m3) => e2(m3), m2;
  }
  function G(e2) {
    let m2 = (m3, h3) => e2(h3).update(z(m3)).digest(), h2 = e2({});
    return m2.outputLen = h2.outputLen, m2.blockLen = h2.blockLen, m2.create = (m3) => e2(m3), m2;
  }
  e.wrapConstructor = U, e.wrapConstructorWithOpts = W, e.wrapXOFConstructorWithOpts = G;
  function K(e2 = 32) {
    if (m.crypto && typeof m.crypto.getRandomValues == "function") return m.crypto.getRandomValues(new Uint8Array(e2));
    if (m.crypto && typeof m.crypto.randomBytes == "function") return Uint8Array.from(m.crypto.randomBytes(e2));
    throw Error("crypto.getRandomValues must be defined");
  }
})), require_sha3 = /* @__PURE__ */ __commonJSMin(((e) => {
  Object.defineProperty(e, "__esModule", { value: true }), e.shake256 = e.shake128 = e.keccak_512 = e.keccak_384 = e.keccak_256 = e.keccak_224 = e.sha3_512 = e.sha3_384 = e.sha3_256 = e.sha3_224 = e.Keccak = void 0, e.keccakP = M;
  var m = require__u64(), h = require_utils(), g = BigInt(0), _ = BigInt(1), v = BigInt(2), y = BigInt(7), b = BigInt(256), x = BigInt(113), S = [], C = [], w = [];
  for (let e2 = 0, m2 = _, h2 = 1, T2 = 0; e2 < 24; e2++) {
    [h2, T2] = [T2, (2 * h2 + 3 * T2) % 5], S.push(2 * (5 * T2 + h2)), C.push((e2 + 1) * (e2 + 2) / 2 % 64);
    let E2 = g;
    for (let e3 = 0; e3 < 7; e3++) m2 = (m2 << _ ^ (m2 >> y) * x) % b, m2 & v && (E2 ^= _ << (_ << /* @__PURE__ */ BigInt(e3)) - _);
    w.push(E2);
  }
  var T = (0, m.split)(w, true), E = T[0], D = T[1], O = (e2, h2, g2) => g2 > 32 ? (0, m.rotlBH)(e2, h2, g2) : (0, m.rotlSH)(e2, h2, g2), A = (e2, h2, g2) => g2 > 32 ? (0, m.rotlBL)(e2, h2, g2) : (0, m.rotlSL)(e2, h2, g2);
  function M(e2, m2 = 24) {
    let g2 = new Uint32Array(10);
    for (let h2 = 24 - m2; h2 < 24; h2++) {
      for (let m4 = 0; m4 < 10; m4++) g2[m4] = e2[m4] ^ e2[m4 + 10] ^ e2[m4 + 20] ^ e2[m4 + 30] ^ e2[m4 + 40];
      for (let m4 = 0; m4 < 10; m4 += 2) {
        let h3 = (m4 + 8) % 10, _3 = (m4 + 2) % 10, v2 = g2[_3], y2 = g2[_3 + 1], b2 = O(v2, y2, 1) ^ g2[h3], x2 = A(v2, y2, 1) ^ g2[h3 + 1];
        for (let h4 = 0; h4 < 50; h4 += 10) e2[m4 + h4] ^= b2, e2[m4 + h4 + 1] ^= x2;
      }
      let m3 = e2[2], _2 = e2[3];
      for (let h3 = 0; h3 < 24; h3++) {
        let g3 = C[h3], v2 = O(m3, _2, g3), y2 = A(m3, _2, g3), b2 = S[h3];
        m3 = e2[b2], _2 = e2[b2 + 1], e2[b2] = v2, e2[b2 + 1] = y2;
      }
      for (let m4 = 0; m4 < 50; m4 += 10) {
        for (let h3 = 0; h3 < 10; h3++) g2[h3] = e2[m4 + h3];
        for (let h3 = 0; h3 < 10; h3++) e2[m4 + h3] ^= ~g2[(h3 + 2) % 10] & g2[(h3 + 4) % 10];
      }
      e2[0] ^= E[h2], e2[1] ^= D[h2];
    }
    (0, h.clean)(g2);
  }
  var N = class e2 extends h.Hash {
    constructor(e3, m2, g2, _2 = false, v2 = 24) {
      if (super(), this.pos = 0, this.posOut = 0, this.finished = false, this.destroyed = false, this.enableXOF = false, this.blockLen = e3, this.suffix = m2, this.outputLen = g2, this.enableXOF = _2, this.rounds = v2, (0, h.anumber)(g2), !(0 < e3 && e3 < 200)) throw Error("only keccak-f1600 function is supported");
      this.state = new Uint8Array(200), this.state32 = (0, h.u32)(this.state);
    }
    clone() {
      return this._cloneInto();
    }
    keccak() {
      (0, h.swap32IfBE)(this.state32), M(this.state32, this.rounds), (0, h.swap32IfBE)(this.state32), this.posOut = 0, this.pos = 0;
    }
    update(e3) {
      (0, h.aexists)(this), e3 = (0, h.toBytes)(e3), (0, h.abytes)(e3);
      let { blockLen: m2, state: g2 } = this, _2 = e3.length;
      for (let h2 = 0; h2 < _2; ) {
        let v2 = Math.min(m2 - this.pos, _2 - h2);
        for (let m3 = 0; m3 < v2; m3++) g2[this.pos++] ^= e3[h2++];
        this.pos === m2 && this.keccak();
      }
      return this;
    }
    finish() {
      if (this.finished) return;
      this.finished = true;
      let { state: e3, suffix: m2, pos: h2, blockLen: g2 } = this;
      e3[h2] ^= m2, m2 & 128 && h2 === g2 - 1 && this.keccak(), e3[g2 - 1] ^= 128, this.keccak();
    }
    writeInto(e3) {
      (0, h.aexists)(this, false), (0, h.abytes)(e3), this.finish();
      let m2 = this.state, { blockLen: g2 } = this;
      for (let h2 = 0, _2 = e3.length; h2 < _2; ) {
        this.posOut >= g2 && this.keccak();
        let v2 = Math.min(g2 - this.posOut, _2 - h2);
        e3.set(m2.subarray(this.posOut, this.posOut + v2), h2), this.posOut += v2, h2 += v2;
      }
      return e3;
    }
    xofInto(e3) {
      if (!this.enableXOF) throw Error("XOF is not possible for this instance");
      return this.writeInto(e3);
    }
    xof(e3) {
      return (0, h.anumber)(e3), this.xofInto(new Uint8Array(e3));
    }
    digestInto(e3) {
      if ((0, h.aoutput)(e3, this), this.finished) throw Error("digest() was already called");
      return this.writeInto(e3), this.destroy(), e3;
    }
    digest() {
      return this.digestInto(new Uint8Array(this.outputLen));
    }
    destroy() {
      this.destroyed = true, (0, h.clean)(this.state);
    }
    _cloneInto(m2) {
      let { blockLen: h2, suffix: g2, outputLen: _2, rounds: v2, enableXOF: y2 } = this;
      return m2 || (m2 = new e2(h2, g2, _2, y2, v2)), m2.state32.set(this.state32), m2.pos = this.pos, m2.posOut = this.posOut, m2.finished = this.finished, m2.rounds = v2, m2.suffix = g2, m2.outputLen = _2, m2.enableXOF = y2, m2.destroyed = this.destroyed, m2;
    }
  };
  e.Keccak = N;
  var P = (e2, m2, g2) => (0, h.createHasher)(() => new N(m2, e2, g2));
  e.sha3_224 = (() => P(6, 144, 224 / 8))(), e.sha3_256 = (() => P(6, 136, 256 / 8))(), e.sha3_384 = (() => P(6, 104, 384 / 8))(), e.sha3_512 = (() => P(6, 72, 512 / 8))(), e.keccak_224 = (() => P(1, 144, 224 / 8))(), e.keccak_256 = (() => P(1, 136, 256 / 8))(), e.keccak_384 = (() => P(1, 104, 384 / 8))(), e.keccak_512 = (() => P(1, 72, 512 / 8))();
  var F = (e2, m2, g2) => (0, h.createXOFer)((h2 = {}) => new N(m2, e2, h2.dkLen === void 0 ? g2 : h2.dkLen, true));
  e.shake128 = (() => F(31, 168, 128 / 8))(), e.shake256 = (() => F(31, 136, 256 / 8))();
})), require_src = /* @__PURE__ */ __commonJSMin(((e, m) => {
  var { sha3_512: h } = require_sha3(), g = 24, _ = 32, v = (e2 = 4, m2 = Math.random) => {
    let h2 = "";
    for (; h2.length < e2; ) h2 += Math.floor(m2() * 36).toString(36);
    return h2;
  };
  function y(e2) {
    let m2 = 0n;
    for (let h2 of e2.values()) {
      let e3 = BigInt(h2);
      m2 = (m2 << 8n) + e3;
    }
    return m2;
  }
  var b = (e2 = "") => y(h(e2)).toString(36).slice(1), x = Array.from({ length: 26 }, (e2, m2) => String.fromCharCode(m2 + 97)), S = (e2) => x[Math.floor(e2() * x.length)], C = ({ globalObj: e2 = typeof global < "u" ? global : typeof window < "u" ? window : {}, random: m2 = Math.random } = {}) => {
    let h2 = Object.keys(e2).toString();
    return b(h2.length ? h2 + v(_, m2) : v(_, m2)).substring(0, _);
  }, w = (e2) => () => e2++, T = 476782367, E = ({ random: e2 = Math.random, counter: m2 = w(Math.floor(e2() * T)), length: h2 = g, fingerprint: _2 = C({ random: e2 }) } = {}) => function() {
    let g2 = S(e2), y2 = Date.now().toString(36), x2 = m2().toString(36);
    return `${g2 + b(`${y2 + v(h2, e2) + x2 + _2}`).substring(1, h2)}`;
  }, D = E(), O = (e2, { minLength: m2 = 2, maxLength: h2 = _ } = {}) => {
    let g2 = e2.length, v2 = /^[0-9a-z]+$/;
    try {
      if (typeof e2 == "string" && g2 >= m2 && g2 <= h2 && v2.test(e2)) return true;
    } finally {
    }
    return false;
  };
  m.exports.getConstants = () => ({
    defaultLength: g,
    bigLength: _
  }), m.exports.init = E, m.exports.createId = D, m.exports.bufToBigInt = y, m.exports.createCounter = w, m.exports.createFingerprint = C, m.exports.isCuid = O;
})), require_cuid2 = /* @__PURE__ */ __commonJSMin(((e, m) => {
  var { createId: h, init: g, getConstants: _, isCuid: v } = require_src();
  m.exports.createId = h, m.exports.init = g, m.exports.getConstants = _, m.exports.isCuid = v;
}));
function updateQueryParams(e) {
  let m = new URL(window.location.href);
  e(m.searchParams), window.history.replaceState({}, "", m.toString());
}
var urlRegex = /^(https?:\/\/\S+)$/;
function isUrl(e) {
  return typeof e == "string" && urlRegex.test(e);
}
function appendQueryParams({ href: e, queryParams: m, keys: h }) {
  let g = typeof m == "string" ? new URLSearchParams(m) : m;
  if (g.size === 0 || e.startsWith("http://") || e.startsWith("https://")) return e;
  let _ = e.startsWith("#"), v = _ && e.includes("?");
  if (_ && !v) {
    let m2 = h ? [...g.entries()].filter(([e2]) => h.includes(e2)) : [...g.entries()], _2 = new URLSearchParams();
    for (let [e2, h2] of m2) _2.set(e2, h2);
    let v2 = _2.toString();
    return v2 ? `/?${v2}${e}` : e;
  }
  let y = e, b = "", x = new URLSearchParams(), S = _ ? 1 : 0, C = e.indexOf("#", S);
  C !== -1 && (b = e.slice(C), y = e.slice(0, C));
  let w = y.indexOf("?");
  w !== -1 && (x = new URLSearchParams(y.slice(w + 1)), y = y.slice(0, w));
  let T = new URLSearchParams(x), E = h ? [...g.entries()].filter(([e2]) => h.includes(e2)) : [...g.entries()];
  for (let [e2, m2] of E) T.set(e2, m2);
  let D = T.toString();
  return D ? `${y}?${D}${b}` : e;
}
var createId = (0, require_cuid2().init)({ length: 6 });
function generateSessionId() {
  return `s_${createId()}`;
}
function isSessionId(e) {
  return e ? /^s_[\da-z]{6}$/.test(e) : false;
}
var sessionId = (() => {
  let e = new URL(window.location.href).searchParams.get(KnownQueryParams.sessionId);
  return isSessionId(e) ? (updateQueryParams((e2) => {
    e2.has(KnownQueryParams.kiosk) || e2.delete(KnownQueryParams.sessionId);
  }), Logger.debug("Connecting to existing session", { sessionId: e }), e) : (Logger.debug("Starting a new session", { sessionId: e }), generateSessionId());
})();
function getSessionId() {
  return sessionId;
}
var RuntimeManager = class {
  constructor(e, m = false) {
    __publicField(this, "initialHealthyCheck", new Deferred());
    this.config = e, this.lazy = m;
    try {
      new URL(this.config.url);
    } catch (e2) {
      throw Error(`Invalid runtime URL: ${this.config.url}. ${e2 instanceof Error ? e2.message : "Unknown error"}`, { cause: e2 });
    }
    this.lazy || this.init();
  }
  get isLazy() {
    return this.lazy;
  }
  get httpURL() {
    return new URL(this.config.url);
  }
  get isSameOrigin() {
    return this.httpURL.origin === window.location.origin;
  }
  formatHttpURL(e, m, h = true) {
    e || (e = "");
    let g = this.httpURL, v = new URLSearchParams(window.location.search);
    if (m) for (let [e2, h2] of m.entries()) g.searchParams.set(e2, h2);
    for (let [e2, m2] of v.entries()) h && !Object.values(KnownQueryParams).includes(e2) || g.searchParams.set(e2, m2);
    return g.pathname = `${g.pathname.replace(/\/$/, "")}/${e.replace(/^\//, "")}`, g.hash = "", g;
  }
  formatWsURL(e, m) {
    return asWsUrl(this.formatHttpURL(e, m, false).toString());
  }
  getWsURL(e) {
    let m = new URL(this.config.url), h = new URLSearchParams(m.search);
    return new URLSearchParams(window.location.search).forEach((e2, m2) => {
      h.has(m2) || h.set(m2, e2);
    }), h.set(KnownQueryParams.sessionId, e), this.formatWsURL("/ws", h);
  }
  getWsSyncURL(e) {
    let m = new URL(this.config.url), h = new URLSearchParams(m.search);
    return new URLSearchParams(window.location.search).forEach((e2, m2) => {
      h.has(m2) || h.set(m2, e2);
    }), h.set(KnownQueryParams.sessionId, e), this.formatWsURL("/ws_sync", h);
  }
  getTerminalWsURL() {
    return this.formatWsURL("/terminal/ws");
  }
  getLSPURL(e) {
    if (e === "copilot") {
      let m = this.formatWsURL(`/lsp/${e}`);
      return m.search = "", m;
    }
    return this.formatWsURL(`/lsp/${e}`);
  }
  getAiURL(e) {
    return this.formatHttpURL(`/api/ai/${e}`);
  }
  healthURL() {
    return this.formatHttpURL("/health");
  }
  async isHealthy() {
    if (isWasm() || isIslands()) return true;
    try {
      let e = await fetch(this.healthURL().toString());
      if (e.redirected) {
        Logger.debug(`Runtime redirected to ${e.url}`);
        let m2 = e.url.replace(/\/health$/, "");
        this.config.url = m2;
      }
      let m = e.ok;
      return m && this.setDOMBaseUri(this.config.url), m;
    } catch {
      return false;
    }
  }
  setDOMBaseUri(e) {
    e = e.split("?", 1)[0], e.endsWith("/") || (e += "/");
    let m = document.querySelector("base");
    m ? m.setAttribute("href", e) : (m = document.createElement("base"), m.setAttribute("href", e), document.head.append(m));
  }
  async init(e) {
    Logger.debug("Initializing runtime...");
    let m = 0;
    for (; !await this.isHealthy(); ) {
      if (m >= 25) {
        Logger.error("Failed to connect after 25 retries"), this.initialHealthyCheck.reject(/* @__PURE__ */ Error("Failed to connect after 25 retries"));
        return;
      }
      if (!(e == null ? void 0 : e.disableRetryDelay)) {
        let e2 = Math.min(100 * 1.2 ** m, 2e3);
        await new Promise((m2) => setTimeout(m2, e2));
      }
      m++;
    }
    Logger.debug("Runtime is healthy"), this.initialHealthyCheck.resolve();
  }
  async waitForHealthy() {
    return this.initialHealthyCheck.promise;
  }
  headers() {
    let e = {
      "Marimo-Session-Id": getSessionId(),
      "Marimo-Server-Token": this.config.serverToken ?? "",
      "x-runtime-url": this.httpURL.toString()
    };
    return this.config.authToken && (e.Authorization = `Bearer ${this.config.authToken}`), e;
  }
  sessionHeaders() {
    return { "Marimo-Session-Id": getSessionId() };
  }
};
function asWsUrl(e) {
  if (!e.startsWith("http")) {
    Logger.warn(`URL must start with http: ${e}`);
    let m = new URL(e);
    return m.protocol = "ws", m;
  }
  return new URL(e.replace(/^http/, "ws"));
}
require_compiler_runtime();
function getBaseURI() {
  let e = new URL(document.baseURI);
  return e.search = "", e.hash = "", e.toString();
}
const runtimeConfigAtom = atom({
  lazy: true,
  url: getBaseURI()
});
var runtimeManagerAtom = atom((e) => {
  let m = e(runtimeConfigAtom);
  return new RuntimeManager(m, m.lazy);
});
function getRuntimeManager() {
  return store.get(runtimeManagerAtom);
}
function asRemoteURL(e) {
  if (e.startsWith("http")) return new URL(e);
  let m = getRuntimeManager().httpURL.toString();
  return m.startsWith("blob:") && (m = m.replace("blob:", "")), new URL(e, m);
}
const millisecondsInWeek = 6048e5, millisecondsInDay = 864e5, millisecondsInMinute = 6e4, millisecondsInHour = 36e5, millisecondsInSecond = 1e3, secondsInDay = 3600 * 24;
secondsInDay * 7, secondsInDay * 365.2425;
const constructFromSymbol = /* @__PURE__ */ Symbol.for("constructDateFrom");
function constructFrom(e, m) {
  return typeof e == "function" ? e(m) : e && typeof e == "object" && constructFromSymbol in e ? e[constructFromSymbol](m) : e instanceof Date ? new e.constructor(m) : new Date(m);
}
function toDate(e, m) {
  return constructFrom(m || e, e);
}
export {
  millisecondsInMinute as a,
  asRemoteURL as c,
  isUrl as d,
  require_cuid2 as f,
  CircleQuestionMark as g,
  isWasm as h,
  millisecondsInHour as i,
  getRuntimeManager as l,
  Deferred as m,
  constructFrom as n,
  millisecondsInSecond as o,
  waitForConnectionOpen as p,
  millisecondsInDay as r,
  millisecondsInWeek as s,
  toDate as t,
  appendQueryParams as u
};
