import * as s from "react";
import { l, s as x, R as V, L as j } from "./contexts-94kvHczU.mjs";
import { Room as U, RoomEvent as a, MediaDeviceFailure as _ } from "@cc-livekit/livekit-client";
function L(n) {
  var e, o, t = "";
  if (typeof n == "string" || typeof n == "number") t += n;
  else if (typeof n == "object") if (Array.isArray(n)) {
    var r = n.length;
    for (e = 0; e < r; e++) n[e] && (o = L(n[e])) && (t && (t += " "), t += o);
  } else for (o in n) n[o] && (t && (t += " "), t += o);
  return t;
}
function M() {
  for (var n, e, o = 0, t = "", r = arguments.length; o < r; o++) (n = arguments[o]) && (e = L(n)) && (t && (t += " "), t += e);
  return t;
}
function I(...n) {
  return (...e) => {
    for (const o of n)
      if (typeof o == "function")
        try {
          o(...e);
        } catch (t) {
          console.error(t);
        }
  };
}
function A(...n) {
  const e = { ...n[0] };
  for (let o = 1; o < n.length; o++) {
    const t = n[o];
    for (const r in t) {
      const m = e[r], u = t[r];
      typeof m == "function" && typeof u == "function" && // This is a lot faster than a regex.
      r[0] === "o" && r[1] === "n" && r.charCodeAt(2) >= /* 'A' */
      65 && r.charCodeAt(2) <= /* 'Z' */
      90 ? e[r] = I(m, u) : (r === "className" || r === "UNSAFE_className") && typeof m == "string" && typeof u == "string" ? e[r] = M(m, u) : e[r] = u !== void 0 ? u : m;
    }
  }
  return e;
}
function J(n) {
  return n !== void 0;
}
function H(...n) {
  return A(...n.filter(J));
}
function Q(n, e, o) {
  return s.Children.map(n, (t) => s.isValidElement(t) && s.Children.only(n) ? (t.props.class && (e ?? (e = {}), e.class = M(t.props.class, e.class), e.style = { ...t.props.style, ...e.style }), s.cloneElement(t, { ...e, key: o })) : t);
}
function W(n) {
  var e, o;
  if (typeof window < "u" && typeof process < "u" && // eslint-disable-next-line turbo/no-undeclared-env-vars
  (((e = process == null ? void 0 : process.env) == null ? void 0 : e.NODE_ENV) === "dev" || // eslint-disable-next-line turbo/no-undeclared-env-vars
  ((o = process == null ? void 0 : process.env) == null ? void 0 : o.NODE_ENV) === "development")) {
    const t = document.querySelector(".lk-room-container");
    t && !getComputedStyle(t).getPropertyValue("--lk-has-imported-styles") && l.warn(
      "It looks like you're not using the `@livekit/components-styles package`. To render the UI with the default styling, please import it in your layout or page."
    );
  }
}
function T(n, e) {
  return n === "processor" && e && typeof e == "object" && "name" in e ? e.name : n === "e2ee" && e ? "e2ee-enabled" : e;
}
const q = {
  connect: !0,
  audio: !1,
  video: !1,
  prepareConnection: !0
};
function $(n) {
  const {
    token: e,
    serverUrl: o,
    options: t,
    room: r,
    connectOptions: m,
    connect: u,
    audio: p,
    video: y,
    screen: g,
    onConnected: v,
    onDisconnected: h,
    onError: c,
    onMediaDeviceFailure: b,
    onEncryptionError: E,
    simulateParticipants: C,
    prepareConnection: P,
    featureFlags: z,
    ...R
  } = { ...q, ...n };
  t && r && l.warn(
    "when using a manually created room, the options object will be ignored. set the desired options directly when creating the room instead."
  );
  const [i, F] = s.useState();
  s.useEffect(() => {
    F(r ?? new U(t));
  }, [r, JSON.stringify(t, T)]);
  const O = s.useMemo(() => i && o && P && e ? i.prepareConnection(o, e) : new Promise((f) => f(void 0)), [o, P, e, i]), K = s.useMemo(() => {
    const { className: f } = x();
    return A(R, { className: f });
  }, [R]);
  return s.useEffect(() => {
    if (!i) return;
    const f = () => {
      const d = i.localParticipant;
      l.debug("trying to publish local tracks"), Promise.all([
        d.setMicrophoneEnabled(!!p, typeof p != "boolean" ? p : void 0),
        d.setCameraEnabled(!!y, typeof y != "boolean" ? y : void 0),
        d.setScreenShareEnabled(!!g, typeof g != "boolean" ? g : void 0)
      ]).catch((w) => {
        l.warn(w), c == null || c(w);
      });
    }, S = (d) => {
      const w = _.getFailure(d);
      b == null || b(w);
    }, k = (d) => {
      E == null || E(d);
    }, N = (d) => {
      h == null || h(d);
    }, D = () => {
      v == null || v();
    };
    return i.on(a.SignalConnected, f).on(a.MediaDevicesError, S).on(a.EncryptionError, k).on(a.Disconnected, N).on(a.Connected, D), () => {
      i.off(a.SignalConnected, f).off(a.MediaDevicesError, S).off(a.EncryptionError, k).off(a.Disconnected, N).off(a.Connected, D);
    };
  }, [
    i,
    p,
    y,
    g,
    c,
    E,
    b,
    v,
    h
  ]), s.useEffect(() => {
    if (i) {
      if (C) {
        i.simulateParticipants({
          participants: {
            count: C
          },
          publish: {
            audio: !0,
            useRealTracks: !0
          }
        });
        return;
      }
      if (!e) {
        l.debug("no token yet");
        return;
      }
      if (!o) {
        l.warn("no livekit url provided"), c == null || c(Error("no livekit url provided"));
        return;
      }
      u ? (l.debug("connecting"), O.then(
        () => i.connect(o, e, m).catch((f) => {
          l.warn(f), c == null || c(f);
        })
      )) : (l.debug("disconnecting because connect is false"), i.disconnect());
    }
  }, [
    u,
    e,
    JSON.stringify(m),
    i,
    c,
    o,
    C
  ]), s.useEffect(() => {
    if (i)
      return () => {
        l.info("disconnecting on onmount"), i.disconnect();
      };
  }, [i]), { room: i, htmlProps: K };
}
const X = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { room: t, htmlProps: r } = $(e);
  return /* @__PURE__ */ s.createElement("div", { ref: o, ...r }, t && /* @__PURE__ */ s.createElement(V.Provider, { value: t }, /* @__PURE__ */ s.createElement(j.Provider, { value: e.featureFlags }, e.children)));
});
export {
  X as L,
  H as a,
  Q as c,
  A as m,
  T as r,
  $ as u,
  W as w
};
//# sourceMappingURL=room-C7g9NN9R.mjs.map
