let e,
  t,
  r,
  o,
  l,
  s,
  n = Object.getPrototypeOf,
  i = { isConnected: 1 },
  f = {},
  h = n(i),
  u = n(n),
  a = (e, t, r, o) =>
    (e ?? (o ? setTimeout(r, o) : queueMicrotask(r), new Set())).add(t),
  d = (e, t, o) => {
    let l = r;
    r = t;
    try {
      return e(o);
    } catch (e) {
      return (console.error(e), o);
    } finally {
      r = l;
    }
  },
  w = (e) => e.filter((e) => e.t?.isConnected),
  c = (e) =>
    (l = a(
      l,
      e,
      () => {
        for (let e of l) ((e.o = w(e.o)), (e.l = w(e.l)));
        l = s;
      },
      1e3,
    )),
  _ = {
    get val() {
      return (r?.i?.add(this), this.rawVal);
    },
    get oldVal() {
      return (r?.i?.add(this), this.h);
    },
    set val(o) {
      (r?.u?.add(this),
        o !== this.rawVal &&
          ((this.rawVal = o),
          this.o.length + this.l.length
            ? (t?.add(this), (e = a(e, this, v)))
            : (this.h = o)));
    },
  },
  S = (e) => ({ __proto__: _, rawVal: e, h: e, o: [], l: [] }),
  g = (e, t) => {
    let r = { i: new Set(), u: new Set() },
      l = { f: e },
      s = o;
    o = [];
    let n = d(e, r, t);
    n = (n ?? document).nodeType ? n : new Text(n);
    for (let e of r.i) r.u.has(e) || (c(e), e.o.push(l));
    for (let e of o) e.t = n;
    return ((o = s), (l.t = n));
  },
  y = (e, t = S(), r) => {
    let l = { i: new Set(), u: new Set() },
      s = { f: e, s: t };
    ((s.t = r ?? o?.push(s) ?? i), (t.val = d(e, l, t.rawVal)));
    for (let e of l.i) l.u.has(e) || (c(e), e.l.push(s));
    return t;
  },
  b = (e, ...t) => {
    for (let r of t.flat(1 / 0)) {
      let t = n(r ?? 0),
        o = t === _ ? g(() => r.val) : t === u ? g(r) : r;
      o != s && e.append(o);
    }
    return e;
  },
  m = (e, t, ...r) => {
    let [{ is: o, ...l }, ...i] = n(r[0] ?? 0) === h ? r : [{}, ...r],
      a = e
        ? document.createElementNS(e, t, { is: o })
        : document.createElement(t, { is: o });
    for (let [e, r] of Object.entries(l)) {
      let o = (t) =>
          t ? (Object.getOwnPropertyDescriptor(t, e) ?? o(n(t))) : s,
        l = t + "," + e,
        i = (f[l] ??= o(n(a))?.set ?? 0),
        h = e.startsWith("on")
          ? (t, r) => {
              let o = e.slice(2);
              (a.removeEventListener(o, r), a.addEventListener(o, t));
            }
          : i
            ? i.bind(a)
            : a.setAttribute.bind(a, e),
        d = n(r ?? 0);
      (e.startsWith("on") || (d === u && ((r = y(r)), (d = _))),
        d === _ ? g(() => (h(r.val, r.h), a)) : h(r));
    }
    return b(a, i);
  },
  x = (e) => ({ get: (t, r) => m.bind(s, e, r) }),
  j = (e, t) => (t ? t !== e && e.replaceWith(t) : e.remove()),
  v = () => {
    let r = 0,
      o = [...e].filter((e) => e.rawVal !== e.h);
    do {
      t = new Set();
      for (let e of new Set(o.flatMap((e) => (e.l = w(e.l)))))
        (y(e.f, e.s, e.t), (e.t = s));
    } while (++r < 100 && (o = [...t]).length);
    let l = [...e].filter((e) => e.rawVal !== e.h);
    e = s;
    for (let e of new Set(l.flatMap((e) => (e.o = w(e.o)))))
      (j(e.t, g(e.f, e.t)), (e.t = s));
    for (let e of l) e.h = e.rawVal;
  };
export default {
  tags: new Proxy((e) => new Proxy(m, x(e)), x()),
  hydrate: (e, t) => j(e, g(t, e)),
  add: b,
  state: S,
  derive: y,
};
