import * as r from "react";
import { useRef as G, useEffect as $ } from "react";
import { u as k, r as j, a as J, b as X, c as T, d as Y, e as Z, f as w, g as K, h as ee, i as te, j as R, k as ne, m as se, n as ae, G as re, o as ce, p as oe, q as N, t as ie, v as ue, w as _, x as W, y as H, z as le, l as A, A as de, B as fe, C as F, D as pe, E as be, F as me, H as ge, I as ve, J as Se, K as he, M as Me, N as B, O as ye, P as Pe, Q as ke, S as Ee, T as Te, U as we, V as Ae, W as Ce, X as Q, Y as Ie, Z as Oe, _ as De, $ as Le, a0 as Re, a1 as Ne, a2 as Fe, a3 as Ve, a4 as Be, a5 as ze, a6 as Ue, a7 as qe, a8 as L, a9 as Je } from "./contexts-7Stj-u1L.mjs";
import { m as E } from "./room-DQY9HC45.mjs";
import { ConnectionQuality as _e, ConnectionState as I, LocalTrackPublication as We, facingModeFromLocalTrack as He, Room as Qe, Track as y, createAudioAnalyser as V, ParticipantKind as xe } from "@cc-livekit/livekit-client";
const Ge = (e) => {
  const n = r.useRef(e);
  return r.useEffect(() => {
    n.current = e;
  }), n;
};
function $e(e, n) {
  const t = Xe(), s = Ge(n);
  return r.useLayoutEffect(() => {
    let a = !1;
    const c = e.current;
    if (!c) return;
    function o(u, i) {
      a || s.current(u, i);
    }
    return t == null || t.subscribe(c, o), () => {
      a = !0, t == null || t.unsubscribe(c, o);
    };
  }, [e.current, t, s]), t == null ? void 0 : t.observer;
}
function je() {
  let e = !1, n = [];
  const t = /* @__PURE__ */ new Map();
  if (typeof window > "u")
    return;
  const s = new ResizeObserver((a, c) => {
    n = n.concat(a), e || window.requestAnimationFrame(() => {
      const o = /* @__PURE__ */ new Set();
      for (let u = 0; u < n.length; u++) {
        if (o.has(n[u].target)) continue;
        o.add(n[u].target);
        const i = t.get(n[u].target);
        i == null || i.forEach((l) => l(n[u], c));
      }
      n = [], e = !1;
    }), e = !0;
  });
  return {
    observer: s,
    subscribe(a, c) {
      s.observe(a);
      const o = t.get(a) ?? [];
      o.push(c), t.set(a, o);
    },
    unsubscribe(a, c) {
      const o = t.get(a) ?? [];
      if (o.length === 1) {
        s.unobserve(a), t.delete(a);
        return;
      }
      const u = o.indexOf(c);
      u !== -1 && o.splice(u, 1), t.set(a, o);
    }
  };
}
let D;
const Xe = () => D || (D = je()), Ye = (e) => {
  const [n, t] = r.useState({ width: 0, height: 0 });
  r.useLayoutEffect(() => {
    if (e.current) {
      const { width: a, height: c } = e.current.getBoundingClientRect();
      t({ width: a, height: c });
    }
  }, [e.current]);
  const s = r.useCallback(
    (a) => t(a.contentRect),
    []
  );
  return $e(e, s), n;
};
function m(e, n, t = !0) {
  const [s, a] = r.useState(n);
  return r.useEffect(() => {
    if (t && a(n), typeof window > "u" || !e) return;
    const c = e.subscribe(a);
    return () => c.unsubscribe();
  }, [e, t]), s;
}
function Mt(e) {
  const n = (c) => typeof window < "u" ? window.matchMedia(c).matches : !1, [t, s] = r.useState(n(e));
  function a() {
    s(n(e));
  }
  return r.useEffect(() => {
    const c = window.matchMedia(e);
    return a(), c.addListener ? c.addListener(a) : c.addEventListener("change", a), () => {
      c.removeListener ? c.removeListener(a) : c.removeEventListener("change", a);
    };
  }, [e]), t;
}
function yt(e) {
  const n = k(e), t = r.useCallback(async () => {
    await n.startAudio();
  }, [n]), s = r.useMemo(
    () => j(n),
    [n]
  ), { canPlayAudio: a } = m(s, {
    canPlayAudio: n.canPlaybackAudio
  });
  return { canPlayAudio: a, startAudio: t };
}
function Pt(e) {
  const { state: n, dispatch: t } = J().pin;
  return { buttonProps: r.useMemo(() => {
    const { className: a } = X();
    return E(e, {
      className: a,
      disabled: !(n != null && n.length),
      onClick: () => {
        t && t({ msg: "clear_pin" });
      }
    });
  }, [e, t, n]) };
}
function kt(e = {}) {
  const n = T(e.participant), { className: t, connectionQualityObserver: s } = r.useMemo(
    () => Y(n),
    [n]
  ), a = m(s, _e.Unknown);
  return { className: t, quality: a };
}
function O(e) {
  const n = k(e), t = r.useMemo(() => Z(n), [n]);
  return m(t, n.state);
}
function Et(e, n) {
  const t = typeof e == "function" ? e : n, s = typeof e == "string" ? e : void 0, a = w(), { send: c, messageObservable: o, isSendingObservable: u } = r.useMemo(
    () => K(a, s, t),
    [a, s, t]
  ), i = m(o, void 0), l = m(u, !1);
  return {
    message: i,
    send: c,
    isSending: l
  };
}
function Tt(e) {
  const n = w(), t = O(n), s = ee();
  return { buttonProps: r.useMemo(() => {
    const { className: c, disconnect: o } = te(n);
    return E(e, {
      className: c,
      onClick: async () => {
        var i;
        s != null && s.onHangup ? await ((i = s == null ? void 0 : s.onHangup) == null ? void 0 : i.call(s)) : o(e.stopTracks ?? !0);
      },
      disabled: t === I.Disconnected
    });
  }, [n, e, t]) };
}
function Ze(e) {
  if (e.publication instanceof We) {
    const n = e.publication.track;
    if (n) {
      const { facingMode: t } = He(n);
      return t;
    }
  }
  return "undefined";
}
function wt({ trackRef: e, props: n }) {
  const t = R(e), s = ne(), { className: a } = r.useMemo(() => se(), []), c = r.useMemo(() => ae(t, s == null ? void 0 : s.pin.state), [t, s == null ? void 0 : s.pin.state]);
  return { mergedProps: r.useMemo(
    () => E(n, {
      className: a,
      onClick: (u) => {
        var i, l, d, f, p;
        (i = n.onClick) == null || i.call(n, u), c ? (d = s == null ? void 0 : (l = s.pin).dispatch) == null || d.call(l, {
          msg: "clear_pin"
        }) : (console.warn("set pin 2"), (p = s == null ? void 0 : (f = s.pin).dispatch) == null || p.call(f, {
          msg: "set_pin",
          trackReference: t
        }));
      }
    }),
    [n, a, t, c, s == null ? void 0 : s.pin]
  ), inFocus: c };
}
function At(e, n, t = {}) {
  const s = t.gridLayouts ?? re, { width: a, height: c } = Ye(e);
  return {
    layout: ce(s, n),
    containerWidth: a,
    containerHeight: c
  };
}
function z(e, n = {}) {
  var u, i;
  const t = typeof e == "string" ? n.participant : e.participant, s = T(t), a = typeof e == "string" ? { participant: s, source: e } : e, [c, o] = r.useState(
    !!((u = a.publication) != null && u.isMuted || (i = s.getTrackPublication(a.source)) != null && i.isMuted)
  );
  return r.useEffect(() => {
    const l = oe(a).subscribe(o);
    return () => l.unsubscribe();
  }, [N(a)]), c;
}
function Ke(e) {
  const n = T(e), t = r.useMemo(() => ie(n), [n]);
  return m(t, n.isSpeaking);
}
function et(e = {}) {
  const n = k(e.room), [t, s] = r.useState(n.localParticipant), [a, c] = r.useState(
    t.isMicrophoneEnabled
  ), [o, u] = r.useState(
    t.isMicrophoneEnabled
  ), [i, l] = r.useState(
    t.lastMicrophoneError
  ), [d, f] = r.useState(t.lastCameraError), [p, g] = r.useState(
    t.isMicrophoneEnabled
  ), [S, b] = r.useState(
    void 0
  ), [v, h] = r.useState(void 0), P = (M) => {
    u(M.isCameraEnabled), c(M.isMicrophoneEnabled), g(M.isScreenShareEnabled), h(M.cameraTrack), b(M.microphoneTrack), l(M.participant.lastMicrophoneError), f(M.participant.lastCameraError), s(M.participant);
  };
  return r.useEffect(() => {
    const M = ue(n.localParticipant).subscribe(P);
    return () => M.unsubscribe();
  }, [n]), {
    isMicrophoneEnabled: a,
    isScreenShareEnabled: p,
    isCameraEnabled: o,
    microphoneTrack: S,
    cameraTrack: v,
    lastMicrophoneError: i,
    lastCameraError: d,
    localParticipant: t
  };
}
function Ct() {
  const e = w(), n = r.useMemo(
    () => _(e.localParticipant),
    [e]
  );
  return m(n, e.localParticipant.permissions);
}
function It({
  kind: e,
  room: n,
  track: t,
  requestPermissions: s,
  onError: a
}) {
  const c = W(), o = r.useMemo(() => n ?? c ?? new Qe(), [n, c]), u = r.useMemo(
    () => H(e, a, s),
    [e, s, a]
  ), i = m(u, []), l = r.useMemo(() => {
    const b = i.find((v) => v.deviceId === "default");
    return b ? [b, ...i.filter((v) => !b.label.includes(v.label))] : i;
  }, [i]), [d, f] = r.useState(
    (o == null ? void 0 : o.getActiveDevice(e)) ?? "default"
  ), { className: p, activeDeviceObservable: g, setActiveMediaDevice: S } = r.useMemo(
    () => le(e, o),
    [e, o, t]
  );
  return r.useEffect(() => {
    const b = g.subscribe((v) => {
      v && (A.info("setCurrentDeviceId", v), f(v));
    });
    return () => {
      b == null || b.unsubscribe();
    };
  }, [g]), {
    devices: l,
    className: p,
    activeDeviceId: d,
    setActiveMediaDevice: S
  };
}
function Ot({
  kind: e,
  onError: n
}) {
  const t = r.useMemo(
    () => H(e, n),
    [e, n]
  );
  return m(t, []);
}
function tt(e, n, t = {}) {
  const s = r.useRef([]), a = r.useRef(-1), c = n !== a.current, o = typeof t.customSortFunction == "function" ? t.customSortFunction(e) : de(e);
  let u = [...o];
  if (c === !1)
    try {
      u = fe(s.current, o, n);
    } catch (i) {
      A.error("Error while running updatePages(): ", i);
    }
  return c ? s.current = o : s.current = u, a.current = n, u;
}
function Dt(e, n) {
  const [t, s] = r.useState(1), a = Math.max(Math.ceil(n.length / e), 1);
  t > a && s(a);
  const c = t * e, o = c - e, u = (f) => {
    s((p) => f === "next" ? p === a ? p : p + 1 : p === 1 ? p : p - 1);
  }, i = (f) => {
    f > a ? s(a) : f < 1 ? s(1) : s(f);
  }, d = tt(n, e).slice(o, c);
  return {
    totalPageCount: a,
    nextPage: () => u("next"),
    prevPage: () => u("previous"),
    setPage: i,
    firstItemIndex: o,
    lastItemIndex: c,
    tracks: d,
    currentPage: t
  };
}
function Lt(e = {}) {
  let n = F();
  e.participant && (n = e.participant);
  const t = r.useMemo(() => pe(n), [n]), { identity: s, name: a, metadata: c } = m(t, {
    name: n == null ? void 0 : n.name,
    identity: n == null ? void 0 : n.identity,
    metadata: n == null ? void 0 : n.metadata
  });
  return { identity: s, name: a, metadata: c };
}
function Rt(e = {}) {
  const n = T(e.participant), t = r.useMemo(() => _(n), [n]);
  return m(t, n.permissions);
}
function Nt({
  trackRef: e,
  onParticipantClick: n,
  disableSpeakingIndicator: t,
  htmlProps: s
}) {
  const a = R(e), c = r.useMemo(() => {
    const { className: p } = be();
    return E(s, {
      className: p,
      onClick: (g) => {
        var S;
        if ((S = s.onClick) == null || S.call(s, g), typeof n == "function") {
          const b = a.publication ?? a.participant.getTrackPublication(a.source);
          n({ participant: a.participant, track: b });
        }
      }
    });
  }, [
    s,
    n,
    a.publication,
    a.source,
    a.participant
  ]), o = a.participant.getTrackPublication(y.Source.Microphone), u = r.useMemo(() => ({
    participant: a.participant,
    source: y.Source.Microphone,
    publication: o
  }), [o, a.participant]), i = z(a), l = z(u), d = Ke(a.participant), f = Ze(a);
  return {
    elementProps: {
      "data-lk-audio-muted": l,
      "data-lk-video-muted": i,
      "data-lk-speaking": t === !0 ? !1 : d,
      "data-lk-local-participant": a.participant.isLocal,
      "data-lk-source": a.source,
      "data-lk-facing-mode": f,
      ...c
    }
  };
}
function x(e = {}) {
  const n = k(e.room), [t, s] = r.useState([]);
  return r.useEffect(() => {
    const a = me(n, {
      additionalRoomEvents: e.updateOnlyOn
    }).subscribe(s);
    return () => a.unsubscribe();
  }, [n, JSON.stringify(e.updateOnlyOn)]), t;
}
function Ft(e = {}) {
  const n = x(e), { localParticipant: t } = et(e);
  return r.useMemo(
    () => [t, ...n],
    [t, n]
  );
}
function Vt(e) {
  return e = ge(e), r.useMemo(() => (e == null ? void 0 : e.pin.state) !== void 0 && e.pin.state.length >= 1 ? e.pin.state : [], [e.pin.state]);
}
function Bt(e, n = {}) {
  const t = w(), [s] = r.useState(n.updateOnlyOn), a = r.useMemo(() => typeof e == "string" ? ve(t, e, {
    additionalEvents: s
  }) : Se(t, e, {
    additionalEvents: s
  }), [t, JSON.stringify(e), s]), [c, o] = r.useState({
    p: void 0
  });
  return r.useEffect(() => {
    const u = a.subscribe((i) => o({ p: i }));
    return () => u.unsubscribe();
  }, [a]), c.p;
}
function zt(e = {}) {
  const n = k(e.room), t = r.useMemo(() => he(n), [n]), { name: s, metadata: a } = m(t, {
    name: n.name,
    metadata: n.metadata
  });
  return { name: s, metadata: a };
}
function nt() {
  const e = w(), n = r.useMemo(() => Me(e), [e]);
  return m(n, e.activeSpeakers);
}
function Ut(e) {
  const [n, t] = r.useState(
    B(e)
  ), s = nt();
  return r.useEffect(() => {
    t(B(e));
  }, [s, e]), n;
}
function qt({ room: e, props: n }) {
  const t = k(e), { className: s, roomAudioPlaybackAllowedObservable: a, handleStartAudioPlayback: c } = r.useMemo(
    () => ye(),
    []
  ), o = r.useMemo(
    () => a(t),
    [t, a]
  ), { canPlayAudio: u } = m(o, {
    canPlayAudio: t.canPlaybackAudio
  });
  return { mergedProps: r.useMemo(
    () => E(n, {
      className: s,
      onClick: () => {
        c(t);
      },
      style: { display: u ? "none" : "block" }
    }),
    [n, s, u, c, t]
  ), canPlayAudio: u };
}
function Jt({ room: e, props: n }) {
  const t = k(e), { className: s, roomVideoPlaybackAllowedObservable: a, handleStartVideoPlayback: c } = r.useMemo(
    () => Pe(),
    []
  ), o = r.useMemo(
    () => a(t),
    [t, a]
  ), { canPlayVideo: u } = m(o, {
    canPlayVideo: t.canPlaybackVideo
  });
  return { mergedProps: r.useMemo(
    () => E(n, {
      className: s,
      onClick: () => {
        c(t);
      },
      style: { display: u ? "none" : "block" }
    }),
    [n, s, u, c, t]
  ), canPlayVideo: u };
}
function _t(e, n = {}) {
  const t = r.useRef(null), s = r.useRef(null), a = n.minSwipeDistance ?? 50, c = (i) => {
    s.current = null, t.current = i.targetTouches[0].clientX;
  }, o = (i) => {
    s.current = i.targetTouches[0].clientX;
  }, u = r.useCallback(() => {
    if (!t.current || !s.current)
      return;
    const i = t.current - s.current, l = i > a, d = i < -a;
    l && n.onLeftSwipe && n.onLeftSwipe(), d && n.onRightSwipe && n.onRightSwipe();
  }, [a, n]);
  r.useEffect(() => {
    const i = e.current;
    return i && (i.addEventListener("touchstart", c, { passive: !0 }), i.addEventListener("touchmove", o, { passive: !0 }), i.addEventListener("touchend", u, { passive: !0 })), () => {
      i && (i.removeEventListener("touchstart", c), i.removeEventListener("touchmove", o), i.removeEventListener("touchend", u));
    };
  }, [e, u]);
}
function Wt({ props: e }) {
  const { dispatch: n, state: t } = J().widget, { className: s } = r.useMemo(() => ke(), []);
  return { mergedProps: r.useMemo(() => E(e, {
    className: s,
    onClick: () => {
      n && n({ msg: "toggle_chat" });
    },
    "aria-pressed": t != null && t.showChat ? "true" : "false",
    "data-lk-unread-msgs": t ? t.unreadMessages < 10 ? t.unreadMessages.toFixed(0) : "9+" : "0"
  }), [e, s, n, t]) };
}
function Ht(e, n, t = {}) {
  const [s, a] = r.useState(void 0);
  return r.useEffect(() => {
    var o;
    if (e === void 0)
      throw Error("token endpoint needs to be defined");
    if (((o = t.userInfo) == null ? void 0 : o.identity) === void 0)
      return;
    (async () => {
      A.debug("fetching token");
      const u = new URLSearchParams({ ...t.userInfo, roomName: n }), i = await fetch(`${e}?${u.toString()}`);
      if (!i.ok) {
        A.error(
          `Could not fetch token. Server responded with status ${i.status}: ${i.statusText}`
        );
        return;
      }
      const { accessToken: l } = await i.json();
      a(l);
    })();
  }, [e, n, JSON.stringify(t)]), s;
}
function Qt(e) {
  var c, o;
  const n = R(e), { className: t, mediaMutedObserver: s } = r.useMemo(
    () => Ee(n),
    [N(n)]
  );
  return { isMuted: m(
    s,
    !!((c = n.publication) != null && c.isMuted || (o = n.participant.getTrackPublication(n.source)) != null && o.isMuted)
  ), className: t };
}
function xt({
  source: e,
  onChange: n,
  initialState: t,
  captureOptions: s,
  publishOptions: a,
  onDeviceError: c,
  ...o
}) {
  var P;
  const u = W(), i = (P = u == null ? void 0 : u.localParticipant) == null ? void 0 : P.getTrackPublication(e), l = r.useRef(!1), { toggle: d, className: f, pendingObserver: p, enabledObserver: g } = r.useMemo(
    () => u ? Te(e, u, s, a, c) : we(),
    [u, e, JSON.stringify(s), a]
  ), S = m(p, !1), b = m(g, t ?? !!(i != null && i.isEnabled));
  r.useEffect(() => {
    n == null || n(b, l.current), l.current = !1;
  }, [b, n]), r.useEffect(() => {
    t !== void 0 && (A.debug("forcing initial toggle state", e, t), d(t));
  }, []);
  const v = r.useMemo(() => E(o, { className: f }), [o, f]), h = r.useCallback(
    (M) => {
      var C;
      l.current = !0, d().catch(() => l.current = !1), (C = o.onClick) == null || C.call(o, M);
    },
    [o, d]
  );
  return {
    toggle: d,
    enabled: b,
    pending: S,
    track: i,
    buttonProps: {
      ...v,
      "aria-pressed": b,
      "data-lk-source": e,
      "data-lk-enabled": b,
      disabled: S,
      onClick: h
    }
  };
}
function Gt(e = [
  y.Source.Camera,
  y.Source.Microphone,
  y.Source.ScreenShare,
  y.Source.ScreenShareAudio,
  y.Source.Unknown
], n = {}) {
  const t = k(n.room), [s, a] = r.useState([]), [c, o] = r.useState([]), u = r.useMemo(() => e.map((l) => Ae(l) ? l.source : l), [JSON.stringify(e)]);
  r.useEffect(() => {
    const l = Ce(t, u, {
      additionalRoomEvents: n.updateOnlyOn,
      onlySubscribed: n.onlySubscribed
    }).subscribe(({ trackReferences: d, participants: f }) => {
      a(d), o(f);
    });
    return () => l.unsubscribe();
  }, [
    t,
    JSON.stringify(n.onlySubscribed),
    JSON.stringify(n.updateOnlyOn),
    JSON.stringify(e)
  ]);
  const i = r.useMemo(() => {
    if (Q(e)) {
      const l = at(e, c), d = Array.from(s);
      return c.forEach((f) => {
        l.has(f.identity) && (l.get(f.identity) ?? []).forEach((g) => {
          if (s.find(
            ({ participant: b, publication: v }) => f.identity === b.identity && v.source === g
          ))
            return;
          const S = {
            participant: f,
            source: g
          };
          d.push(S);
        });
      }), d;
    } else
      return s;
  }, [s, c, e]);
  return n.filterLocalTracks ? i.filter(
    (l) => !l.participant.isLocal
  ) : i;
}
function st(e, n) {
  const t = new Set(e);
  for (const s of n)
    t.delete(s);
  return t;
}
function at(e, n) {
  const t = /* @__PURE__ */ new Map();
  if (Q(e)) {
    const s = e.filter((a) => a.withPlaceholder).map((a) => a.source);
    n.forEach((a) => {
      const c = a.getTrackPublications().map((u) => {
        var i;
        return (i = u.track) == null ? void 0 : i.source;
      }).filter((u) => u !== void 0), o = Array.from(
        st(new Set(s), new Set(c))
      );
      o.length > 0 && t.set(a.identity, o);
    });
  }
  return t;
}
function rt(e) {
  const [n, t] = r.useState(Ie(e)), { trackObserver: s } = r.useMemo(() => Oe(e), [e.participant.sid ?? e.participant.identity, e.source]);
  return r.useEffect(() => {
    const a = s.subscribe((c) => {
      t(c);
    });
    return () => a == null ? void 0 : a.unsubscribe();
  }, [s]), {
    participant: e.participant,
    source: e.source ?? y.Source.Unknown,
    publication: n
  };
}
function $t(e, n) {
  const t = T(n);
  return rt({ name: e, participant: t });
}
function jt(e) {
  const n = w(), t = O(n), s = r.useMemo(
    () => t === I.Disconnected,
    [t]
  ), a = r.useMemo(
    () => De(n, e),
    [n, e, s]
  ), c = m(a.isSendingObservable, !1), o = m(a.messageObservable, []);
  return { send: a.send, update: a.update, chatMessages: o, isSending: c };
}
function Xt(e = {}) {
  const [n, t] = r.useState(
    Le(e.defaults, e.preventLoad ?? !1)
  ), s = r.useCallback((i) => {
    t((l) => ({ ...l, audioEnabled: i }));
  }, []), a = r.useCallback((i) => {
    t((l) => ({ ...l, videoEnabled: i }));
  }, []), c = r.useCallback((i) => {
    t((l) => ({ ...l, audioDeviceId: i }));
  }, []), o = r.useCallback((i) => {
    t((l) => ({ ...l, videoDeviceId: i }));
  }, []), u = r.useCallback((i) => {
    t((l) => ({ ...l, username: i }));
  }, []);
  return r.useEffect(() => {
    Re(n, e.preventSave ?? !1);
  }, [n, e.preventSave]), {
    userChoices: n,
    saveAudioInputEnabled: s,
    saveVideoInputEnabled: a,
    saveAudioInputDeviceId: c,
    saveVideoInputDeviceId: o,
    saveUsername: u
  };
}
function Yt(e, n = {}) {
  const t = T(e), s = k(n.room), a = r.useMemo(() => Ne(s, t), [s, t]);
  return m(
    a,
    t.isLocal ? t.isE2EEEnabled : !!(t != null && t.isEncrypted)
  );
}
function Zt(e, n = { fftSize: 32, smoothingTimeConstant: 0 }) {
  const t = Fe(e) ? e.publication.track : e, [s, a] = r.useState(0);
  return r.useEffect(() => {
    if (!t || !t.mediaStream)
      return;
    const { cleanup: c, analyser: o } = V(t, n), u = o.frequencyBinCount, i = new Uint8Array(u), d = setInterval(() => {
      o.getByteFrequencyData(i);
      let f = 0;
      for (let p = 0; p < i.length; p++) {
        const g = i[p];
        f += g * g;
      }
      a(Math.sqrt(f / i.length) / 255);
    }, 1e3 / 30);
    return () => {
      c(), clearInterval(d);
    };
  }, [t, t == null ? void 0 : t.mediaStream, JSON.stringify(n)]), s;
}
const ct = (e) => {
  const n = (t) => {
    let c = 1 - Math.max(-100, Math.min(-10, t)) * -1 / 100;
    return c = Math.sqrt(c), c;
  };
  return e.map((t) => t === -1 / 0 ? 0 : n(t));
}, ot = {
  bands: 5,
  loPass: 100,
  hiPass: 600,
  updateInterval: 32,
  analyserOptions: { fftSize: 2048 }
};
function Kt(e, n = {}) {
  var o;
  const t = e instanceof y ? e : (o = e == null ? void 0 : e.publication) == null ? void 0 : o.track, s = { ...ot, ...n }, [a, c] = r.useState(
    new Array(s.bands).fill(0)
  );
  return r.useEffect(() => {
    if (!t || !(t != null && t.mediaStream))
      return;
    const { analyser: u, cleanup: i } = V(t, s.analyserOptions), l = u.frequencyBinCount, d = new Float32Array(l), p = setInterval(() => {
      u.getFloatFrequencyData(d);
      let g = new Float32Array(d.length);
      for (let h = 0; h < d.length; h++)
        g[h] = d[h];
      g = g.slice(n.loPass, n.hiPass);
      const S = ct(g), b = Math.ceil(S.length / s.bands), v = [];
      for (let h = 0; h < s.bands; h++) {
        const P = S.slice(h * b, (h + 1) * b).reduce((M, C) => M += C, 0);
        v.push(P / b);
      }
      c(v);
    }, s.updateInterval);
    return () => {
      i(), clearInterval(p);
    };
  }, [t, t == null ? void 0 : t.mediaStream, JSON.stringify(n)]), a;
}
const it = {
  barCount: 120,
  volMultiplier: 5,
  updateInterval: 20
};
function en(e, n = {}) {
  var d;
  const t = e instanceof y ? e : (d = e == null ? void 0 : e.publication) == null ? void 0 : d.track, s = { ...it, ...n }, a = r.useRef(new Float32Array()), c = r.useRef(performance.now()), o = r.useRef(0), [u, i] = r.useState([]), l = r.useCallback((f) => {
    i(
      Array.from(
        lt(f, s.barCount).map((p) => Math.sqrt(p) * s.volMultiplier)
        // wave.slice(0, opts.barCount).map((v) => sigmoid(v * opts.volMultiplier, 0.08, 0.2)),
      )
    );
  }, []);
  return r.useEffect(() => {
    if (!t || !(t != null && t.mediaStream))
      return;
    const { analyser: f, cleanup: p } = V(t, {
      fftSize: U(s.barCount)
    }), g = U(s.barCount), S = new Float32Array(g), b = () => {
      if (v = requestAnimationFrame(b), f.getFloatTimeDomainData(S), a.current.map((h, P) => h + S[P]), o.current += 1, performance.now() - c.current >= s.updateInterval) {
        const h = S.map((P) => P / o.current);
        l(h), c.current = performance.now(), o.current = 0;
      }
    };
    let v = requestAnimationFrame(b);
    return () => {
      p(), cancelAnimationFrame(v);
    };
  }, [t, t == null ? void 0 : t.mediaStream, JSON.stringify(n), l]), {
    bars: u
  };
}
function U(e) {
  return e < 32 ? 32 : ut(e);
}
function ut(e) {
  let n = 2;
  for (; e >>= 1; )
    n <<= 1;
  return n;
}
function lt(e, n) {
  const t = Math.floor(e.length / n), s = new Float32Array(n);
  for (let a = 0; a < n; a++) {
    const c = t * a;
    let o = 0;
    for (let u = 0; u < t; u++)
      o = o + Math.abs(e[c + u]);
    s[a] = o / t;
  }
  return s;
}
function dt(e, n) {
  const t = w(), s = F(), a = n ? t.getParticipantByIdentity(n) : s, c = r.useMemo(
    () => a ? Ve(a, { sources: e }) : void 0,
    [a == null ? void 0 : a.sid, a == null ? void 0 : a.identity, JSON.stringify(e)]
  );
  return m(c, []);
}
function ft(e) {
  var t, s, a;
  const n = r.useMemo(
    () => {
      var c;
      return (c = e == null ? void 0 : e.publication) != null && c.track ? Be(e == null ? void 0 : e.publication.track) : void 0;
    },
    [(t = e == null ? void 0 : e.publication) == null ? void 0 : t.track]
  );
  return m(n, {
    timestamp: Date.now(),
    rtpTimestamp: (a = (s = e == null ? void 0 : e.publication) == null ? void 0 : s.track) == null ? void 0 : a.rtpTimestamp
  });
}
const pt = {
  bufferSize: 100
  // maxAge: 2_000,
};
function bt(e, n) {
  const t = { ...pt, ...n }, [s, a] = r.useState([]), c = ft(e), o = (u) => {
    var i;
    (i = t.onTranscription) == null || i.call(t, u), a(
      (l) => Ue(
        l,
        // when first receiving a segment, add the current media timestamp to it
        u.map((d) => qe(d, c)),
        t.bufferSize
      )
    );
  };
  return r.useEffect(() => {
    if (!(e != null && e.publication))
      return;
    const u = ze(e.publication).subscribe((i) => {
      o(...i);
    });
    return () => {
      u.unsubscribe();
    };
  }, [e && N(e), o]), { segments: s };
}
function mt(e = {}) {
  const n = F(), t = e.participant ?? n, s = r.useMemo(
    // weird typescript constraint
    () => t ? L(t) : L(t),
    [t]
  );
  return m(s, {
    attributes: t == null ? void 0 : t.attributes
  });
}
function tn(e, n = {}) {
  const t = T(n.participant), [s, a] = r.useState(t.attributes[e]);
  return r.useEffect(() => {
    if (!t)
      return;
    const c = L(t).subscribe((o) => {
      o.changed[e] !== void 0 && a(o.attributes[e]);
    });
    return () => {
      c.unsubscribe();
    };
  }, [t, e]), s;
}
const q = "lk.agent.state";
function nn() {
  const e = x().find((o) => o.kind === xe.AGENT), n = dt([y.Source.Microphone], e == null ? void 0 : e.identity)[0], { segments: t } = bt(n), s = O(), { attributes: a } = mt({ participant: e }), c = r.useMemo(() => s === I.Disconnected ? "disconnected" : s === I.Connecting || !e || !(a != null && a[q]) ? "connecting" : a[q], [a, e, s]);
  return {
    agent: e,
    state: c,
    audioTrack: n,
    agentTranscriptions: t,
    agentAttributes: a
  };
}
function sn(e) {
  const n = k(e), t = O(n), s = r.useMemo(() => Je(n), [n, t]);
  return m(s, n.isRecording);
}
function an(e) {
  const n = G();
  return $(() => {
    n.current = e;
  }, [e]), n.current;
}
export {
  Ct as A,
  Ft as B,
  Ut as C,
  et as D,
  yt as E,
  Et as F,
  Ze as G,
  z as H,
  Dt as I,
  Lt as J,
  Rt as K,
  Bt as L,
  x as M,
  _t as N,
  Ht as O,
  tt as P,
  $t as Q,
  Yt as R,
  Zt as S,
  en as T,
  dt as U,
  bt as V,
  nn as W,
  mt as X,
  tn as Y,
  sn as Z,
  O as a,
  Wt as b,
  Tt as c,
  wt as d,
  It as e,
  qt as f,
  Jt as g,
  xt as h,
  m as i,
  Qt as j,
  Ke as k,
  Nt as l,
  Vt as m,
  Gt as n,
  nt as o,
  At as p,
  Ye as q,
  Kt as r,
  kt as s,
  zt as t,
  Pt as u,
  an as v,
  jt as w,
  Xt as x,
  Ot as y,
  Mt as z
};
//# sourceMappingURL=hooks-CIG4j4dL.mjs.map
