UNPKG

@react-native-youtube-bridge/web

Version:
1,008 lines (1,005 loc) 33.1 kB
var Me = Object.defineProperty; var We = (a, r, t) => r in a ? Me(a, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[r] = t; var F = (a, r, t) => We(a, typeof r != "symbol" ? r + "" : r, t); import Ue, { useState as Q, useEffect as G, useMemo as Te, useCallback as Re, useRef as ke } from "react"; var Z = { exports: {} }, $ = {}; /** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var _e; function De() { if (_e) return $; _e = 1; var a = Symbol.for("react.transitional.element"), r = Symbol.for("react.fragment"); function t(d, i, f) { var y = null; if (f !== void 0 && (y = "" + f), i.key !== void 0 && (y = "" + i.key), "key" in i) { f = {}; for (var p in i) p !== "key" && (f[p] = i[p]); } else f = i; return i = f.ref, { $$typeof: a, type: d, key: y, ref: i !== void 0 ? i : null, props: f }; } return $.Fragment = r, $.jsx = t, $.jsxs = t, $; } var H = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Pe; function Le() { return Pe || (Pe = 1, process.env.NODE_ENV !== "production" && function() { function a(e) { if (e == null) return null; if (typeof e == "function") return e.$$typeof === xe ? null : e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case C: return "Fragment"; case Y: return "Portal"; case V: return "Profiler"; case h: return "StrictMode"; case ee: return "Suspense"; case re: return "SuspenseList"; } if (typeof e == "object") switch (typeof e.tag == "number" && console.error( "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue." ), e.$$typeof) { case L: return (e.displayName || "Context") + ".Provider"; case j: return (e._context.displayName || "Context") + ".Consumer"; case _: var o = e.render; return e = e.displayName, e || (e = o.displayName || o.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case te: return o = e.displayName || null, o !== null ? o : a(e.type) || "Memo"; case ae: o = e._payload, e = e._init; try { return a(e(o)); } catch { } } return null; } function r(e) { return "" + e; } function t(e) { try { r(e); var o = !1; } catch { o = !0; } if (o) { o = console; var n = o.error, u = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return n.call( o, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", u ), r(e); } } function d() { } function i() { if (J === 0) { ce = console.log, de = console.info, fe = console.warn, ye = console.error, ge = console.group, pe = console.groupCollapsed, be = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: d, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } J++; } function f() { if (J--, J === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: W({}, e, { value: ce }), info: W({}, e, { value: de }), warn: W({}, e, { value: fe }), error: W({}, e, { value: ye }), group: W({}, e, { value: ge }), groupCollapsed: W({}, e, { value: pe }), groupEnd: W({}, e, { value: be }) }); } 0 > J && console.error( "disabledDepth fell below zero. This is a bug in React. Please file an issue." ); } function y(e) { if (ne === void 0) try { throw Error(); } catch (n) { var o = n.stack.trim().match(/\n( *(at )?)/); ne = o && o[1] || "", me = -1 < n.stack.indexOf(` at`) ? " (<anonymous>)" : -1 < n.stack.indexOf("@") ? "@unknown:0:0" : ""; } return ` ` + ne + e + me; } function p(e, o) { if (!e || se) return ""; var n = ie.get(e); if (n !== void 0) return n; se = !0, n = Error.prepareStackTrace, Error.prepareStackTrace = void 0; var u = null; u = M.H, M.H = null, i(); try { var w = { DetermineComponentFrameRoot: function() { try { if (o) { var S = function() { throw Error(); }; if (Object.defineProperty(S.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(S, []); } catch (I) { var K = I; } Reflect.construct(e, [], S); } else { try { S.call(); } catch (I) { K = I; } e.call(S.prototype); } } else { try { throw Error(); } catch (I) { K = I; } (S = e()) && typeof S.catch == "function" && S.catch(function() { }); } } catch (I) { if (I && K && typeof I.stack == "string") return [I.stack, K.stack]; } return [null, null]; } }; w.DetermineComponentFrameRoot.displayName = "DetermineComponentFrameRoot"; var g = Object.getOwnPropertyDescriptor( w.DetermineComponentFrameRoot, "name" ); g && g.configurable && Object.defineProperty( w.DetermineComponentFrameRoot, "name", { value: "DetermineComponentFrameRoot" } ); var s = w.DetermineComponentFrameRoot(), A = s[0], B = s[1]; if (A && B) { var T = A.split(` `), U = B.split(` `); for (s = g = 0; g < T.length && !T[g].includes( "DetermineComponentFrameRoot" ); ) g++; for (; s < U.length && !U[s].includes( "DetermineComponentFrameRoot" ); ) s++; if (g === T.length || s === U.length) for (g = T.length - 1, s = U.length - 1; 1 <= g && 0 <= s && T[g] !== U[s]; ) s--; for (; 1 <= g && 0 <= s; g--, s--) if (T[g] !== U[s]) { if (g !== 1 || s !== 1) do if (g--, s--, 0 > s || T[g] !== U[s]) { var q = ` ` + T[g].replace( " at new ", " at " ); return e.displayName && q.includes("<anonymous>") && (q = q.replace("<anonymous>", e.displayName)), typeof e == "function" && ie.set(e, q), q; } while (1 <= g && 0 <= s); break; } } } finally { se = !1, M.H = u, f(), Error.prepareStackTrace = n; } return T = (T = e ? e.displayName || e.name : "") ? y(T) : "", typeof e == "function" && ie.set(e, T), T; } function P(e) { if (e == null) return ""; if (typeof e == "function") { var o = e.prototype; return p( e, !(!o || !o.isReactComponent) ); } if (typeof e == "string") return y(e); switch (e) { case ee: return y("Suspense"); case re: return y("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case _: return e = p(e.render, !1), e; case te: return P(e.type); case ae: o = e._payload, e = e._init; try { return P(e(o)); } catch { } } return ""; } function E() { var e = M.A; return e === null ? null : e.getOwner(); } function N(e) { if (ue.call(e, "key")) { var o = Object.getOwnPropertyDescriptor(e, "key").get; if (o && o.isReactWarning) return !1; } return e.key !== void 0; } function x(e, o) { function n() { ve || (ve = !0, console.error( "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)", o )); } n.isReactWarning = !0, Object.defineProperty(e, "key", { get: n, configurable: !0 }); } function D() { var e = a(this.type); return Ee[e] || (Ee[e] = !0, console.error( "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release." )), e = this.props.ref, e !== void 0 ? e : null; } function k(e, o, n, u, w, g) { return n = g.ref, e = { $$typeof: l, type: e, key: o, props: g, _owner: w }, (n !== void 0 ? n : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: D }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: 0 }), Object.defineProperty(e, "_debugInfo", { configurable: !1, enumerable: !1, writable: !0, value: null }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function O(e, o, n, u, w, g) { if (typeof e == "string" || typeof e == "function" || e === C || e === V || e === h || e === ee || e === re || e === Se || typeof e == "object" && e !== null && (e.$$typeof === ae || e.$$typeof === te || e.$$typeof === L || e.$$typeof === j || e.$$typeof === _ || e.$$typeof === Ye || e.getModuleId !== void 0)) { var s = o.children; if (s !== void 0) if (u) if (oe(s)) { for (u = 0; u < s.length; u++) b(s[u], e); Object.freeze && Object.freeze(s); } else console.error( "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead." ); else b(s, e); } else s = "", (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (s += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."), e === null ? u = "null" : oe(e) ? u = "array" : e !== void 0 && e.$$typeof === l ? (u = "<" + (a(e.type) || "Unknown") + " />", s = " Did you accidentally export a JSX literal instead of a component?") : u = typeof e, console.error( "React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", u, s ); if (ue.call(o, "key")) { s = a(e); var A = Object.keys(o).filter(function(T) { return T !== "key"; }); u = 0 < A.length ? "{key: someKey, " + A.join(": ..., ") + ": ...}" : "{key: someKey}", he[s + u] || (A = 0 < A.length ? "{" + A.join(": ..., ") + ": ...}" : "{}", console.error( `A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, u, s, A, s ), he[s + u] = !0); } if (s = null, n !== void 0 && (t(n), s = "" + n), N(o) && (t(o.key), s = "" + o.key), "key" in o) { n = {}; for (var B in o) B !== "key" && (n[B] = o[B]); } else n = o; return s && x( n, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), k(e, s, g, w, E(), n); } function b(e, o) { if (typeof e == "object" && e && e.$$typeof !== je) { if (oe(e)) for (var n = 0; n < e.length; n++) { var u = e[n]; c(u) && m(u, o); } else if (c(e)) e._store && (e._store.validated = 1); else if (e === null || typeof e != "object" ? n = null : (n = le && e[le] || e["@@iterator"], n = typeof n == "function" ? n : null), typeof n == "function" && n !== e.entries && (n = n.call(e), n !== e)) for (; !(e = n.next()).done; ) c(e.value) && m(e.value, o); } } function c(e) { return typeof e == "object" && e !== null && e.$$typeof === l; } function m(e, o) { if (e._store && !e._store.validated && e.key == null && (e._store.validated = 1, o = R(o), !we[o])) { we[o] = !0; var n = ""; e && e._owner != null && e._owner !== E() && (n = null, typeof e._owner.tag == "number" ? n = a(e._owner.type) : typeof e._owner.name == "string" && (n = e._owner.name), n = " It was passed a child from " + n + "."); var u = M.getCurrentStack; M.getCurrentStack = function() { var w = P(e.type); return u && (w += u() || ""), w; }, console.error( 'Each child in a list should have a unique "key" prop.%s%s See https://react.dev/link/warning-keys for more information.', o, n ), M.getCurrentStack = u; } } function R(e) { var o = "", n = E(); return n && (n = a(n.type)) && (o = ` Check the render method of \`` + n + "`."), o || (e = a(e)) && (o = ` Check the top-level render call using <` + e + ">."), o; } var v = Ue, l = Symbol.for("react.transitional.element"), Y = Symbol.for("react.portal"), C = Symbol.for("react.fragment"), h = Symbol.for("react.strict_mode"), V = Symbol.for("react.profiler"), j = Symbol.for("react.consumer"), L = Symbol.for("react.context"), _ = Symbol.for("react.forward_ref"), ee = Symbol.for("react.suspense"), re = Symbol.for("react.suspense_list"), te = Symbol.for("react.memo"), ae = Symbol.for("react.lazy"), Se = Symbol.for("react.offscreen"), le = Symbol.iterator, xe = Symbol.for("react.client.reference"), M = v.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, ue = Object.prototype.hasOwnProperty, W = Object.assign, Ye = Symbol.for("react.client.reference"), oe = Array.isArray, J = 0, ce, de, fe, ye, ge, pe, be; d.__reactDisabledLog = !0; var ne, me, se = !1, ie = new (typeof WeakMap == "function" ? WeakMap : Map)(), je = Symbol.for("react.client.reference"), ve, Ee = {}, he = {}, we = {}; H.Fragment = C, H.jsx = function(e, o, n, u, w) { return O(e, o, n, !1, u, w); }, H.jsxs = function(e, o, n, u, w) { return O(e, o, n, !0, u, w); }; }()), H; } var Ne; function Be() { return Ne || (Ne = 1, process.env.NODE_ENV === "production" ? Z.exports = De() : Z.exports = Le()), Z.exports; } var Ce = Be(); let z = /* @__PURE__ */ function(a) { return a[a.UNSTARTED = -1] = "UNSTARTED", a[a.ENDED = 0] = "ENDED", a[a.PLAYING = 1] = "PLAYING", a[a.PAUSED = 2] = "PAUSED", a[a.BUFFERING = 3] = "BUFFERING", a[a.CUED = 5] = "CUED", a; }({}); const X = { 2: "INVALID_PARAMETER_VALUE", 5: "HTML5_PLAYER_ERROR", 100: "VIDEO_NOT_FOUND_OR_PRIVATE", 101: "EMBEDDED_PLAYBACK_NOT_ALLOWED", 150: "EMBEDDED_RESTRICTED", 1e3: "FAILED_TO_PARSE_WEBVIEW_MESSAGE", 1001: "WEBVIEW_LOADING_ERROR", 1002: "INVALID_YOUTUBE_VIDEO_ID", 1003: "FAILED_TO_LOAD_YOUTUBE_API", 1004: "UNKNOWN_ERROR" }, Fe = /(?:youtu\.be\/|youtube(?:-nocookie|education)?\.com\/(?:embed\/|v\/|watch\/|watch\?v=|watch\?.+&v=|shorts\/|live\/))((\w|-){11})/, ze = (a) => { if (!a) return; const r = a.match(Fe); return r ? r[1] : void 0; }, Oe = (a) => a ? /^[\w-]{11}$/.test(a) : !1; var Ge = class Ve { constructor() { F(this, "player", null); F(this, "progressInterval", null); F(this, "callbacks", {}); F(this, "progressIntervalMs", 1e3); F(this, "seekTimeout", null); } static createInstance() { return new Ve(); } static async initialize() { var r; return typeof window > "u" || (r = window.YT) != null && r.Player ? Promise.resolve() : (window._ytApiPromise || (window._ytApiPromise = new Promise((t) => { if (document.querySelector('script[src*="youtube.com/iframe_api"]')) { let i = 0; const f = 100, y = () => { var p; if ((p = window.YT) != null && p.Player) { t(); return; } if (i >= f) { console.error("YouTube API failed to load after timeout"), t(); return; } i++, setTimeout(y, 100); }; y(); return; } window.onYouTubeIframeAPIReady = () => { t(); }; const d = document.createElement("script"); d.src = "https://www.youtube.com/iframe_api", d.async = !0, document.head.appendChild(d); })), window._ytApiPromise); } createPlayer(r, t) { var i, f, y, p, P, E, N, x, D, k, O; if (!(typeof window > "u" || !((i = window.YT) != null && i.Player) || !document.getElementById(r))) { if (!Oe(t.videoId)) { (y = (f = this.callbacks).onError) == null || y.call(f, { code: 1002, message: X[1002] }); return; } if (this.player) try { this.player.destroy(); } catch (b) { console.warn("Error destroying YouTube player:", b); } this.player = new window.YT.Player(r, { width: "100%", height: "100%", videoId: t.videoId, playerVars: { autoplay: (p = t.playerVars) != null && p.autoplay ? 1 : 0, controls: (P = t.playerVars) != null && P.controls ? 1 : 0, loop: (E = t.playerVars) != null && E.loop ? 1 : 0, start: (N = t.playerVars) == null ? void 0 : N.startTime, end: (x = t.playerVars) == null ? void 0 : x.endTime, playsinline: (D = t.playerVars) != null && D.playsinline ? 1 : 0, rel: (k = t.playerVars) != null && k.rel ? 1 : 0, origin: (O = t.playerVars) == null ? void 0 : O.origin, enablejsapi: 1 }, events: { onReady: (b) => { var m, R; const { playerInfo: c } = b.target; (R = (m = this.callbacks).onReady) == null || R.call(m, { availablePlaybackRates: c.availablePlaybackRates, availableQualityLevels: c.availableQualityLevels, currentTime: c.currentTime, duration: c.duration, muted: c.muted, playbackQuality: c.playbackQuality, playbackRate: c.playbackRate, playerState: c.playerState, size: c.size, volume: c.volume }), this.startProgressTracking(); }, onStateChange: (b) => { var m, R; const c = b.data; (R = (m = this.callbacks).onStateChange) == null || R.call(m, c), this.handleStateChange(c); }, onError: (b) => { var m, R, v, l; console.error("YouTube player error:", b.data); const c = b.data; if (X[c]) { (R = (m = this.callbacks).onError) == null || R.call(m, { code: c, message: X[c] }); return; } (l = (v = this.callbacks).onError) == null || l.call(v, { code: 1004, message: "UNKNOWN_ERROR" }); }, onPlaybackQualityChange: (b) => { var c, m; (m = (c = this.callbacks).onPlaybackQualityChange) == null || m.call(c, b.data); }, onPlaybackRateChange: (b) => { var c, m; (m = (c = this.callbacks).onPlaybackRateChange) == null || m.call(c, b.data); }, onAutoplayBlocked: this.callbacks.onAutoplayBlocked } }); } } handleStateChange(r) { if (r === z.ENDED) { this.stopProgressTracking(), this.sendProgress(); return; } if (r === z.PLAYING) { this.startProgressTracking(); return; } if (r === z.PAUSED) { this.stopProgressTracking(), this.sendProgress(); return; } if (r === z.BUFFERING) { this.startProgressTracking(); return; } if (r === z.CUED) { this.stopProgressTracking(), this.sendProgress(); return; } this.stopProgressTracking(); } startProgressTracking() { !this.progressIntervalMs || this.progressInterval || (this.progressInterval = setInterval(async () => { if (!this.player || !this.player.getCurrentTime) { this.stopProgressTracking(); return; } try { await this.sendProgress(); } catch (r) { console.error("Progress tracking error:", r), this.stopProgressTracking(); } }, this.progressIntervalMs)); } stopProgressTracking() { this.progressInterval && (clearInterval(this.progressInterval), this.progressInterval = null); } async sendProgress() { var f, y; if (!this.player || !this.player.getCurrentTime) return; const r = await this.player.getCurrentTime(), t = await this.player.getDuration(), d = t > 0 ? r / t * 100 : 0, i = await this.player.getVideoLoadedFraction(); (y = (f = this.callbacks).onProgress) == null || y.call(f, { currentTime: r, duration: t, percentage: d, loadedFraction: i }); } getPlayer() { return this.player; } play() { var r; (r = this.player) == null || r.playVideo(); } pause() { var r; (r = this.player) == null || r.pauseVideo(); } stop() { var r; (r = this.player) == null || r.stopVideo(); } async seekTo(r, t = !0) { var d; (d = this.player) == null || d.seekTo(r, t), this.seekTimeout && clearTimeout(this.seekTimeout), this.seekTimeout = setTimeout(() => { this.sendProgress(); }, 200); } setVolume(r) { var t; (t = this.player) == null || t.setVolume(r); } async getVolume() { var t; return await ((t = this.player) == null ? void 0 : t.getVolume()) ?? 0; } mute() { var r; (r = this.player) == null || r.mute(); } unMute() { var r; (r = this.player) == null || r.unMute(); } async isMuted() { var t; return await ((t = this.player) == null ? void 0 : t.isMuted()) ?? !1; } async getCurrentTime() { var t; return await ((t = this.player) == null ? void 0 : t.getCurrentTime()) ?? 0; } async getDuration() { var t; return await ((t = this.player) == null ? void 0 : t.getDuration()) ?? 0; } async getVideoUrl() { var t; return await ((t = this.player) == null ? void 0 : t.getVideoUrl()) ?? ""; } async getVideoEmbedCode() { var t; return await ((t = this.player) == null ? void 0 : t.getVideoEmbedCode()) ?? ""; } async getPlaybackRate() { var t; return await ((t = this.player) == null ? void 0 : t.getPlaybackRate()) ?? 1; } async getAvailablePlaybackRates() { var t; return await ((t = this.player) == null ? void 0 : t.getAvailablePlaybackRates()) ?? [1]; } async getPlayerState() { var t; return await ((t = this.player) == null ? void 0 : t.getPlayerState()) ?? z.UNSTARTED; } async setPlaybackRate(r) { var t; await ((t = this.player) == null ? void 0 : t.setPlaybackRate(r)); } async getVideoLoadedFraction() { var t; return await ((t = this.player) == null ? void 0 : t.getVideoLoadedFraction()) ?? 0; } loadVideoById(r, t, d) { var i; (i = this.player) == null || i.loadVideoById(r, t, d); } cueVideoById(r, t, d) { var i; (i = this.player) == null || i.cueVideoById(r, t, d); } setSize(r, t) { var d; (d = this.player) == null || d.setSize(r, t); } updateProgressInterval(r) { if (this.progressIntervalMs = r, this.progressInterval && this.stopProgressTracking(), r) { this.startProgressTracking(); return; } this.stopProgressTracking(); } updateCallbacks(r) { this.callbacks = { ...this.callbacks, ...r }; } destroy() { if (this.stopProgressTracking(), this.seekTimeout && (clearTimeout(this.seekTimeout), this.seekTimeout = null), this.player) { try { this.player.destroy(); } catch (r) { console.warn("Error destroying YouTube player:", r); } this.player = null; } } }, Ae = Ge; const Je = (a, r) => { const t = Te(() => { if (a) return typeof a == "string" ? a : "videoId" in a ? a.videoId : "url" in a ? a.url : null; }, [typeof a == "string" ? a : a && "videoId" in a ? a.videoId : a && "url" in a ? a.url : null]); return Te(() => { if (t === null) return console.error("Invalid YouTube source: ", t), r == null || r({ code: 1002, message: X[1002] }), null; if (t === void 0) return; if (Oe(t)) return t; const i = ze(t); return i || (console.error("Invalid YouTube source: ", t), r == null || r({ code: 1002, message: X[1002] }), null); }, [t, r]); }; var qe = Je; const $e = (a) => { const [r, t] = Q(), [d, i] = Q(!1), [f, y] = Q(null); return G(() => { if (!a) return; const p = new AbortController(); return y(null), t(void 0), (async () => { i(!0); try { const E = await fetch(`https://www.youtube.com/oembed?format=json&url=${encodeURIComponent(a)}`, { signal: p.signal }); if (!E.ok) throw new Error("Failed to fetch oEmbed"); const N = await E.json(); t(N); } catch (E) { if (E instanceof Error && E.name === "AbortError") return; if (E instanceof Error) { y(E); return; } y(new Error("Failed to fetch oEmbed")); } finally { i(!1); } })(), () => { p.abort(); }; }, [a]), { oEmbed: r, isLoading: d, error: f }; }; var Ke = $e; const He = () => { const [a, r] = Q(!1), t = Re((i) => { if (!window.ReactNativeWebView) return console.warn("WebView is not available"), !1; try { const f = { ...i, timestamp: Date.now() }; return window.ReactNativeWebView.postMessage(JSON.stringify(f)), !0; } catch (f) { return console.error("Failed to send WebView message:", f), !1; } }, []), d = Re((i) => { const f = (y) => { try { i(y); } catch (p) { const P = p instanceof Error ? p.message : "unknown error"; y.id && window.ReactNativeWebView && window.ReactNativeWebView.postMessage( JSON.stringify({ type: "error", id: y.id, error: { code: -5, message: `Execution failed: ${P}` } }) ); } }; return window.__execCommand = f, () => { window.__execCommand = void 0; }; }, []); return G(() => { r(!!window.ReactNativeWebView); }, []), { isWebView: a, sendMessage: t, onMessage: d }; }, Ie = (a, r) => { if (!a) return r; const t = Number(a); return Number.isNaN(t) ? r : t; }; function Ze() { const a = new URLSearchParams(window.location.search), r = a.get("videoId") ?? "", t = a.get("startTime") ?? "0", d = a.get("endTime") ?? "", i = a.get("autoplay") === "true", f = a.get("controls") === "true", y = a.get("loop") === "true", p = a.get("muted") === "true", P = a.get("playsinline") === "true", E = a.get("rel") === "true", N = a.get("origin") ?? "", [x, D] = Q(!1), { sendMessage: k, onMessage: O } = He(), b = qe(r), c = Ie(t, 0), m = Ie(d), R = ke(null), v = ke(null); return G(() => { Ae.initialize().then(() => { D(!0); const l = Ae.createInstance(); v.current = l; }); }, []), G(() => { var Y, C; if (!x || !R.current || !b) return; const l = `youtube-player-${b}`; R.current.id = l, (Y = v.current) == null || Y.updateCallbacks({ onReady: (h) => { k({ type: "ready", playerInfo: h }); }, onStateChange: (h) => { k({ type: "stateChange", state: h }); }, onError: (h) => { k({ type: "error", error: h }); }, onPlaybackRateChange: (h) => { k({ type: "playbackRateChange", playbackRate: h }); }, onPlaybackQualityChange: (h) => { k({ type: "playbackQualityChange", quality: h }); }, onAutoplayBlocked: () => { k({ type: "autoplayBlocked" }); }, onProgress: (h) => { k({ type: "progress", progress: h }); } }), (C = v.current) == null || C.createPlayer(l, { videoId: b, playerVars: { origin: N, controls: f, autoplay: i, muted: p, playsinline: P, loop: y, rel: E, startTime: c, endTime: m } }); }, [ k, x, b, N, f, i, p, P, y, E, c, m ]), G(() => () => { var l; v.current && ((l = v.current) == null || l.destroy(), v.current = null); }, []), G(() => { O((l) => { var Y, C, h; if (l.command === "updateProgressInterval") { const V = l.args || [], j = Number(V[0]) > 0 ? Number(V[0]) : 0; (Y = v.current) == null || Y.updateProgressInterval(j); return; } if (v.current) { if (l.command === "cleanup") { (C = v.current) == null || C.destroy(), v.current = null; return; } if (l.command in v.current) { const V = v.current[l.command], j = l.args || []; if (typeof V != "function") { l.id && window.ReactNativeWebView && window.ReactNativeWebView.postMessage( JSON.stringify({ type: "error", id: l.id, error: { code: -4, message: `Command not found: ${l.command}` } }) ); return; } if (l.command === "setVolume") { const _ = Number(j[0]); if (Number.isNaN(_)) return; (h = v.current) == null || h.setVolume(_); return; } const L = V.apply(v.current, j); if (L instanceof Promise) { L.then((_) => { l.id && window.ReactNativeWebView && window.ReactNativeWebView.postMessage( JSON.stringify({ type: "commandResult", id: l.id, result: _ }) ); }).catch((_) => { l.id && window.ReactNativeWebView && window.ReactNativeWebView.postMessage( JSON.stringify({ type: "error", id: l.id, error: { code: -5, message: (_ == null ? void 0 : _.message) || String(_) } }) ); }); return; } l.id && window.ReactNativeWebView && window.ReactNativeWebView.postMessage( JSON.stringify({ type: "commandResult", id: l.id, result: L }) ); } } }); }, [O]), /* @__PURE__ */ Ce.jsx("div", { id: "player-container", children: /* @__PURE__ */ Ce.jsx( "div", { ref: R, style: { width: "100%", height: "100%" } } ) }); } export { Ze as YoutubePlayer, Ke as useYoutubeOEmbed };