var K = Object.defineProperty;
var B = (r) => {
  throw TypeError(r);
};
var N = (r, n, e) => n in r ? K(r, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[n] = e;
var T = (r, n, e) => N(r, typeof n != "symbol" ? n + "" : n, e), R = (r, n, e) => n.has(r) || B("Cannot " + e);
var s = (r, n, e) => (R(r, n, "read from private field"), e ? e.call(r) : n.get(r)), p = (r, n, e) => n.has(r) ? B("Cannot add the same private member more than once") : n instanceof WeakSet ? n.add(r) : n.set(r, e), l = (r, n, e, t) => (R(r, n, "write to private field"), t ? t.call(r, e) : n.set(r, e), e), o = (r, n, e) => (R(r, n, "access private method"), e);
import { y as q, J as z, p as A, e as G, b as H, M as Q, K as C, O as W, t as $, P as X } from "./install-CGlpWYKn.js";
import { E as Z } from "./vidstack-DDXt6fpN-5Vior0vI.js";
import { resolveYouTubeVideoId as ee } from "./vidstack-Dm1xEU9Q-qSXq3AI-.js";
const b = {
  Ended: 0,
  Playing: 1,
  Paused: 2,
  Buffering: 3,
  Cued: 5
};
var a, k, v, S, P, m, y, i, te, se, D, f, j, Y, F, I, M, x, L, O, V, U;
class ne extends Z {
  constructor(e, t) {
    super(e);
    p(this, i);
    T(this, "$$PROVIDER_TYPE", "YOUTUBE");
    T(this, "scope", q());
    p(this, a);
    p(this, k, z(""));
    p(this, v, -1);
    p(this, S, null);
    p(this, P, -1);
    p(this, m, !1);
    p(this, y, /* @__PURE__ */ new Map());
    /**
     * Sets the player's interface language. The parameter value is an ISO 639-1 two-letter
     * language code or a fully specified locale. For example, fr and fr-ca are both valid values.
     * Other language input codes, such as IETF language tags (BCP 47) might also be handled properly.
     *
     * The interface language is used for tooltips in the player and also affects the default caption
     * track. Note that YouTube might select a different caption track language for a particular
     * user based on the user's individual language preferences and the availability of caption tracks.
     *
     * @defaultValue 'en'
     */
    T(this, "language", "en");
    T(this, "color", "red");
    /**
     * Whether cookies should be enabled on the embed. This is turned off by default to be
     * GDPR-compliant.
     *
     * @defaultValue `false`
     */
    T(this, "cookies", !1);
    l(this, a, t);
  }
  get currentSrc() {
    return s(this, S);
  }
  get type() {
    return "youtube";
  }
  get videoId() {
    return s(this, k).call(this);
  }
  preconnect() {
    A(this.getOrigin());
  }
  setup() {
    super.setup(), G(o(this, i, D).bind(this)), s(this, a).notify("provider-setup", this);
  }
  destroy() {
    o(this, i, O).call(this);
    const e = "provider destroyed";
    for (const t of s(this, y).values())
      for (const { reject: d } of t) d(e);
    s(this, y).clear();
  }
  async play() {
    return o(this, i, f).call(this, "playVideo");
  }
  async pause() {
    return o(this, i, f).call(this, "pauseVideo");
  }
  setMuted(e) {
    e ? o(this, i, f).call(this, "mute") : o(this, i, f).call(this, "unMute");
  }
  setCurrentTime(e) {
    o(this, i, f).call(this, "seekTo", e), s(this, a).notify("seeking", e);
  }
  setVolume(e) {
    o(this, i, f).call(this, "setVolume", e * 100);
  }
  setPlaybackRate(e) {
    o(this, i, f).call(this, "setPlaybackRate", e);
  }
  async loadSource(e) {
    if (!H(e.src)) {
      l(this, S, null), s(this, k).set("");
      return;
    }
    const t = ee(e.src);
    s(this, k).set(t ?? ""), l(this, S, e);
  }
  getOrigin() {
    return this.cookies ? "https://www.youtube.com" : "https://www.youtube-nocookie.com";
  }
  buildParams() {
    const { keyDisabled: e } = s(this, a).$props, { muted: t, playsInline: d, nativeControls: u } = s(this, a).$state, h = u();
    return {
      rel: 0,
      autoplay: 0,
      cc_lang_pref: this.language,
      cc_load_policy: h ? 1 : void 0,
      color: this.color,
      controls: h ? 1 : 0,
      disablekb: !h || e() ? 1 : 0,
      enablejsapi: 1,
      fs: 1,
      hl: this.language,
      iv_load_policy: h ? 1 : 3,
      mute: t() ? 1 : 0,
      playsinline: d() ? 1 : 0
    };
  }
  onLoad() {
    window.setTimeout(() => this.postMessage({ event: "listening" }), 100);
  }
  onMessage({ info: e }, t) {
    var c;
    if (!e) return;
    const { title: d, intrinsicDuration: u, playbackRate: h } = s(this, a).$state;
    if (W(e.videoData) && e.videoData.title !== d() && s(this, a).notify("title-change", e.videoData.title, t), $(e.duration) && e.duration !== u()) {
      if ($(e.videoLoadedFraction)) {
        const g = ((c = e.progressState) == null ? void 0 : c.loaded) ?? e.videoLoadedFraction * e.duration, w = new C(0, e.duration);
        o(this, i, I).call(this, g, w, t);
      }
      s(this, a).notify("duration-change", e.duration, t);
    }
    if ($(e.playbackRate) && e.playbackRate !== h() && s(this, a).notify("rate-change", e.playbackRate, t), e.progressState) {
      const { current: g, seekableStart: w, seekableEnd: E, loaded: J, duration: _ } = e.progressState;
      o(this, i, F).call(this, g, t), o(this, i, I).call(this, J, new C(w, E), t), _ !== u() && s(this, a).notify("duration-change", _, t);
    }
    if ($(e.volume) && X(e.muted) && !s(this, m)) {
      const g = {
        muted: e.muted,
        volume: e.volume / 100
      };
      s(this, a).notify("volume-change", g, t);
    }
    $(e.playerState) && e.playerState !== s(this, v) && o(this, i, L).call(this, e.playerState, t);
  }
}
a = new WeakMap(), k = new WeakMap(), v = new WeakMap(), S = new WeakMap(), P = new WeakMap(), m = new WeakMap(), y = new WeakMap(), i = new WeakSet(), te = function(e) {
  var t;
  (t = o(this, i, V).call(this, "playVideo")) == null || t.reject(e);
}, se = function(e) {
  var t;
  (t = o(this, i, V).call(this, "pauseVideo")) == null || t.reject(e);
}, D = function() {
  o(this, i, O).call(this);
  const e = s(this, k).call(this);
  if (!e) {
    this.src.set("");
    return;
  }
  this.src.set(`${this.getOrigin()}/embed/${e}`), s(this, a).notify("load-start");
}, f = function(e, t) {
  let d = Q(), u = s(this, y).get(e);
  return u || s(this, y).set(e, u = []), u.push(d), this.postMessage({
    event: "command",
    func: e,
    args: t ? [t] : void 0
  }), d.promise;
}, j = function(e) {
  s(this, a).notify("loaded-metadata"), s(this, a).notify("loaded-data"), s(this, a).delegate.ready(void 0, e);
}, Y = function(e) {
  var t;
  (t = o(this, i, V).call(this, "pauseVideo")) == null || t.resolve(), s(this, a).notify("pause", void 0, e);
}, F = function(e, t) {
  const { duration: d, realCurrentTime: u } = s(this, a).$state, h = s(this, v) === b.Ended, c = h ? d() : e;
  s(this, a).notify("time-change", c, t), !h && Math.abs(c - u()) > 1 && s(this, a).notify("seeking", c, t);
}, I = function(e, t, d) {
  const u = {
    buffered: new C(0, e),
    seekable: t
  };
  s(this, a).notify("progress", u, d);
  const { seeking: h, realCurrentTime: c } = s(this, a).$state;
  h() && e > c() && o(this, i, M).call(this, d);
}, M = function(e) {
  const { paused: t, realCurrentTime: d } = s(this, a).$state;
  window.clearTimeout(s(this, P)), l(this, P, window.setTimeout(
    () => {
      s(this, a).notify("seeked", d(), e), l(this, P, -1);
    },
    t() ? 100 : 0
  ));
}, x = function(e) {
  const { seeking: t } = s(this, a).$state;
  t() && o(this, i, M).call(this, e), s(this, a).notify("pause", void 0, e), s(this, a).notify("end", void 0, e);
}, L = function(e, t) {
  var E;
  const { paused: d, seeking: u } = s(this, a).$state, h = e === b.Playing, c = e === b.Buffering, g = o(this, i, U).call(this, "playVideo"), w = d() && (c || h);
  if (c && s(this, a).notify("waiting", void 0, t), u() && h && o(this, i, M).call(this, t), s(this, m) && h) {
    this.pause(), l(this, m, !1), this.setMuted(s(this, a).$state.muted());
    return;
  }
  if (!g && w) {
    l(this, m, !0), this.setMuted(!0);
    return;
  }
  switch (w && ((E = o(this, i, V).call(this, "playVideo")) == null || E.resolve(), s(this, a).notify("play", void 0, t)), e) {
    case b.Cued:
      o(this, i, j).call(this, t);
      break;
    case b.Playing:
      s(this, a).notify("playing", void 0, t);
      break;
    case b.Paused:
      o(this, i, Y).call(this, t);
      break;
    case b.Ended:
      o(this, i, x).call(this, t);
      break;
  }
  l(this, v, e);
}, O = function() {
  l(this, v, -1), l(this, P, -1), l(this, m, !1);
}, V = function(e) {
  var t;
  return (t = s(this, y).get(e)) == null ? void 0 : t.shift();
}, U = function(e) {
  var t;
  return !!((t = s(this, y).get(e)) != null && t.length);
};
export {
  ne as YouTubeProvider
};
