import { F as e, M as t, N as n, Q as r, g as i, h as a, v as o, w as s } from "./vidstack-D9mvYVIz-DesuYgKV.js";
import { i as c } from "./vidstack-mvRkf9tF-CoFf7dS9.js";
import { resolveYouTubeVideoId as l } from "./vidstack-IMa-zkAV-DevzBPj3.js";
import { t as u } from "./vidstack-CdfyZegh-Bn47J4h6.js";
import { t as d } from "./vidstack-CceZzgm4-C37rtwSQ.js";
//#region node_modules/vidstack/prod/providers/vidstack-youtube.js
var f = {
	Unstarted: -1,
	Ended: 0,
	Playing: 1,
	Paused: 2,
	Buffering: 3,
	Cued: 5
}, p = class extends d {
	$$PROVIDER_TYPE = "YOUTUBE";
	scope = a();
	#e;
	#t = r("");
	#n = -1;
	#r = null;
	#i = -1;
	#a = !1;
	#o = /* @__PURE__ */ new Map();
	constructor(e, t) {
		super(e), this.#e = t;
	}
	language = "en";
	color = "red";
	cookies = !1;
	get currentSrc() {
		return this.#r;
	}
	get type() {
		return "youtube";
	}
	get videoId() {
		return this.#t();
	}
	preconnect() {
		c(this.getOrigin());
	}
	setup() {
		super.setup(), o(this.#s.bind(this)), this.#e.notify("provider-setup", this);
	}
	destroy() {
		this.#g();
		for (let e of this.#o.values()) for (let { reject: t } of e) t("provider destroyed");
		this.#o.clear();
	}
	async play() {
		return this.#c("playVideo");
	}
	async pause() {
		return this.#c("pauseVideo");
	}
	setMuted(e) {
		e ? this.#c("mute") : this.#c("unMute");
	}
	setCurrentTime(e) {
		this.#c("seekTo", [e, !0]), this.#e.notify("seeking", e);
	}
	setVolume(e) {
		this.#c("setVolume", e * 100);
	}
	setPlaybackRate(e) {
		this.#c("setPlaybackRate", e);
	}
	async loadSource(t) {
		if (!e(t.src)) {
			this.#r = null, this.#t.set("");
			return;
		}
		let n = l(t.src);
		this.#t.set(n ?? ""), this.#r = t;
	}
	getOrigin() {
		return this.cookies ? "https://www.youtube.com" : "https://www.youtube-nocookie.com";
	}
	#s() {
		this.#g();
		let e = this.#t();
		if (!e) {
			this.src.set("");
			return;
		}
		this.src.set(`${this.getOrigin()}/embed/${e}`), this.#e.notify("load-start");
	}
	buildParams() {
		let { keyDisabled: e } = this.#e.$props, { muted: t, playsInline: n, nativeControls: r } = this.#e.$state, i = r();
		return {
			rel: 0,
			autoplay: 0,
			cc_lang_pref: this.language,
			cc_load_policy: i ? 1 : void 0,
			color: this.color,
			controls: +!!i,
			disablekb: !i || e() ? 1 : 0,
			enablejsapi: 1,
			fs: 1,
			hl: this.language,
			iv_load_policy: i ? 1 : 3,
			mute: +!!t(),
			playsinline: +!!n()
		};
	}
	#c(e, t) {
		let n = i(), r = this.#o.get(e);
		r || this.#o.set(e, r = []), r.push(n);
		let a = Array.isArray(t) ? t : t ? [t] : void 0;
		return this.postMessage({
			event: "command",
			func: e,
			args: a
		}), n.promise;
	}
	onLoad() {
		window.setTimeout(() => this.postMessage({ event: "listening" }), 100);
	}
	#l(e) {
		this.#e.notify("loaded-metadata"), this.#e.notify("loaded-data"), this.#e.delegate.ready(void 0, e);
	}
	#u(e) {
		this.#_("pauseVideo")?.resolve(), this.#e.notify("pause", void 0, e);
	}
	#d(e, t) {
		let { duration: n, realCurrentTime: r } = this.#e.$state, i = this.#n === f.Ended, a = i ? n() : e;
		this.#e.notify("time-change", a, t), !i && Math.abs(a - r()) > 1 && this.#e.notify("seeking", a, t);
	}
	#f(e, t, n) {
		let r = {
			buffered: new u(0, e),
			seekable: t
		};
		this.#e.notify("progress", r, n);
		let { seeking: i, realCurrentTime: a } = this.#e.$state;
		i() && e > a() && this.#p(n);
	}
	#p(e) {
		let { paused: t, realCurrentTime: n } = this.#e.$state;
		window.clearTimeout(this.#i), this.#i = window.setTimeout(() => {
			this.#e.notify("seeked", n(), e), this.#i = -1;
		}, t() ? 100 : 0);
	}
	#m(e) {
		let { seeking: t } = this.#e.$state;
		t() && this.#p(e), this.#e.notify("pause", void 0, e), this.#e.notify("end", void 0, e);
	}
	#h(e, t) {
		let { paused: n, seeking: r } = this.#e.$state, i = e === f.Playing, a = e === f.Buffering, o = this.#v("playVideo"), s = n() && (a || i);
		if (a && this.#e.notify("waiting", void 0, t), r() && i && this.#p(t), this.#a && i) {
			this.pause(), this.#a = !1, this.setMuted(this.#e.$state.muted());
			return;
		}
		if (!o && s) {
			this.#a = !0, this.setMuted(!0);
			return;
		}
		switch (s && (this.#_("playVideo")?.resolve(), this.#e.notify("play", void 0, t)), e) {
			case f.Cued:
				this.#l(t);
				break;
			case f.Playing:
				this.#e.notify("playing", void 0, t);
				break;
			case f.Paused:
				this.#u(t);
				break;
			case f.Ended:
				this.#m(t);
				break;
		}
		this.#n = e;
	}
	onMessage({ info: e }, r) {
		if (!e) return;
		let { title: i, intrinsicDuration: a, playbackRate: o } = this.#e.$state;
		if (n(e.videoData) && e.videoData.title !== i() && this.#e.notify("title-change", e.videoData.title, r), t(e.duration) && e.duration !== a()) {
			if (t(e.videoLoadedFraction)) {
				let t = e.progressState?.loaded ?? e.videoLoadedFraction * e.duration, n = new u(0, e.duration);
				this.#f(t, n, r);
			}
			this.#e.notify("duration-change", e.duration, r);
		}
		if (t(e.playbackRate) && e.playbackRate !== o() && this.#e.notify("rate-change", e.playbackRate, r), e.progressState) {
			let { current: t, seekableStart: n, seekableEnd: i, loaded: o, duration: s } = e.progressState;
			this.#d(t, r), this.#f(o, new u(n, i), r), s !== a() && this.#e.notify("duration-change", s, r);
		}
		if (t(e.volume) && s(e.muted) && !this.#a) {
			let t = {
				muted: e.muted,
				volume: e.volume / 100
			};
			this.#e.notify("volume-change", t, r);
		}
		t(e.playerState) && e.playerState !== this.#n && this.#h(e.playerState, r);
	}
	#g() {
		this.#n = -1, this.#i = -1, this.#a = !1;
	}
	#_(e) {
		return this.#o.get(e)?.shift();
	}
	#v(e) {
		return !!this.#o.get(e)?.length;
	}
};
//#endregion
export { p as YouTubeProvider };
