import { ab as k, o as v, k as r, ak as S, d as w, n as f, m as b, al as o, U as P, G as C, x as E, am as A, l as m, D as x, v as l } from "./useVidstackSrc-CNo2sIR4.js";
import { R as L } from "./vidstack-DqAw8m9J-kmvNQesV.js";
import { g as y, L as u } from "./wt-vidstack-player-DL-84dHQ.js";
let h = null, d = [], c = [];
function p() {
  return h ??= new AudioContext();
}
function G() {
  const s = p(), t = s.createGain();
  return t.connect(s.destination), d.push(t), t;
}
function $(s, t) {
  const i = p(), e = i.createMediaElementSource(s);
  return t && e.connect(t), c.push(e), e;
}
function N(s) {
  const t = d.indexOf(s);
  t !== -1 && (d.splice(t, 1), s.disconnect(), T());
}
function M(s) {
  const t = c.indexOf(s);
  t !== -1 && (c.splice(t, 1), s.disconnect(), T());
}
function T() {
  h && d.length === 0 && c.length === 0 && h.close().then(() => {
    h = null;
  });
}
class R {
  #s;
  #t;
  #e = null;
  #a = null;
  get currentGain() {
    return this.#e?.gain?.value ?? null;
  }
  get supported() {
    return !0;
  }
  constructor(t, i) {
    this.#s = t, this.#t = i;
  }
  setGain(t) {
    const i = this.currentGain;
    if (t !== this.currentGain) {
      if (t === 1 && i !== 1) {
        this.removeGain();
        return;
      }
      this.#e || (this.#e = G(), this.#a && this.#a.connect(this.#e)), this.#a || (this.#a = $(this.#s, this.#e)), this.#e.gain.value = t, this.#t(t);
    }
  }
  removeGain() {
    this.#e && (this.#a && this.#a.connect(p().destination), this.#r(), this.#t(null));
  }
  destroy() {
    this.#h(), this.#r();
  }
  #h() {
    if (this.#a)
      try {
        M(this.#a);
      } catch {
      } finally {
        this.#a = null;
      }
  }
  #r() {
    if (this.#e)
      try {
        N(this.#e);
      } catch {
      } finally {
        this.#e = null;
      }
  }
}
const D = ["focus", "blur", "visibilitychange", "pageshow", "pagehide"];
class O {
  #s = l(g());
  #t = l(document.visibilityState);
  #e;
  connect() {
    const t = new f(window), i = this.#a.bind(this);
    for (const e of D)
      t.add(e, i);
    o && t.add("beforeunload", (e) => {
      this.#e = setTimeout(() => {
        e.defaultPrevented || e.returnValue.length > 0 || (this.#s.set("hidden"), this.#t.set("hidden"));
      }, 0);
    });
  }
  /**
   * The current page state. Important to note we only account for a subset of page states, as
   * the rest aren't valuable to the player at the moment.
   *
   * - **active:** A page is in the active state if it is visible and has input focus.
   * - **passive:** A page is in the passive state if it is visible and does not have input focus.
   * - **hidden:** A page is in the hidden state if it is not visible.
   *
   * @see https://developers.google.com/web/updates/2018/07/page-lifecycle-api#states
   */
  get pageState() {
    return this.#s();
  }
  /**
   * The current document visibility state.
   *
   * - **visible:** The page content may be at least partially visible. In practice, this means that
   * the page is the foreground tab of a non-minimized window.
   * - **hidden:** The page content is not visible to the user. In practice this means that the
   * document is either a background tab or part of a minimized window, or the OS screen lock is
   * active.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState
   */
  get visibility() {
    return this.#t();
  }
  #a(t) {
    o && window.clearTimeout(this.#e), (t.type !== "blur" || this.#s() === "active") && (this.#s.set(g(t)), this.#t.set(document.visibilityState == "hidden" ? "hidden" : "visible"));
  }
}
function g(s) {
  return s?.type === "blur" || document.visibilityState === "hidden" ? "hidden" : document.hasFocus() ? "active" : "passive";
}
class I {
  #s;
  #t;
  #e = !1;
  #a = !1;
  #h = !1;
  #r = new L(this.#y.bind(this));
  #o = new O();
  #c;
  get #i() {
    return this.#s.media;
  }
  constructor(t, i) {
    this.#s = t, this.#t = i, this.#c = new f(t.media), this.#g(), this.#o.connect(), b(this.#M.bind(this)), v(this.#p.bind(this));
  }
  #p() {
    this.#a = !1, this.#h = !1, this.#r.stop(), this.#c.abort(), this.#T?.clear();
  }
  /**
   * The `timeupdate` event fires surprisingly infrequently during playback, meaning your progress
   * bar (or whatever else is synced to the currentTime) moves in a choppy fashion. This helps
   * resolve that by retrieving time updates in a request animation frame loop.
   */
  #u = 0;
  #l = -1;
  #y() {
    const t = this.#i.currentTime;
    !(o && t - this.#l < 0.35) && this.#u !== t && (this.#d(t), this.#u = t);
  }
  #g() {
    this.#n("loadstart", this.#k), this.#n("abort", this.#m), this.#n("emptied", this.#S), this.#n("error", this.#j), this.#n("volumechange", this.#O);
  }
  #v() {
    this.#a || (this.#n("loadeddata", this.#w), this.#n("loadedmetadata", this.#P), this.#n("canplay", this.#A), this.#n("canplaythrough", this.#x), this.#n("durationchange", this.#D), this.#n("play", this.#C), this.#n("progress", this.#U), this.#n("stalled", this.#G), this.#n("suspend", this.#F), this.#n("ratechange", this.#V), this.#a = !0);
  }
  #b() {
    this.#h || (this.#n("pause", this.#E), this.#n("playing", this.#L), this.#n("seeked", this.#I), this.#n("seeking", this.#H), this.#n("ended", this.#N), this.#n("waiting", this.#$), this.#h = !0);
  }
  #T = void 0;
  #q = void 0;
  #n(t, i) {
    this.#c.add(t, i.bind(this));
  }
  #B(t) {
  }
  #d(t, i) {
    const e = Math.min(t, this.#t.$state.seekableEnd());
    this.#t.notify("time-change", e, i);
  }
  #k(t) {
    if (this.#i.networkState === 3) {
      this.#m(t);
      return;
    }
    this.#v(), this.#t.notify("load-start", void 0, t);
  }
  #m(t) {
    this.#t.notify("abort", void 0, t);
  }
  #S() {
    this.#t.notify("emptied", void 0, event);
  }
  #w(t) {
    this.#t.notify("loaded-data", void 0, t);
  }
  #P(t) {
    this.#u = 0, this.#l = -1, this.#b(), this.#t.notify("loaded-metadata", void 0, t), (P || o && C(this.#t.$state.source())) && this.#t.delegate.ready(this.#f(), t);
  }
  #f() {
    return {
      provider: E(this.#t.$provider),
      duration: this.#i.duration,
      buffered: this.#i.buffered,
      seekable: this.#i.seekable
    };
  }
  #C(t) {
    this.#t.$state.canPlay && this.#t.notify("play", void 0, t);
  }
  #E(t) {
    this.#i.readyState === 1 && !this.#e || (this.#e = !1, this.#r.stop(), this.#t.notify("pause", void 0, t));
  }
  #A(t) {
    this.#t.delegate.ready(this.#f(), t);
  }
  #x(t) {
    this.#t.$state.started() || this.#t.notify("can-play-through", this.#f(), t);
  }
  #L(t) {
    this.#i.paused || (this.#e = !1, this.#t.notify("playing", void 0, t), this.#r.start());
  }
  #G(t) {
    this.#t.notify("stalled", void 0, t), this.#i.readyState < 3 && (this.#e = !0, this.#t.notify("waiting", void 0, t));
  }
  #$(t) {
    this.#i.readyState < 3 && (this.#e = !0, this.#t.notify("waiting", void 0, t));
  }
  #N(t) {
    this.#r.stop(), this.#d(this.#i.duration, t), this.#t.notify("end", void 0, t), this.#t.$state.loop() && A(this.#i.controls) && (this.#i.controls = !1);
  }
  #M() {
    const t = this.#t.$state.paused(), i = this.#o.visibility === "hidden";
    (t || i) && m(this.#i, "timeupdate", this.#R.bind(this));
  }
  #R(t) {
    this.#d(this.#i.currentTime, t);
  }
  #D(t) {
    this.#t.$state.ended() && this.#d(this.#i.duration, t), this.#t.notify("duration-change", this.#i.duration, t);
  }
  #O(t) {
    const i = {
      volume: this.#i.volume,
      muted: this.#i.muted
    };
    this.#t.notify("volume-change", i, t);
  }
  #I(t) {
    this.#l = this.#i.currentTime, this.#d(this.#i.currentTime, t), this.#t.notify("seeked", this.#i.currentTime, t), Math.trunc(this.#i.currentTime) === Math.trunc(this.#i.duration) && y(this.#i.duration) > y(this.#i.currentTime) && (this.#d(this.#i.duration, t), this.#i.ended || this.#t.player.dispatch(
      new x("media-play-request", {
        trigger: t
      })
    ));
  }
  #H(t) {
    this.#t.notify("seeking", this.#i.currentTime, t);
  }
  #U(t) {
    const i = {
      buffered: this.#i.buffered,
      seekable: this.#i.seekable
    };
    this.#t.notify("progress", i, t);
  }
  #F(t) {
    this.#t.notify("suspend", void 0, t);
  }
  #V(t) {
    this.#t.notify("rate-change", this.#i.playbackRate, t);
  }
  #j(t) {
    const i = this.#i.error;
    if (!i) return;
    const e = {
      message: i.message,
      code: i.code,
      mediaError: i
    };
    this.#t.notify("error", e, t);
  }
}
class H {
  #s;
  #t;
  get #e() {
    return this.#s.media.audioTracks;
  }
  constructor(t, i) {
    this.#s = t, this.#t = i, this.#e.onaddtrack = this.#a.bind(this), this.#e.onremovetrack = this.#h.bind(this), this.#e.onchange = this.#r.bind(this), m(this.#t.audioTracks, "change", this.#c.bind(this));
  }
  #a(t) {
    const i = t.track;
    if (i.label === "") return;
    const e = i.id.toString() || `native-audio-${this.#t.audioTracks.length}`, a = {
      id: e,
      label: i.label,
      language: i.language,
      kind: i.kind,
      selected: !1
    };
    this.#t.audioTracks[u.add](a, t), i.enabled && (a.selected = !0);
  }
  #h(t) {
    const i = this.#t.audioTracks.getById(t.track.id);
    i && this.#t.audioTracks[u.remove](i, t);
  }
  #r(t) {
    let i = this.#o();
    if (!i) return;
    const e = this.#t.audioTracks.getById(i.id);
    e && this.#t.audioTracks[u.select](e, !0, t);
  }
  #o() {
    return Array.from(this.#e).find((t) => t.enabled);
  }
  #c(t) {
    const { current: i } = t.detail;
    if (!i) return;
    const e = this.#e.getTrackById(i.id);
    if (e) {
      const a = this.#o();
      a && (a.enabled = !1), e.enabled = !0;
    }
  }
}
class q {
  constructor(t, i) {
    this.media = t, this.ctx = i, this.audioGain = new R(t, (e) => {
      this.ctx.notify("audio-gain-change", e);
    });
  }
  scope = k();
  currentSrc = null;
  audioGain;
  setup() {
    new I(this, this.ctx), "audioTracks" in this.media && new H(this, this.ctx), v(() => {
      this.audioGain.destroy(), this.media.srcObject = null, this.media.removeAttribute("src");
      for (const t of this.media.querySelectorAll("source")) t.remove();
      this.media.load();
    });
  }
  get type() {
    return "";
  }
  setPlaybackRate(t) {
    this.media.playbackRate = t;
  }
  async play() {
    return this.media.play();
  }
  async pause() {
    return this.media.pause();
  }
  setMuted(t) {
    this.media.muted = t;
  }
  setVolume(t) {
    this.media.volume = t;
  }
  setCurrentTime(t) {
    this.media.currentTime = t;
  }
  setPlaysInline(t) {
    r(this.media, "playsinline", t);
  }
  async loadSource({ src: t, type: i }, e) {
    this.media.preload = e || "", S(t) ? (this.removeSource(), this.media.srcObject = t) : (this.media.srcObject = null, w(t) ? i !== "?" ? this.appendSource({ src: t, type: i }) : (this.removeSource(), this.media.src = this.#s(t)) : (this.removeSource(), this.media.src = window.URL.createObjectURL(t))), this.media.load(), this.currentSrc = { src: t, type: i };
  }
  /**
   * Append source so it works when requesting AirPlay since hls.js will remove it.
   */
  appendSource(t, i) {
    const e = this.media.querySelector("source[data-vds]"), a = e ?? document.createElement("source");
    r(a, "src", this.#s(t.src)), r(a, "type", t.type !== "?" ? t.type : i), r(a, "data-vds", ""), e || this.media.append(a);
  }
  removeSource() {
    this.media.querySelector("source[data-vds]")?.remove();
  }
  #s(t) {
    const { clipStartTime: i, clipEndTime: e } = this.ctx.$state, a = i(), n = e();
    return a > 0 && n > 0 ? `${t}#t=${a},${n}` : a > 0 ? `${t}#t=${a}` : n > 0 ? `${t}#t=0,${n}` : t;
  }
}
class U {
  #s;
  #t;
  #e;
  #a = l(!1);
  get supported() {
    return this.#a();
  }
  constructor(t, i) {
    this.#s = t, this.#t = i, this.#h();
  }
  #h() {
    !this.#s?.remote || !this.canPrompt || (this.#s.remote.watchAvailability((t) => {
      this.#a.set(t);
    }).catch(() => {
      this.#a.set(!1);
    }), b(this.#r.bind(this)));
  }
  #r() {
    if (!this.#a()) return;
    const t = ["connecting", "connect", "disconnect"], i = this.#o.bind(this);
    i(), m(this.#s, "playing", i);
    const e = new f(this.#s.remote);
    for (const a of t)
      e.add(a, i);
  }
  async prompt() {
    if (!this.supported) throw Error("Not supported on this platform.");
    return this.type === "airplay" && this.#s.webkitShowPlaybackTargetPicker ? this.#s.webkitShowPlaybackTargetPicker() : this.#s.remote.prompt();
  }
  #o(t) {
    const i = this.#s.remote.state;
    if (i === this.#e) return;
    const e = { type: this.type, state: i };
    this.#t.notify("remote-playback-change", e, t), this.#e = i;
  }
}
class B extends U {
  type = "airplay";
  get canPrompt() {
    return "WebKitPlaybackTargetAvailabilityEvent" in window;
  }
}
export {
  q as H,
  B as a
};
