var vt = Object.defineProperty;
var Y = (n) => {
  throw TypeError(n);
};
var wt = (n, t, e) => t in n ? vt(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
var I = (n, t, e) => wt(n, typeof t != "symbol" ? t + "" : t, e), b = (n, t, e) => t.has(n) || Y("Cannot " + e);
var i = (n, t, e) => (b(n, t, "read from private field"), e ? e.call(n) : t.get(n)), f = (n, t, e) => t.has(n) ? Y("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(n) : t.set(n, e), p = (n, t, e, r) => (b(n, t, "write to private field"), r ? r.call(n, e) : t.set(n, e), e), h = (n, t, e) => (b(n, t, "access private method"), e);
import { o as Et, b as _, p as Lt, d as At, Q as z, l as J, e as Dt, D as N, f as xt, g as O, T as Mt, L as q, q as Ct, r as Nt, t as Rt, h as Ft, j as Z, k as Pt, m as qt, n as _t } from "./install-CGlpWYKn.js";
import { VideoProvider as $t } from "./vidstack-video-BN5zWDNX.js";
import { R as jt } from "./vidstack-DqAw8m9J-CGDZ7qCW.js";
function W(n) {
  try {
    return new Intl.DisplayNames(navigator.languages, { type: "language" }).of(n) ?? null;
  } catch {
    return null;
  }
}
const Ht = (n) => `dash-${xt(n)}`;
var L, d, o, R, F, s, x, k, tt, it, w, P, et, st, nt, rt, ot, at, ht, dt, A, ut, Q, G, ct, V, lt, pt, U;
class It {
  constructor(t, e) {
    f(this, s);
    f(this, L);
    f(this, d);
    f(this, o, null);
    f(this, R, /* @__PURE__ */ new Set());
    f(this, F, null);
    I(this, "config", {});
    f(this, w, null);
    f(this, P, {});
    f(this, A, -1);
    p(this, L, t), p(this, d, e);
  }
  get instance() {
    return i(this, o);
  }
  setup(t) {
    p(this, o, t().create());
    const e = h(this, s, it).bind(this);
    for (const r of Object.values(t.events)) i(this, o).on(r, e);
    i(this, o).on(t.events.ERROR, h(this, s, at).bind(this));
    for (const r of i(this, R)) r(i(this, o));
    i(this, d).player.dispatch("dash-instance", {
      detail: i(this, o)
    }), i(this, o).initialize(i(this, L), void 0, !1), i(this, o).updateSettings({
      streaming: {
        text: {
          // Disabling text rendering by dash.
          defaultEnabled: !1,
          dispatchForManualRendering: !0
        },
        buffer: {
          /// Enables buffer replacement when switching bitrates for faster switching.
          fastSwitchEnabled: !0
        }
      },
      ...this.config
    }), i(this, o).on(t.events.FRAGMENT_LOADING_STARTED, h(this, s, ht).bind(this)), i(this, o).on(
      t.events.FRAGMENT_LOADING_COMPLETED,
      h(this, s, dt).bind(this)
    ), i(this, o).on(t.events.MANIFEST_LOADED, h(this, s, ot).bind(this)), i(this, o).on(t.events.QUALITY_CHANGE_RENDERED, h(this, s, rt).bind(this)), i(this, o).on(t.events.TEXT_TRACKS_ADDED, h(this, s, st).bind(this)), i(this, o).on(t.events.TRACK_CHANGE_RENDERED, h(this, s, nt).bind(this)), i(this, d).qualities[z.enableAuto] = h(this, s, ct).bind(this), J(i(this, d).qualities, "change", h(this, s, lt).bind(this)), J(i(this, d).audioTracks, "change", h(this, s, pt).bind(this)), p(this, F, Dt(h(this, s, k).bind(this)));
  }
  onInstance(t) {
    return i(this, R).add(t), () => i(this, R).delete(t);
  }
  loadSource(t) {
    var e;
    h(this, s, U).call(this), _(t.src) && ((e = i(this, o)) == null || e.attachSource(t.src));
  }
  destroy() {
    var t, e;
    h(this, s, U).call(this), (t = i(this, o)) == null || t.destroy(), p(this, o, null), (e = i(this, F)) == null || e.call(this), p(this, F, null);
  }
}
L = new WeakMap(), d = new WeakMap(), o = new WeakMap(), R = new WeakMap(), F = new WeakMap(), s = new WeakSet(), x = function(t) {
  return new N(Ht(t.type), { detail: t });
}, k = function() {
  if (!i(this, d).$state.live()) return;
  const t = new jt(h(this, s, tt).bind(this));
  return t.start(), t.stop.bind(t);
}, tt = function() {
  if (!i(this, o)) return;
  const t = i(this, o).duration() - i(this, o).time();
  i(this, d).$state.liveSyncPosition.set(isNaN(t) ? 1 / 0 : t);
}, it = function(t) {
  var e;
  (e = i(this, d).player) == null || e.dispatch(h(this, s, x).call(this, t));
}, w = new WeakMap(), P = new WeakMap(), et = function(t) {
  var u;
  const e = (u = i(this, w)) == null ? void 0 : u[O.native], r = (e == null ? void 0 : e.track).cues;
  if (!e || !r) return;
  const l = i(this, w).id, g = i(this, P)[l] ?? 0, y = h(this, s, x).call(this, t);
  for (let m = g; m < r.length; m++) {
    const c = r[m];
    c.positionAlign || (c.positionAlign = "auto"), i(this, w).addCue(c, y);
  }
  i(this, P)[l] = r.length;
}, st = function(t) {
  var g;
  if (!i(this, o)) return;
  const e = t.tracks, r = [...i(this, L).textTracks].filter((y) => "manualMode" in y), l = h(this, s, x).call(this, t);
  for (let y = 0; y < r.length; y++) {
    const u = e[y], m = r[y], c = `dash-${u.kind}-${y}`, E = new Mt({
      id: c,
      label: (u == null ? void 0 : u.label) ?? ((g = u.labels.find((a) => a.text)) == null ? void 0 : g.text) ?? ((u == null ? void 0 : u.lang) && W(u.lang)) ?? (u == null ? void 0 : u.lang) ?? void 0,
      language: u.lang ?? void 0,
      kind: u.kind,
      default: u.defaultTrack
    });
    E[O.native] = {
      managed: !0,
      track: m
    }, E[O.readyState] = 2, E[O.onModeChange] = () => {
      i(this, o) && (E.mode === "showing" ? (i(this, o).setTextTrack(y), p(this, w, E)) : (i(this, o).setTextTrack(-1), p(this, w, null)));
    }, i(this, d).textTracks.add(E, l);
  }
}, nt = function(t) {
  const { mediaType: e, newMediaInfo: r } = t;
  if (e === "audio") {
    const l = i(this, d).audioTracks.getById(`dash-audio-${r.index}`);
    if (l) {
      const g = h(this, s, x).call(this, t);
      i(this, d).audioTracks[q.select](l, !0, g);
    }
  }
}, rt = function(t) {
  if (t.mediaType !== "video") return;
  const e = i(this, d).qualities[t.newQuality];
  if (e) {
    const r = h(this, s, x).call(this, t);
    i(this, d).qualities[q.select](e, !0, r);
  }
}, ot = function(t) {
  if (i(this, d).$state.canPlay() || !i(this, o)) return;
  const { type: e, mediaPresentationDuration: r } = t.data, l = h(this, s, x).call(this, t);
  i(this, d).notify("stream-type-change", e !== "static" ? "live" : "on-demand", l), i(this, d).notify("duration-change", r, l), i(this, d).qualities[z.setAuto](!0, l);
  const g = i(this, o).getVideoElement(), y = i(this, o).getTracksForTypeFromManifest(
    "video",
    t.data
  ), u = [...new Set(y.map((a) => a.mimeType))].find(
    (a) => a && Ct(g, a)
  ), m = y.filter(
    (a) => u === a.mimeType
  )[0];
  let c = i(this, o).getTracksForTypeFromManifest(
    "audio",
    t.data
  );
  const E = [...new Set(c.map((a) => a.mimeType))].find(
    (a) => a && Nt(g, a)
  );
  if (c = c.filter((a) => E === a.mimeType), m.bitrateList.forEach((a, H) => {
    var C;
    const B = {
      id: ((C = a.id) == null ? void 0 : C.toString()) ?? `dash-bitrate-${H}`,
      width: a.width ?? 0,
      height: a.height ?? 0,
      bitrate: a.bandwidth ?? 0,
      codec: m.codec,
      index: H
    };
    i(this, d).qualities[q.add](B, l);
  }), Rt(m.index)) {
    const a = i(this, d).qualities[m.index];
    a && i(this, d).qualities[q.select](a, !0, l);
  }
  c.forEach((a, H) => {
    const C = a.labels.find((K) => navigator.languages.some((St) => K.lang && St.toLowerCase().startsWith(K.lang.toLowerCase()))) || a.labels[0], Tt = {
      id: `dash-audio-${a == null ? void 0 : a.index}`,
      label: (C == null ? void 0 : C.text) ?? (a.lang && W(a.lang)) ?? a.lang ?? "",
      language: a.lang ?? "",
      kind: "main",
      mimeType: a.mimeType,
      codec: a.codec,
      index: H
    };
    i(this, d).audioTracks[q.add](Tt, l);
  }), g.dispatchEvent(new N("canplay", { trigger: l }));
}, at = function(t) {
  const { type: e, error: r } = t;
  switch (r.code) {
    case 27:
      h(this, s, ut).call(this, r);
      break;
    default:
      h(this, s, G).call(this, r);
      break;
  }
}, ht = function() {
  i(this, A) >= 0 && h(this, s, Q).call(this);
}, dt = function(t) {
  t.mediaType === "text" && requestAnimationFrame(h(this, s, et).bind(this, t));
}, A = new WeakMap(), ut = function(t) {
  var e;
  h(this, s, Q).call(this), (e = i(this, o)) == null || e.play(), p(this, A, window.setTimeout(() => {
    p(this, A, -1), h(this, s, G).call(this, t);
  }, 5e3));
}, Q = function() {
  clearTimeout(i(this, A)), p(this, A, -1);
}, G = function(t) {
  i(this, d).notify("error", {
    message: t.message ?? "",
    code: 1,
    error: t
  });
}, ct = function() {
  var e;
  h(this, s, V).call(this, "video", !0);
  const { qualities: t } = i(this, d);
  (e = i(this, o)) == null || e.setQualityFor("video", t.selectedIndex, !0);
}, V = function(t, e) {
  var r;
  (r = i(this, o)) == null || r.updateSettings({
    streaming: { abr: { autoSwitchBitrate: { [t]: e } } }
  });
}, lt = function() {
  const { qualities: t } = i(this, d);
  !i(this, o) || t.auto || !t.selected || (h(this, s, V).call(this, "video", !1), i(this, o).setQualityFor("video", t.selectedIndex, t.switch === "current"), Ft && (i(this, L).currentTime = i(this, L).currentTime));
}, pt = function() {
  if (!i(this, o)) return;
  const { audioTracks: t } = i(this, d), e = i(this, o).getTracksFor("audio").find(
    (r) => t.selected && t.selected.id === `dash-audio-${r.index}`
  );
  e && i(this, o).setCurrentTrack(e);
}, U = function() {
  h(this, s, Q).call(this), p(this, w, null), p(this, P, {});
};
var M, S, $, v, gt, ft, yt, mt;
class Ot {
  constructor(t, e, r) {
    f(this, v);
    f(this, M);
    f(this, S);
    f(this, $);
    p(this, M, t), p(this, S, e), p(this, $, r), h(this, v, gt).call(this);
  }
}
M = new WeakMap(), S = new WeakMap(), $ = new WeakMap(), v = new WeakSet(), gt = async function() {
  const t = {
    onLoadStart: h(this, v, ft).bind(this),
    onLoaded: h(this, v, yt).bind(this),
    onLoadError: h(this, v, mt).bind(this)
  };
  let e = await bt(i(this, M), t);
  if (Z(e) && !_(i(this, M)) && (e = await Qt(i(this, M), t)), !e) return null;
  if (!window.dashjs.supportsMediaSource()) {
    const r = "[vidstack] `dash.js` is not supported in this environment";
    return i(this, S).player.dispatch(new N("dash-unsupported")), i(this, S).notify("error", { message: r, code: 4 }), null;
  }
  return e;
}, ft = function() {
  i(this, S).player.dispatch(new N("dash-lib-load-start"));
}, yt = function(t) {
  i(this, S).player.dispatch(
    new N("dash-lib-loaded", {
      detail: t
    })
  ), i(this, $).call(this, t);
}, mt = function(t) {
  const e = Pt(t);
  i(this, S).player.dispatch(
    new N("dash-lib-load-error", {
      detail: e
    })
  ), i(this, S).notify("error", {
    message: e.message,
    code: 4,
    error: e
  });
};
async function Qt(n, t = {}) {
  var e, r, l, g, y, u, m;
  if (!Z(n)) {
    if ((e = t.onLoadStart) == null || e.call(t), Gt(n))
      return (r = t.onLoaded) == null || r.call(t, n), n;
    if (X(n)) {
      const c = n.MediaPlayer;
      return (l = t.onLoaded) == null || l.call(t, c), c;
    }
    try {
      const c = (g = await n()) == null ? void 0 : g.default;
      if (X(c))
        return (y = t.onLoaded) == null || y.call(t, c.MediaPlayer), c.MediaPlayer;
      if (c)
        (u = t.onLoaded) == null || u.call(t, c);
      else
        throw Error(
          ""
        );
      return c;
    } catch (c) {
      (m = t.onLoadError) == null || m.call(t, c);
    }
  }
}
async function bt(n, t = {}) {
  var e, r, l;
  if (_(n)) {
    (e = t.onLoadStart) == null || e.call(t);
    try {
      if (await qt(n), !_t(window.dashjs.MediaPlayer))
        throw Error(
          ""
        );
      const g = window.dashjs.MediaPlayer;
      return (r = t.onLoaded) == null || r.call(t, g), g;
    } catch (g) {
      (l = t.onLoadError) == null || l.call(t, g);
    }
  }
}
function Gt(n) {
  return n && n.prototype && n.prototype !== Function;
}
function X(n) {
  return n && "MediaPlayer" in n;
}
const Vt = "https://cdn.jsdelivr.net";
var j, T, D;
class Ut extends $t {
  constructor() {
    super(...arguments);
    I(this, "$$PROVIDER_TYPE", "DASH");
    f(this, j, null);
    f(this, T, new It(this.video, this.ctx));
    f(this, D, `${Vt}/npm/dashjs@4.7.4/dist/dash.all.min.js`);
  }
  /**
   * The `dash.js` constructor.
   */
  get ctor() {
    return i(this, j);
  }
  /**
   * The current `dash.js` instance.
   */
  get instance() {
    return i(this, T).instance;
  }
  get type() {
    return "dash";
  }
  get canLiveSync() {
    return !0;
  }
  /**
   * The `dash.js` configuration object.
   *
   * @see {@link https://cdn.dashjs.org/latest/jsdoc/module-Settings.html}
   */
  get config() {
    return i(this, T).config;
  }
  set config(e) {
    i(this, T).config = e;
  }
  /**
   * The `dash.js` constructor (supports dynamic imports) or a URL of where it can be found.
   *
   * @defaultValue `https://cdn.jsdelivr.net/npm/dashjs@4.7.4/dist/dash.all.min.js`
   */
  get library() {
    return i(this, D);
  }
  set library(e) {
    p(this, D, e);
  }
  preconnect() {
    _(i(this, D)) && Lt(i(this, D));
  }
  setup() {
    super.setup(), new Ot(i(this, D), this.ctx, (e) => {
      p(this, j, e), i(this, T).setup(e), this.ctx.notify("provider-setup", this);
      const r = At(this.ctx.$state.source);
      r && this.loadSource(r);
    });
  }
  async loadSource(e, r) {
    if (!_(e.src)) {
      this.removeSource();
      return;
    }
    this.media.preload = r || "", this.appendSource(e, "application/x-mpegurl"), i(this, T).loadSource(e), this.currentSrc = e;
  }
  /**
   * The given callback is invoked when a new `dash.js` instance is created and right before it's
   * attached to media.
   */
  onInstance(e) {
    const r = i(this, T).instance;
    return r && e(r), i(this, T).onInstance(e);
  }
  destroy() {
    i(this, T).destroy();
  }
}
j = new WeakMap(), T = new WeakMap(), D = new WeakMap(), /**
 * Whether `dash.js` is supported in this environment.
 */
I(Ut, "supported", Et());
export {
  Ut as DASHProvider
};
