import O from "react";
import { __require as w } from "../patterns.js";
var l, f;
function D() {
  if (f) return l;
  f = 1;
  var E = Object.create, o = Object.defineProperty, _ = Object.getOwnPropertyDescriptor, g = Object.getOwnPropertyNames, b = Object.getPrototypeOf, L = Object.prototype.hasOwnProperty, I = (r, e, t) => e in r ? o(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, k = (r, e) => {
    for (var t in e)
      o(r, t, { get: e[t], enumerable: !0 });
  }, d = (r, e, t, i) => {
    if (e && typeof e == "object" || typeof e == "function")
      for (let s of g(e))
        !L.call(r, s) && s !== t && o(r, s, { get: () => e[s], enumerable: !(i = _(e, s)) || i.enumerable });
    return r;
  }, R = (r, e, t) => (t = r != null ? E(b(r)) : {}, d(
    // 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.
    !r || !r.__esModule ? o(t, "default", { value: r, enumerable: !0 }) : t,
    r
  )), x = (r) => d(o({}, "__esModule", { value: !0 }), r), n = (r, e, t) => (I(r, typeof e != "symbol" ? e + "" : e, t), t), c = {};
  k(c, {
    default: () => p
  }), l = x(c);
  var v = R(O), u = /* @__PURE__ */ w();
  const M = "https://cdn.jsdelivr.net/npm/@mux/mux-player@VERSION/dist/mux-player.mjs";
  class p extends v.Component {
    constructor() {
      super(...arguments), n(this, "onReady", (...e) => this.props.onReady(...e)), n(this, "onPlay", (...e) => this.props.onPlay(...e)), n(this, "onBuffer", (...e) => this.props.onBuffer(...e)), n(this, "onBufferEnd", (...e) => this.props.onBufferEnd(...e)), n(this, "onPause", (...e) => this.props.onPause(...e)), n(this, "onEnded", (...e) => this.props.onEnded(...e)), n(this, "onError", (...e) => this.props.onError(...e)), n(this, "onPlayBackRateChange", (e) => this.props.onPlaybackRateChange(e.target.playbackRate)), n(this, "onEnablePIP", (...e) => this.props.onEnablePIP(...e)), n(this, "onSeek", (e) => {
        this.props.onSeek(e.target.currentTime);
      }), n(this, "onDurationChange", () => {
        const e = this.getDuration();
        this.props.onDuration(e);
      }), n(this, "mute", () => {
        this.player.muted = !0;
      }), n(this, "unmute", () => {
        this.player.muted = !1;
      }), n(this, "ref", (e) => {
        this.player = e;
      });
    }
    componentDidMount() {
      this.props.onMount && this.props.onMount(this), this.addListeners(this.player);
      const e = this.getPlaybackId(this.props.url);
      e && (this.player.playbackId = e);
    }
    componentWillUnmount() {
      this.player.playbackId = null, this.removeListeners(this.player);
    }
    addListeners(e) {
      const { playsinline: t } = this.props;
      e.addEventListener("play", this.onPlay), e.addEventListener("waiting", this.onBuffer), e.addEventListener("playing", this.onBufferEnd), e.addEventListener("pause", this.onPause), e.addEventListener("seeked", this.onSeek), e.addEventListener("ended", this.onEnded), e.addEventListener("error", this.onError), e.addEventListener("ratechange", this.onPlayBackRateChange), e.addEventListener("enterpictureinpicture", this.onEnablePIP), e.addEventListener("leavepictureinpicture", this.onDisablePIP), e.addEventListener("webkitpresentationmodechanged", this.onPresentationModeChange), e.addEventListener("canplay", this.onReady), t && e.setAttribute("playsinline", "");
    }
    removeListeners(e) {
      e.removeEventListener("canplay", this.onReady), e.removeEventListener("play", this.onPlay), e.removeEventListener("waiting", this.onBuffer), e.removeEventListener("playing", this.onBufferEnd), e.removeEventListener("pause", this.onPause), e.removeEventListener("seeked", this.onSeek), e.removeEventListener("ended", this.onEnded), e.removeEventListener("error", this.onError), e.removeEventListener("ratechange", this.onPlayBackRateChange), e.removeEventListener("enterpictureinpicture", this.onEnablePIP), e.removeEventListener("leavepictureinpicture", this.onDisablePIP), e.removeEventListener("canplay", this.onReady);
    }
    async load(e) {
      var t;
      const { onError: i, config: s } = this.props;
      if (!((t = globalThis.customElements) != null && t.get("mux-player")))
        try {
          await import(
            /* webpackIgnore: true */
            `${M.replace("VERSION", s.version)}`
          ), this.props.onLoaded();
        } catch (a) {
          i(a);
        }
      const [, h] = e.match(u.MATCH_URL_MUX);
      this.player.playbackId = h;
    }
    play() {
      const e = this.player.play();
      e && e.catch(this.props.onError);
    }
    pause() {
      this.player.pause();
    }
    stop() {
      this.player.playbackId = null;
    }
    seekTo(e, t = !0) {
      this.player.currentTime = e, t || this.pause();
    }
    setVolume(e) {
      this.player.volume = e;
    }
    enablePIP() {
      this.player.requestPictureInPicture && document.pictureInPictureElement !== this.player && this.player.requestPictureInPicture();
    }
    disablePIP() {
      document.exitPictureInPicture && document.pictureInPictureElement === this.player && document.exitPictureInPicture();
    }
    setPlaybackRate(e) {
      try {
        this.player.playbackRate = e;
      } catch (t) {
        this.props.onError(t);
      }
    }
    getDuration() {
      if (!this.player)
        return null;
      const { duration: e, seekable: t } = this.player;
      return e === 1 / 0 && t.length > 0 ? t.end(t.length - 1) : e;
    }
    getCurrentTime() {
      return this.player ? this.player.currentTime : null;
    }
    getSecondsLoaded() {
      if (!this.player)
        return null;
      const { buffered: e } = this.player;
      if (e.length === 0)
        return 0;
      const t = e.end(e.length - 1), i = this.getDuration();
      return t > i ? i : t;
    }
    getPlaybackId(e) {
      const [, t] = e.match(u.MATCH_URL_MUX);
      return t;
    }
    render() {
      const { url: e, playing: t, loop: i, controls: s, muted: h, config: a, width: m, height: y } = this.props, P = {
        width: m === "auto" ? m : "100%",
        height: y === "auto" ? y : "100%"
      };
      return s === !1 && (P["--controls"] = "none"), /* @__PURE__ */ v.default.createElement(
        "mux-player",
        {
          ref: this.ref,
          "playback-id": this.getPlaybackId(e),
          style: P,
          preload: "auto",
          autoPlay: t || void 0,
          muted: h ? "" : void 0,
          loop: i ? "" : void 0,
          ...a.attributes
        }
      );
    }
  }
  return n(p, "displayName", "Mux"), n(p, "canPlay", u.canPlay.mux), l;
}
export {
  D as __require
};
//# sourceMappingURL=Mux.js.map
