import { E as e, F as t, G as n, L as r, V as i, f as a, r as o, v as s } from "./vidstack-D9mvYVIz-DesuYgKV.js";
import { r as c, y as l } from "./useVidstackSrc-epUQZhkL.js";
import { i as u, r as d } from "./vidstack-mvRkf9tF-CoFf7dS9.js";
import { n as f, t as p } from "./vidstack-ijoHbXQB-DBClSWHE.js";
import { n as m, t as h } from "./vidstack-DYPyTdYw-BzqKxwCK.js";
import { t as g } from "./vidstack-_E7LrUjK-DaCLs8ge.js";
import { t as _ } from "./vidstack-C6nBVWwn-BlZqtkYB.js";
import { n as v } from "./vidstack-CDOoBnJL-BCwLE1fB.js";
//#region node_modules/vidstack/prod/providers/vidstack-hls.js
var y = (e) => a(e), b = class {
	#e;
	#t;
	#n = null;
	#r = null;
	config = {};
	#i = /* @__PURE__ */ new Set();
	get instance() {
		return this.#n;
	}
	constructor(e, t) {
		this.#e = e, this.#t = t;
	}
	setup(e) {
		let { streamType: t } = this.#t.$state, r = n(t).includes("live"), a = n(t).includes("ll-"), o = this.#n = new e({
			lowLatencyMode: a,
			backBufferLength: a ? 4 : r ? 8 : void 0,
			renderTextTracksNatively: !1,
			...this.config
		}), c = (e) => (t, n) => {
			this.#n === o && e(t, n);
		}, l = c(this.#c.bind(this));
		for (let t of Object.values(e.Events)) o.on(t, l);
		o.on(e.Events.ERROR, c(this.#h.bind(this)));
		for (let e of this.#i) e(o);
		this.#t.player.dispatch("hls-instance", { detail: o }), o.attachMedia(this.#e), o.on(e.Events.AUDIO_TRACK_SWITCHED, c(this.#d.bind(this))), o.on(e.Events.LEVEL_SWITCHED, c(this.#f.bind(this))), o.on(e.Events.LEVEL_LOADED, c(this.#m.bind(this, o))), o.on(e.Events.LEVEL_UPDATED, c(this.#p.bind(this))), o.on(e.Events.NON_NATIVE_TEXT_TRACKS_FOUND, c(this.#l.bind(this, o))), o.on(e.Events.CUES_PARSED, c(this.#u.bind(this, o))), this.#t.qualities[g.enableAuto] = this.#_.bind(this), i(this.#t.qualities, "change", this.#v.bind(this)), i(this.#t.audioTracks, "change", this.#y.bind(this)), this.#r = s(this.#o.bind(this));
	}
	#a(e, t) {
		return new o(y(e), { detail: t });
	}
	#o() {
		if (!this.#t.$state.live()) return;
		let e = new f(this.#s.bind(this));
		return e.start(), e.stop.bind(e);
	}
	#s() {
		this.#t.$state.liveSyncPosition.set(this.#n?.liveSyncPosition ?? Infinity);
	}
	#c(e, t) {
		this.#t.player?.dispatch(this.#a(e, t));
	}
	#l(e, t, n) {
		let r = this.#a(t, n), i = -1;
		for (let t = 0; t < n.tracks.length; t++) {
			let a = n.tracks[t], o = a.subtitleTrack ?? a.closedCaptions, s = new h({
				id: `hls-${a.kind}-${t}`,
				src: o?.url,
				label: a.label,
				language: o?.lang,
				kind: a.kind,
				default: a.default
			});
			s[m.readyState] = 2, s[m.onModeChange] = () => {
				this.#n === e && (s.mode === "showing" ? (e.subtitleTrack = t, i = t) : i === t && (e.subtitleTrack = -1, i = -1));
			}, this.#t.textTracks.add(s, r);
		}
	}
	#u(e, t, n) {
		let r = e.subtitleTrack, i = this.#t.textTracks.getById(`hls-${n.type}-${r}`);
		if (!i) return;
		let a = this.#a(t, n);
		for (let e of n.cues) e.positionAlign = "auto", i.addCue(e, a);
	}
	#d(e, t) {
		let n = this.#t.audioTracks[t.id];
		if (n) {
			let r = this.#a(e, t);
			this.#t.audioTracks[p.select](n, !0, r);
		}
	}
	#f(e, t) {
		let n = this.#t.qualities[t.level];
		if (n) {
			let r = this.#a(e, t);
			this.#t.qualities[p.select](n, !0, r);
		}
	}
	#p(e, t) {
		t.details.totalduration > 0 && this.#t.$state.inferredLiveDVRWindow.set(t.details.totalduration);
	}
	#m(e, t, n) {
		if (this.#t.$state.canPlay()) return;
		let r = e.media;
		if (!r) return;
		let { type: i, live: a, totalduration: s, targetduration: c } = n.details, l = this.#a(t, n);
		this.#t.notify("stream-type-change", a ? i === "EVENT" && Number.isFinite(s) && c >= 10 ? "live:dvr" : "live" : "on-demand", l), this.#t.notify("duration-change", s, l), e.currentLevel === -1 && this.#t.qualities[g.setAuto](!0, l);
		for (let t of e.audioTracks) {
			let e = {
				id: t.id.toString(),
				label: t.name,
				language: t.lang || "",
				kind: "main"
			};
			this.#t.audioTracks[p.add](e, l);
		}
		for (let t of e.levels) {
			let e = {
				id: t.id?.toString() ?? t.height + "p",
				width: t.width,
				height: t.height,
				codec: t.codecSet,
				bitrate: t.bitrate
			};
			this.#t.qualities[p.add](e, l);
		}
		r.dispatchEvent(new o("canplay", { trigger: l }));
	}
	#h(e, t) {
		if (t.fatal) switch (t.type) {
			case "mediaError":
				this.#n?.recoverMediaError();
				break;
			default:
				this.#g(t.error);
				break;
		}
	}
	#g(e) {
		this.#t.notify("error", {
			message: e.message,
			code: 1,
			error: e
		});
	}
	#_() {
		this.#n && (this.#n.currentLevel = -1);
	}
	#v() {
		let { qualities: e } = this.#t;
		!this.#n || e.auto || (this.#n[e.switch + "Level"] = e.selectedIndex, c && (this.#e.currentTime = this.#e.currentTime));
	}
	#y() {
		let { audioTracks: e } = this.#t;
		this.#n && this.#n.audioTrack !== e.selectedIndex && (this.#n.audioTrack = e.selectedIndex);
	}
	onInstance(e) {
		return this.#i.add(e), () => this.#i.delete(e);
	}
	loadSource(e) {
		t(e.src) && this.#n?.loadSource(e.src);
	}
	destroy() {
		this.#n?.destroy(), this.#n = null, this.#r?.(), this.#r = null;
	}
}, x = 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 C(this.#e, e);
		return r(n) && !t(this.#e) && (n = await S(this.#e, e)), n ? n.isSupported() ? n : (this.#t.player.dispatch(new o("hls-unsupported")), this.#t.notify("error", {
			message: "[vidstack] `hls.js` is not supported in this environment",
			code: 4
		}), null) : null;
	}
	#i() {
		this.#t.player.dispatch(new o("hls-lib-load-start"));
	}
	#a(e) {
		this.#t.player.dispatch(new o("hls-lib-loaded", { detail: e })), this.#n(e);
	}
	#o(e) {
		let t = v(e);
		this.#t.player.dispatch(new o("hls-lib-load-error", { detail: t })), this.#t.notify("error", {
			message: t.message,
			code: 4,
			error: t
		});
	}
};
async function S(e, t = {}) {
	if (!r(e)) {
		if (t.onLoadStart?.(), e.prototype && e.prototype !== Function) return t.onLoaded?.(e), e;
		try {
			let n = (await e())?.default;
			if (n && n.isSupported) t.onLoaded?.(n);
			else throw Error("");
			return n;
		} catch (e) {
			t.onLoadError?.(e);
		}
	}
}
async function C(n, r = {}) {
	if (t(n)) {
		r.onLoadStart?.();
		try {
			if (await d(n), !e(window.Hls)) throw Error("");
			let t = window.Hls;
			return r.onLoaded?.(t), t;
		} catch (e) {
			r.onLoadError?.(e);
		}
	}
}
var w = "https://cdn.jsdelivr.net", T = class extends _ {
	$$PROVIDER_TYPE = "HLS";
	#e = null;
	#t = new b(this.video, this.ctx);
	get ctor() {
		return this.#e;
	}
	get instance() {
		return this.#t.instance;
	}
	static supported = l();
	get type() {
		return "hls";
	}
	get canLiveSync() {
		return !0;
	}
	#n = `${w}/npm/hls.js@^1.5.0/dist/hls.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) && u(this.#n);
	}
	setup() {
		super.setup(), new x(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 { T as HLSProvider };
