import { C as e, F as t, M as n, V as r, Y as i, a, r as o, x as s } from "./vidstack-D9mvYVIz-DesuYgKV.js";
import { n as c } from "./vidstack-mvRkf9tF-CoFf7dS9.js";
//#region node_modules/vidstack/prod/chunks/vidstack-B1Z6f96t.js
function l(e, t) {
	return t >= e.startTime && t < e.endTime;
}
function u(e, n, a) {
	let o = null, c = s();
	function l() {
		let s = t(n) ? [n] : n, l = e.toArray().find((e) => s.includes(e.kind) && e.mode === "showing");
		if (l !== o) {
			if (!l) {
				a(null), o = null;
				return;
			}
			l.readyState == 2 ? a(l) : (a(null), i(() => {
				let e = r(l, "load", () => {
					a(l), e();
				}, { once: !0 });
			}, c)), o = l;
		}
	}
	return l(), r(e, "mode-change", l);
}
function d(e, t, n) {
	u(e, t, (e) => {
		if (!e) {
			n("");
			return;
		}
		let t = () => {
			let t = e?.activeCues[0];
			n(t?.text || "");
		};
		t(), r(e, "cue-change", t);
	});
}
//#endregion
//#region node_modules/vidstack/prod/chunks/vidstack-DYPyTdYw.js
var f = {
	crossOrigin: Symbol(0),
	readyState: Symbol(0),
	updateActiveCues: Symbol(0),
	canLoad: Symbol(0),
	onModeChange: Symbol(0),
	native: Symbol(0),
	nativeHLS: Symbol(0)
}, p = class extends a {
	static createId(e) {
		return `vds-${e.type}-${e.kind}-${e.src ?? e.label ?? "?"}`;
	}
	src;
	content;
	type;
	encoding;
	id = "";
	label = "";
	language = "";
	kind;
	default = !1;
	#e = !1;
	#t = 0;
	#n = "disabled";
	#r = {};
	#i = [];
	#a = [];
	#o = [];
	#s = /* @__PURE__ */ new Set();
	[f.readyState] = 0;
	[f.crossOrigin];
	[f.onModeChange] = null;
	[f.native] = null;
	get metadata() {
		return this.#r;
	}
	get regions() {
		return this.#i;
	}
	get cues() {
		return this.#a;
	}
	get activeCues() {
		return this.#o;
	}
	get readyState() {
		return this[f.readyState];
	}
	get mode() {
		return this.#n;
	}
	set mode(e) {
		this.setMode(e);
	}
	constructor(e) {
		super();
		for (let t of Object.keys(e)) this[t] = e[t];
		this.type ||= "vtt", e.content ? this.#c(e) : e.src || (this[f.readyState] = 2);
	}
	addCue(e, t) {
		if (e.id) {
			if (this.#s.has(e.id)) return;
			this.#s.add(e.id);
		}
		let n = 0, r = this.#a.length;
		for (n = 0; n < r && !(e.endTime <= this.#a[n].startTime); n++);
		n === r ? this.#a.push(e) : this.#a.splice(n, 0, e), e instanceof TextTrackCue || this[f.native]?.track.addCue(e), this.dispatchEvent(new o("add-cue", {
			detail: e,
			trigger: t
		})), l(e, this.#t) && this[f.updateActiveCues](this.#t, t);
	}
	removeCue(e, t) {
		let n = this.#a.indexOf(e);
		if (n >= 0) {
			let r = this.#o.includes(e);
			e.id && this.#s.delete(e.id), this.#a.splice(n, 1), this[f.native]?.track.removeCue(e), this.dispatchEvent(new o("remove-cue", {
				detail: e,
				trigger: t
			})), r && this[f.updateActiveCues](this.#t, t);
		}
	}
	setMode(e, t) {
		this.#n !== e && (this.#n = e, e === "disabled" ? (this.#o = [], this.#p()) : this.readyState === 2 ? this[f.updateActiveCues](this.#t, t) : this.#l(), this.dispatchEvent(new o("mode-change", {
			detail: this,
			trigger: t
		})), this[f.onModeChange]?.());
	}
	[f.updateActiveCues](e, t) {
		if (this.#t = e, this.mode === "disabled" || !this.#a.length) return;
		let n = [];
		for (let t = 0, r = this.#a.length; t < r; t++) {
			let r = this.#a[t];
			l(r, e) && n.push(r);
		}
		let r = n.length !== this.#o.length;
		if (!r) {
			for (let e = 0; e < n.length; e++) if (!this.#o.includes(n[e])) {
				r = !0;
				break;
			}
		}
		this.#o = n, r && this.#p(t);
	}
	[f.canLoad]() {
		this.#e = !0, this.#n !== "disabled" && this.#l();
	}
	#c(e) {
		import("./prod-DSuxcp5t.js").then(({ parseText: n, VTTCue: r, VTTRegion: i }) => {
			!t(e.content) || e.type === "json" ? (this.#f(e.content, r, i), this.readyState !== 3 && this.#u()) : n(e.content.split("\n").map((e) => e.trim()).join("\n").trim(), { type: e.type }).then(({ cues: e, regions: t }) => {
				this.#a = e, this.#i = t, this.#u();
			});
		});
	}
	async #l() {
		if (!(!this.#e || this[f.readyState] > 0)) {
			if (this[f.readyState] = 1, this.dispatchEvent(new o("load-start")), !this.src) {
				this.#u();
				return;
			}
			try {
				let { parseResponse: e, VTTCue: t, VTTRegion: n } = await import("./prod-DSuxcp5t.js"), r = this[f.crossOrigin]?.(), i = fetch(this.src, {
					headers: this.type === "json" ? { "Content-Type": "application/json" } : void 0,
					credentials: c(r)
				});
				if (this.type === "json") this.#f(await (await i).text(), t, n);
				else {
					let { errors: t, metadata: n, regions: r, cues: a } = await e(i, {
						type: this.type,
						encoding: this.encoding
					});
					if (t[0]?.code === 0) throw t[0];
					this.#r = n, this.#i = r, this.#a = a;
				}
				this.#u();
			} catch (e) {
				this.#d(e);
			}
		}
	}
	#u() {
		if (this[f.readyState] = 2, !this.src || this.type !== "vtt") {
			let e = this[f.native];
			if (e && !e.managed) for (let t of this.#a) e.track.addCue(t);
		}
		let e = new o("load");
		this[f.updateActiveCues](this.#t, e), this.dispatchEvent(e);
	}
	#d(e) {
		this[f.readyState] = 3, this.dispatchEvent(new o("error", { detail: e }));
	}
	#f(e, t, n) {
		try {
			let { regions: r, cues: i } = g(e, t, n);
			this.#i = r, this.#a = i;
		} catch (e) {
			this.#d(e);
		}
	}
	#p(e) {
		this.dispatchEvent(new o("cue-change", { trigger: e }));
	}
}, m = /captions|subtitles/;
function h(e) {
	return m.test(e.kind);
}
function g(r, i, a) {
	let o = t(r) ? JSON.parse(r) : r, s = [], c = [];
	return o.regions && a && (s = o.regions.map((e) => Object.assign(new a(), e))), (o.cues || e(o)) && (c = (e(o) ? o : o.cues).filter((e) => n(e.startTime) && n(e.endTime)).map((e) => Object.assign(new i(0, 0, ""), e))), {
		regions: s,
		cues: c
	};
}
//#endregion
export { u as a, l as i, f as n, d as o, h as r, p as t };
