import { E as e, F as t, G as n, L as r, M as i, V as a, f as o, r as s, v as c } from "./vidstack-D9mvYVIz-DesuYgKV.js";
import { _ as l, f as u, r as d, u as f } from "./useVidstackSrc-epUQZhkL.js";
import { i as p, r as m } from "./vidstack-mvRkf9tF-CoFf7dS9.js";
import { n as h, t as g } from "./vidstack-ijoHbXQB-DBClSWHE.js";
import { n as _, t as v } from "./vidstack-DYPyTdYw-BzqKxwCK.js";
import { t as y } from "./vidstack-_E7LrUjK-DaCLs8ge.js";
import { t as b } from "./vidstack-C6nBVWwn-BlZqtkYB.js";
import { n as x } from "./vidstack-CDOoBnJL-BCwLE1fB.js";
//#region node_modules/vidstack/prod/chunks/vidstack-cIhilC9U.js
function S(e) {
	try {
		return new Intl.DisplayNames(navigator.languages, { type: "language" }).of(e) ?? null;
	} catch {
		return null;
	}
}
var C = (e) => `dash-${o(e)}`, w = class {
	#e;
	#t;
	#n = null;
	#r = /* @__PURE__ */ new Set();
	#i = null;
	config = {};
	get instance() {
		return this.#n;
	}
	constructor(e, t) {
		this.#e = e, this.#t = t;
	}
	setup(e) {
		this.#n = e().create();
		let t = this.#c.bind(this);
		for (let n of Object.values(e.events)) this.#n.on(n, t);
		this.#n.on(e.events.ERROR, this.#g.bind(this));
		for (let e of this.#r) e(this.#n);
		this.#t.player.dispatch("dash-instance", { detail: this.#n }), this.#n.initialize(this.#e, void 0, !1), this.#n.updateSettings({
			streaming: {
				text: {
					defaultEnabled: !1,
					dispatchForManualRendering: !0
				},
				buffer: { fastSwitchEnabled: !0 }
			},
			...this.config
		}), this.#n.on(e.events.FRAGMENT_LOADING_STARTED, this.#_.bind(this)), this.#n.on(e.events.FRAGMENT_LOADING_COMPLETED, this.#v.bind(this)), this.#n.on(e.events.MANIFEST_LOADED, this.#h.bind(this)), this.#n.on(e.events.QUALITY_CHANGE_RENDERED, this.#m.bind(this)), this.#n.on(e.events.TEXT_TRACKS_ADDED, this.#f.bind(this)), this.#n.on(e.events.TRACK_CHANGE_RENDERED, this.#p.bind(this)), this.#t.qualities[y.enableAuto] = this.#C.bind(this), a(this.#t.qualities, "change", this.#T.bind(this)), a(this.#t.audioTracks, "change", this.#E.bind(this)), this.#i = c(this.#o.bind(this));
	}
	#a(e) {
		return new s(C(e.type), { detail: e });
	}
	#o() {
		if (!this.#t.$state.live()) return;
		let e = new h(this.#s.bind(this));
		return e.start(), e.stop.bind(e);
	}
	#s() {
		if (!this.#n) return;
		let e = this.#n.duration() - this.#n.time();
		this.#t.$state.liveSyncPosition.set(isNaN(e) ? Infinity : e);
	}
	#c(e) {
		this.#t.player?.dispatch(this.#a(e));
	}
	#l = null;
	#u = {};
	#d(e) {
		let t = this.#l?.[_.native], n = (t?.track).cues;
		if (!t || !n) return;
		let r = this.#l.id, i = this.#u[r] ?? 0, a = this.#a(e);
		for (let e = i; e < n.length; e++) {
			let t = n[e];
			t.positionAlign ||= "auto", this.#l.addCue(t, a);
		}
		this.#u[r] = n.length;
	}
	#f(e) {
		if (!this.#n) return;
		let t = e.tracks, n = [...this.#e.textTracks].filter((e) => "manualMode" in e), r = this.#a(e);
		for (let e = 0; e < n.length; e++) {
			let i = t[e], a = n[e], o = new v({
				id: `dash-${i.kind}-${e}`,
				label: i?.label ?? i.labels.find((e) => e.text)?.text ?? (i?.lang && S(i.lang)) ?? i?.lang ?? void 0,
				language: i.lang ?? void 0,
				kind: i.kind,
				default: i.defaultTrack
			});
			o[_.native] = {
				managed: !0,
				track: a
			}, o[_.readyState] = 2, o[_.onModeChange] = () => {
				this.#n && (o.mode === "showing" ? (this.#n.setTextTrack(e), this.#l = o) : (this.#n.setTextTrack(-1), this.#l = null));
			}, this.#t.textTracks.add(o, r);
		}
	}
	#p(e) {
		let { mediaType: t, newMediaInfo: n } = e;
		if (t === "audio") {
			let t = this.#t.audioTracks.getById(`dash-audio-${n.index}`);
			if (t) {
				let n = this.#a(e);
				this.#t.audioTracks[g.select](t, !0, n);
			}
		}
	}
	#m(e) {
		if (e.mediaType !== "video") return;
		let t = this.#t.qualities[e.newQuality];
		if (t) {
			let n = this.#a(e);
			this.#t.qualities[g.select](t, !0, n);
		}
	}
	#h(e) {
		if (this.#t.$state.canPlay() || !this.#n) return;
		let { type: t, mediaPresentationDuration: n } = e.data, r = this.#a(e);
		this.#t.notify("stream-type-change", t === "static" ? "on-demand" : "live", r), this.#t.notify("duration-change", n, r), this.#t.qualities[y.setAuto](!0, r);
		let a = this.#n.getVideoElement(), o = this.#n.getTracksForTypeFromManifest("video", e.data), c = [...new Set(o.map((e) => e.mimeType))].find((e) => e && u(a, e)), l = o.find((e) => c === e.mimeType), d = this.#n.getTracksForTypeFromManifest("audio", e.data), p = [...new Set(d.map((e) => e.mimeType))].find((e) => e && f(a, e));
		if (d = d.filter((e) => p === e.mimeType), l && (l.bitrateList.forEach((e, t) => {
			let n = {
				id: e.id?.toString() ?? `dash-bitrate-${t}`,
				width: e.width ?? 0,
				height: e.height ?? 0,
				bitrate: e.bandwidth ?? 0,
				codec: l.codec,
				index: t
			};
			this.#t.qualities[g.add](n, r);
		}), i(l.index))) {
			let e = this.#t.qualities[l.index];
			e && this.#t.qualities[g.select](e, !0, r);
		}
		d.forEach((e, t) => {
			let n = e.labels.find((e) => navigator.languages.some((t) => e.lang && t.toLowerCase().startsWith(e.lang.toLowerCase()))) || e.labels[0], i = {
				id: `dash-audio-${e?.index}`,
				label: n?.text ?? (e.lang && S(e.lang)) ?? e.lang ?? "",
				language: e.lang ?? "",
				kind: "main",
				mimeType: e.mimeType,
				codec: e.codec,
				index: t
			};
			this.#t.audioTracks[g.add](i, r);
		}), a.dispatchEvent(new s("canplay", { trigger: r }));
	}
	#g(e) {
		let { type: t, error: n } = e;
		switch (n.code) {
			case 27:
				this.#b(n);
				break;
			default:
				this.#S(n);
				break;
		}
	}
	#_() {
		this.#y >= 0 && this.#x();
	}
	#v(e) {
		e.mediaType === "text" && requestAnimationFrame(this.#d.bind(this, e));
	}
	#y = -1;
	#b(e) {
		this.#x(), this.#n?.play(), this.#y = window.setTimeout(() => {
			this.#y = -1, this.#S(e);
		}, 5e3);
	}
	#x() {
		clearTimeout(this.#y), this.#y = -1;
	}
	#S(e) {
		this.#t.notify("error", {
			message: e.message ?? "",
			code: 1,
			error: e
		});
	}
	#C() {
		this.#w("video", !0);
		let { qualities: e } = this.#t;
		this.#n?.setQualityFor("video", e.selectedIndex, !0);
	}
	#w(e, t) {
		this.#n?.updateSettings({ streaming: { abr: { autoSwitchBitrate: { [e]: t } } } });
	}
	#T() {
		let { qualities: e } = this.#t;
		!this.#n || e.auto || !e.selected || (this.#w("video", !1), this.#n.setQualityFor("video", e.selectedIndex, e.switch === "current"), d && (this.#e.currentTime = this.#e.currentTime));
	}
	#E() {
		if (!this.#n) return;
		let { audioTracks: e } = this.#t, t = this.#n.getTracksFor("audio").find((t) => e.selected && e.selected.id === `dash-audio-${t.index}`);
		t && this.#n.setCurrentTrack(t);
	}
	#D() {
		this.#x(), this.#l = null, this.#u = {};
	}
	onInstance(e) {
		return this.#r.add(e), () => this.#r.delete(e);
	}
	loadSource(e) {
		this.#D(), t(e.src) && this.#n?.attachSource(e.src);
	}
	destroy() {
		this.#D(), this.#n?.destroy(), this.#n = null, this.#i?.(), this.#i = null;
	}
}, T = class {
	#e;
	#t;
	#n;
	constructor(e, t, n) {
		this.#e = e, this.#t = t, this.#n = n, this.#r();
	}
	async #r() {
		let e = {
			onLoadStart: this.#i.bind(this),
			onLoaded: this.#a.bind(this),
			onLoadError: this.#o.bind(this)
		}, n = await D(this.#e, e);
		return r(n) && !t(this.#e) && (n = await E(this.#e, e)), n ? window.dashjs.supportsMediaSource() ? n : (this.#t.player.dispatch(new s("dash-unsupported")), this.#t.notify("error", {
			message: "[vidstack] `dash.js` is not supported in this environment",
			code: 4
		}), null) : null;
	}
	#i() {
		this.#t.player.dispatch(new s("dash-lib-load-start"));
	}
	#a(e) {
		this.#t.player.dispatch(new s("dash-lib-loaded", { detail: e })), this.#n(e);
	}
	#o(e) {
		let t = x(e);
		this.#t.player.dispatch(new s("dash-lib-load-error", { detail: t })), this.#t.notify("error", {
			message: t.message,
			code: 4,
			error: t
		});
	}
};
async function E(e, t = {}) {
	if (!r(e)) {
		if (t.onLoadStart?.(), O(e)) return t.onLoaded?.(e), e;
		if (k(e)) {
			let n = e.MediaPlayer;
			return t.onLoaded?.(n), n;
		}
		try {
			let n = (await e())?.default;
			if (k(n)) return t.onLoaded?.(n.MediaPlayer), n.MediaPlayer;
			if (n) t.onLoaded?.(n);
			else throw Error("");
			return n;
		} catch (e) {
			t.onLoadError?.(e);
		}
	}
}
async function D(n, r = {}) {
	if (t(n)) {
		r.onLoadStart?.();
		try {
			if (await m(n), !e(window.dashjs.MediaPlayer)) throw Error("");
			let t = window.dashjs.MediaPlayer;
			return r.onLoaded?.(t), t;
		} catch (e) {
			r.onLoadError?.(e);
		}
	}
}
function O(e) {
	return e && e.prototype && e.prototype !== Function;
}
function k(e) {
	return e && "MediaPlayer" in e;
}
var A = "https://cdn.jsdelivr.net", j = class extends b {
	$$PROVIDER_TYPE = "DASH";
	#e = null;
	#t = new w(this.video, this.ctx);
	get ctor() {
		return this.#e;
	}
	get instance() {
		return this.#t.instance;
	}
	static supported = l();
	get type() {
		return "dash";
	}
	get canLiveSync() {
		return !0;
	}
	#n = `${A}/npm/dashjs@4.7.4/dist/dash.all.min.js`;
	get config() {
		return this.#t.config;
	}
	set config(e) {
		this.#t.config = e;
	}
	get library() {
		return this.#n;
	}
	set library(e) {
		this.#n = e;
	}
	preconnect() {
		t(this.#n) && p(this.#n);
	}
	setup() {
		super.setup(), new T(this.#n, this.ctx, (e) => {
			this.#e = e, this.#t.setup(e), this.ctx.notify("provider-setup", this);
			let t = n(this.ctx.$state.source);
			t && this.loadSource(t);
		});
	}
	async loadSource(e, n) {
		if (!t(e.src)) {
			this.removeSource();
			return;
		}
		this.media.preload = n || "", this.appendSource(e, "application/x-mpegurl"), this.#t.loadSource(e), this.currentSrc = e;
	}
	onInstance(e) {
		let t = this.#t.instance;
		return t && e(t), this.#t.onInstance(e);
	}
	destroy() {
		this.#t.destroy();
	}
};
//#endregion
export { j as DASHProvider };
