import { B as e, G as t, V as n, W as r, et as i, h as a, r as o, v as s } from "./vidstack-D9mvYVIz-DesuYgKV.js";
import { a as c, d as l, i as u, n as d, s as f, t as p } from "./vidstack-DATGlkSz2-BnlUxvSa.js";
import { n as m, t as h } from "./vidstack-ijoHbXQB-DBClSWHE.js";
import { t as g } from "./vidstack-CdfyZegh-Bn47J4h6.js";
//#region node_modules/vidstack/prod/providers/vidstack-google-cast.js
var _ = class {
	#e;
	constructor(e) {
		this.#e = new chrome.cast.media.MediaInfo(e.src, e.type);
	}
	build() {
		return this.#e;
	}
	setStreamType(e) {
		return e.includes("live") ? this.#e.streamType = chrome.cast.media.StreamType.LIVE : this.#e.streamType = chrome.cast.media.StreamType.BUFFERED, this;
	}
	setTracks(e) {
		return this.#e.tracks = e.map(this.#t), this;
	}
	setMetadata(e, t) {
		return this.#e.metadata = new chrome.cast.media.GenericMediaMetadata(), this.#e.metadata.title = e, this.#e.metadata.images = [{ url: t }], this;
	}
	#t(e, t) {
		let n = new chrome.cast.media.Track(t, chrome.cast.media.TrackType.TEXT);
		return n.name = e.label, n.trackContentId = e.src, n.trackContentType = "text/vtt", n.language = e.language, n.subtype = e.kind.toUpperCase(), n;
	}
}, v = class {
	#e;
	#t;
	#n;
	constructor(e, t, n) {
		this.#e = e, this.#t = t, this.#n = n;
	}
	setup() {
		let e = this.syncRemoteActiveIds.bind(this);
		n(this.#t.audioTracks, "change", e), n(this.#t.textTracks, "mode-change", e), s(this.#o.bind(this));
	}
	getLocalTextTracks() {
		return this.#t.$state.textTracks().filter((e) => e.src && e.type === "vtt");
	}
	#r() {
		return this.#t.$state.audioTracks();
	}
	#i(e) {
		let t = this.#e.mediaInfo?.tracks ?? [];
		return e ? t.filter((t) => t.type === e) : t;
	}
	#a() {
		let e = [], t = this.#r().find((e) => e.selected), n = this.getLocalTextTracks().filter((e) => e.mode === "showing");
		if (t) {
			let n = this.#i(chrome.cast.media.TrackType.AUDIO), r = this.#l(n, t);
			r && e.push(r.trackId);
		}
		if (n?.length) {
			let t = this.#i(chrome.cast.media.TrackType.TEXT);
			if (t.length) for (let r of n) {
				let n = this.#l(t, r);
				n && e.push(n.trackId);
			}
		}
		return e;
	}
	#o() {
		let e = this.getLocalTextTracks();
		if (!this.#e.isMediaLoaded) return;
		let t = this.#i(chrome.cast.media.TrackType.TEXT);
		for (let n of e) if (!this.#l(t, n)) {
			i(() => this.#n?.());
			break;
		}
	}
	syncRemoteTracks(e) {
		if (!this.#e.isMediaLoaded) return;
		let t = this.#r(), n = this.getLocalTextTracks(), r = this.#i(chrome.cast.media.TrackType.AUDIO), i = this.#i(chrome.cast.media.TrackType.TEXT);
		for (let n of r) {
			if (this.#c(t, n)) continue;
			let r = {
				id: n.trackId.toString(),
				label: n.name,
				language: n.language,
				kind: n.subtype ?? "main",
				selected: !1
			};
			this.#t.audioTracks[h.add](r, e);
		}
		for (let t of i) {
			if (this.#c(n, t)) continue;
			let r = {
				id: t.trackId.toString(),
				src: t.trackContentId,
				label: t.name,
				language: t.language,
				kind: t.subtype.toLowerCase()
			};
			this.#t.textTracks.add(r, e);
		}
	}
	syncRemoteActiveIds(e) {
		if (!this.#e.isMediaLoaded) return;
		let t = this.#a(), n = new chrome.cast.media.EditTracksInfoRequest(t);
		this.#s(n).catch((e) => {});
	}
	#s(e) {
		let t = c();
		return new Promise((n, r) => t?.editTracksInfo(e, n, r));
	}
	#c(e, t) {
		return e.find((e) => this.#u(e, t));
	}
	#l(e, t) {
		return e.find((e) => this.#u(t, e));
	}
	#u(e, t) {
		return t.name === e.label && t.language === e.language && t.subtype.toLowerCase() === e.kind.toLowerCase();
	}
}, y = class {
	$$PROVIDER_TYPE = "GOOGLE_CAST";
	scope = a();
	#e;
	#t;
	#n;
	#r = null;
	#i = "disconnected";
	#a = 0;
	#o = 0;
	#s = new g(0, 0);
	#c = new m(this.#v.bind(this));
	#l;
	#u = null;
	#d = !1;
	constructor(e, t) {
		this.#e = e, this.#t = t, this.#n = new v(e, t, this.#I.bind(this));
	}
	get type() {
		return "google-cast";
	}
	get currentSrc() {
		return this.#r;
	}
	get player() {
		return this.#e;
	}
	get cast() {
		return p();
	}
	get session() {
		return u();
	}
	get media() {
		return c();
	}
	get hasActiveSession() {
		return f(this.#r);
	}
	setup() {
		this.#f(), this.#p(), this.#n.setup(), this.#t.notify("provider-setup", this);
	}
	#f() {
		l(cast.framework.CastContextEventType.CAST_STATE_CHANGED, this.#b.bind(this));
	}
	#p() {
		let t = cast.framework.RemotePlayerEventType, n = {
			[t.IS_CONNECTED_CHANGED]: this.#b,
			[t.IS_MEDIA_LOADED_CHANGED]: this.#x,
			[t.CAN_CONTROL_VOLUME_CHANGED]: this.#S,
			[t.CAN_SEEK_CHANGED]: this.#C,
			[t.DURATION_CHANGED]: this.#E,
			[t.IS_MUTED_CHANGED]: this.#D,
			[t.VOLUME_LEVEL_CHANGED]: this.#D,
			[t.IS_PAUSED_CHANGED]: this.#O,
			[t.LIVE_SEEKABLE_RANGE_CHANGED]: this.#k,
			[t.PLAYER_STATE_CHANGED]: this.#A
		};
		this.#l = n;
		let i = this.#y.bind(this);
		for (let t of e(n)) this.#e.controller.addEventListener(t, i);
		r(() => {
			for (let t of e(n)) this.#e.controller.removeEventListener(t, i);
		});
	}
	async play() {
		if (!(!this.#e.isPaused && !this.#d)) {
			if (this.#d) {
				await this.#F(!1, 0);
				return;
			}
			this.#e.controller?.playOrPause();
		}
	}
	async pause() {
		this.#e.isPaused || this.#e.controller?.playOrPause();
	}
	getMediaStatus(e) {
		return new Promise((t, n) => {
			this.media?.getStatus(e, t, n);
		});
	}
	setMuted(e) {
		(e && !this.#e.isMuted || !e && this.#e.isMuted) && this.#e.controller?.muteOrUnmute();
	}
	setCurrentTime(e) {
		this.#e.currentTime = e, this.#t.notify("seeking", e), this.#e.controller?.seek();
	}
	setVolume(e) {
		this.#e.volumeLevel = e, this.#e.controller?.setVolumeLevel();
	}
	async loadSource(e) {
		if (this.#u?.src !== e && (this.#u = null), f(e)) {
			this.#h(), this.#r = e;
			return;
		}
		this.#t.notify("load-start");
		let t = this.#P(e), n = await this.session.loadMedia(t);
		if (n) {
			this.#r = null, this.#t.notify("error", Error(d(n)));
			return;
		}
		this.#r = e;
	}
	destroy() {
		this.#m(), this.#g();
	}
	#m() {
		this.#u || (this.#o = 0, this.#s = new g(0, 0)), this.#c.stop(), this.#a = 0, this.#u = null;
	}
	#h() {
		let e = new o("resume-session", { detail: this.session });
		this.#x(e);
		let { muted: t, volume: n, savedState: r } = this.#t.$state, i = r();
		this.setCurrentTime(Math.max(this.#e.currentTime, i?.currentTime ?? 0)), this.setMuted(t()), this.setVolume(n()), i?.paused === !1 && this.play();
	}
	#g() {
		this.cast.endCurrentSession(!0);
		let { remotePlaybackLoader: e } = this.#t.$state;
		e.set(null);
	}
	#_() {
		let { savedState: e } = this.#t.$state;
		e.set({
			paused: this.#e.isPaused,
			currentTime: this.#e.currentTime
		}), this.#g();
	}
	#v() {
		this.#T();
	}
	#y(e) {
		this.#l[e.type].call(this, e);
	}
	#b(e) {
		let t = this.cast.getCastState(), n = t === cast.framework.CastState.CONNECTED ? "connected" : t === cast.framework.CastState.CONNECTING ? "connecting" : "disconnected";
		if (this.#i === n) return;
		let r = {
			type: "google-cast",
			state: n
		}, i = this.#M(e);
		this.#i = n, this.#t.notify("remote-playback-change", r, i), n === "disconnected" && this.#_();
	}
	#x(e) {
		if (!this.#e.isMediaLoaded) return;
		let n = t(this.#t.$state.source);
		Promise.resolve().then(() => {
			if (n !== t(this.#t.$state.source) || !this.#e.isMediaLoaded) return;
			this.#m();
			let r = this.#e.duration;
			this.#s = new g(0, r);
			let i = {
				provider: this,
				duration: r,
				buffered: new g(0, 0),
				seekable: this.#j()
			}, a = this.#M(e);
			this.#t.notify("loaded-metadata", void 0, a), this.#t.notify("loaded-data", void 0, a), this.#t.notify("can-play", i, a), this.#S(), this.#C(e);
			let { volume: o, muted: s } = this.#t.$state;
			this.setVolume(o()), this.setMuted(s()), this.#c.start(), this.#n.syncRemoteTracks(a), this.#n.syncRemoteActiveIds(a);
		});
	}
	#S() {
		this.#t.$state.canSetVolume.set(this.#e.canControlVolume);
	}
	#C(e) {
		let t = this.#M(e);
		this.#t.notify("stream-type-change", this.#w(), t);
	}
	#w() {
		return this.#e.mediaInfo?.streamType === chrome.cast.media.StreamType.LIVE ? this.#e.canSeek ? "live:dvr" : "live" : "on-demand";
	}
	#T() {
		if (this.#u) return;
		let e = this.#e.currentTime;
		e !== this.#a && (this.#t.notify("time-change", e), e > this.#o && (this.#o = e, this.#k()), this.#t.$state.seeking() && this.#t.notify("seeked", e), this.#a = e);
	}
	#E(e) {
		if (!this.#e.isMediaLoaded || this.#u) return;
		let t = this.#e.duration, n = this.#M(e);
		this.#s = new g(0, t), this.#t.notify("duration-change", t, n);
	}
	#D(e) {
		if (!this.#e.isMediaLoaded) return;
		let t = {
			muted: this.#e.isMuted,
			volume: this.#e.volumeLevel
		}, n = this.#M(e);
		this.#t.notify("volume-change", t, n);
	}
	#O(e) {
		let t = this.#M(e);
		this.#e.isPaused ? this.#t.notify("pause", void 0, t) : this.#t.notify("play", void 0, t);
	}
	#k(e) {
		let t = {
			seekable: this.#j(),
			buffered: new g(0, this.#o)
		}, n = e ? this.#M(e) : void 0;
		this.#t.notify("progress", t, n);
	}
	#A(e) {
		let t = this.#e.playerState, n = chrome.cast.media.PlayerState;
		if (this.#d = t === n.IDLE, t === n.PAUSED) return;
		let r = this.#M(e);
		switch (t) {
			case n.PLAYING:
				this.#t.notify("playing", void 0, r);
				break;
			case n.BUFFERING:
				this.#t.notify("waiting", void 0, r);
				break;
			case n.IDLE:
				this.#c.stop(), this.#t.notify("pause"), this.#t.notify("end");
				break;
		}
	}
	#j() {
		return this.#e.liveSeekableRange ? new g(this.#e.liveSeekableRange.start, this.#e.liveSeekableRange.end) : this.#s;
	}
	#M(e) {
		return e instanceof Event ? e : new o(e.type, { detail: e });
	}
	#N(e) {
		let { streamType: t, title: n, poster: r } = this.#t.$state;
		return new _(e).setMetadata(n(), r()).setStreamType(t()).setTracks(this.#n.getLocalTextTracks()).build();
	}
	#P(e) {
		let t = this.#N(e), n = new chrome.cast.media.LoadRequest(t), r = this.#t.$state.savedState();
		return n.autoplay = (this.#u?.paused ?? r?.paused) === !1, n.currentTime = this.#u?.time ?? r?.currentTime ?? 0, n;
	}
	async #F(e, n) {
		let r = t(this.#t.$state.source);
		this.#u = {
			src: r,
			paused: e,
			time: n
		}, await this.loadSource(r);
	}
	#I() {
		this.#F(this.#e.isPaused, this.#e.currentTime).catch((e) => {});
	}
};
//#endregion
export { y as GoogleCastProvider };
