import F from "react";
import { __require as G } from "../utils.js";
import { __require as z } from "../patterns.js";
var v, E;
function W() {
  if (E) return v;
  E = 1;
  var C = Object.create, p = Object.defineProperty, A = Object.getOwnPropertyDescriptor, R = Object.getOwnPropertyNames, U = Object.getPrototypeOf, I = Object.prototype.hasOwnProperty, L = (a, e, t) => e in a ? p(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t, M = (a, e) => {
    for (var t in e)
      p(a, t, { get: e[t], enumerable: !0 });
  }, O = (a, e, t, n) => {
    if (e && typeof e == "object" || typeof e == "function")
      for (let r of R(e))
        !I.call(a, r) && r !== t && p(a, r, { get: () => e[r], enumerable: !(n = A(e, r)) || n.enumerable });
    return a;
  }, Y = (a, e, t) => (t = a != null ? C(U(a)) : {}, O(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    !a || !a.__esModule ? p(t, "default", { value: a, enumerable: !0 }) : t,
    a
  )), k = (a) => O(p({}, "__esModule", { value: !0 }), a), s = (a, e, t) => (L(a, typeof e != "symbol" ? e + "" : e, t), t), w = {};
  M(w, {
    default: () => P
  }), v = k(w);
  var _ = Y(F), i = /* @__PURE__ */ G(), S = /* @__PURE__ */ z();
  const N = "https://www.youtube.com/iframe_api", D = "YT", V = "onYouTubeIframeAPIReady", u = /[?&](?:list|channel)=([a-zA-Z0-9_-]+)/, f = /user\/([a-zA-Z0-9_-]+)\/?/, q = /youtube-nocookie\.com/, B = "https://www.youtube-nocookie.com";
  class P extends _.Component {
    constructor() {
      super(...arguments), s(this, "callPlayer", i.callPlayer), s(this, "parsePlaylist", (e) => {
        if (e instanceof Array)
          return {
            listType: "playlist",
            playlist: e.map(this.getID).join(",")
          };
        if (u.test(e)) {
          const [, t] = e.match(u);
          return {
            listType: "playlist",
            list: t.replace(/^UC/, "UU")
          };
        }
        if (f.test(e)) {
          const [, t] = e.match(f);
          return {
            listType: "user_uploads",
            list: t
          };
        }
        return {};
      }), s(this, "onStateChange", (e) => {
        const { data: t } = e, { onPlay: n, onPause: r, onBuffer: m, onBufferEnd: g, onEnded: b, onReady: T, loop: y, config: { playerVars: l, onUnstarted: h } } = this.props, { UNSTARTED: d, PLAYING: c, PAUSED: o, BUFFERING: j, ENDED: K, CUED: H } = window[D].PlayerState;
        if (t === d && h(), t === c && (n(), g()), t === o && r(), t === j && m(), t === K) {
          const x = !!this.callPlayer("getPlaylist");
          y && !x && (l.start ? this.seekTo(l.start) : this.play()), b();
        }
        t === H && T();
      }), s(this, "mute", () => {
        this.callPlayer("mute");
      }), s(this, "unmute", () => {
        this.callPlayer("unMute");
      }), s(this, "ref", (e) => {
        this.container = e;
      });
    }
    componentDidMount() {
      this.props.onMount && this.props.onMount(this);
    }
    getID(e) {
      return !e || e instanceof Array || u.test(e) ? null : e.match(S.MATCH_URL_YOUTUBE)[1];
    }
    load(e, t) {
      const { playing: n, muted: r, playsinline: m, controls: g, loop: b, config: T, onError: y } = this.props, { playerVars: l, embedOptions: h } = T, d = this.getID(e);
      if (t) {
        if (u.test(e) || f.test(e) || e instanceof Array) {
          this.player.loadPlaylist(this.parsePlaylist(e));
          return;
        }
        this.player.cueVideoById({
          videoId: d,
          startSeconds: (0, i.parseStartTime)(e) || l.start,
          endSeconds: (0, i.parseEndTime)(e) || l.end
        });
        return;
      }
      (0, i.getSDK)(N, D, V, (c) => c.loaded).then((c) => {
        this.container && (this.player = new c.Player(this.container, {
          width: "100%",
          height: "100%",
          videoId: d,
          playerVars: {
            autoplay: n ? 1 : 0,
            mute: r ? 1 : 0,
            controls: g ? 1 : 0,
            start: (0, i.parseStartTime)(e),
            end: (0, i.parseEndTime)(e),
            origin: window.location.origin,
            playsinline: m ? 1 : 0,
            ...this.parsePlaylist(e),
            ...l
          },
          events: {
            onReady: () => {
              b && this.player.setLoop(!0), this.props.onReady();
            },
            onPlaybackRateChange: (o) => this.props.onPlaybackRateChange(o.data),
            onPlaybackQualityChange: (o) => this.props.onPlaybackQualityChange(o),
            onStateChange: this.onStateChange,
            onError: (o) => y(o.data)
          },
          host: q.test(e) ? B : void 0,
          ...h
        }));
      }, y), h.events && console.warn("Using `embedOptions.events` will likely break things. Use ReactPlayer’s callback props instead, eg onReady, onPlay, onPause");
    }
    play() {
      this.callPlayer("playVideo");
    }
    pause() {
      this.callPlayer("pauseVideo");
    }
    stop() {
      document.body.contains(this.callPlayer("getIframe")) && this.callPlayer("stopVideo");
    }
    seekTo(e, t = !1) {
      this.callPlayer("seekTo", e), !t && !this.props.playing && this.pause();
    }
    setVolume(e) {
      this.callPlayer("setVolume", e * 100);
    }
    setPlaybackRate(e) {
      this.callPlayer("setPlaybackRate", e);
    }
    setLoop(e) {
      this.callPlayer("setLoop", e);
    }
    getDuration() {
      return this.callPlayer("getDuration");
    }
    getCurrentTime() {
      return this.callPlayer("getCurrentTime");
    }
    getSecondsLoaded() {
      return this.callPlayer("getVideoLoadedFraction") * this.getDuration();
    }
    render() {
      const { display: e } = this.props, t = {
        width: "100%",
        height: "100%",
        display: e
      };
      return /* @__PURE__ */ _.default.createElement("div", { style: t }, /* @__PURE__ */ _.default.createElement("div", { ref: this.ref }));
    }
  }
  return s(P, "displayName", "YouTube"), s(P, "canPlay", S.canPlay.youtube), v;
}
export {
  W as __require
};
//# sourceMappingURL=YouTube.js.map
