import _ from "react";
import { r as O } from "../../../../react-fast-compare@3.2.2/node_modules/react-fast-compare/index.mjs";
import { p as b } from "./props.mjs";
import { u as R } from "./utils.mjs";
var v = Object.create, o = Object.defineProperty, k = Object.getOwnPropertyDescriptor, T = Object.getOwnPropertyNames, L = Object.getPrototypeOf, D = Object.prototype.hasOwnProperty, C = (i, e, t) => e in i ? o(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t, S = (i, e) => {
  for (var t in e)
    o(i, t, { get: e[t], enumerable: !0 });
}, y = (i, e, t, r) => {
  if (e && typeof e == "object" || typeof e == "function")
    for (let s of T(e))
      !D.call(i, s) && s !== t && o(i, s, { get: () => e[s], enumerable: !(r = k(e, s)) || r.enumerable });
  return i;
}, c = (i, e, t) => (t = i != null ? v(L(i)) : {}, y(
  // 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.
  e || !i || !i.__esModule ? o(t, "default", { value: i, enumerable: !0 }) : t,
  i
)), E = (i) => y(o({}, "__esModule", { value: !0 }), i), a = (i, e, t) => (C(i, typeof e != "symbol" ? e + "" : e, t), t), f = {};
S(f, {
  default: () => n
});
var U = E(f), u = c(_), w = c(O), P = b, I = R;
const M = 5e3;
class n extends u.Component {
  constructor() {
    super(...arguments), a(this, "mounted", !1), a(this, "isReady", !1), a(this, "isPlaying", !1), a(this, "isLoading", !0), a(this, "loadOnReady", null), a(this, "startOnPlay", !0), a(this, "seekOnPlay", null), a(this, "onDurationCalled", !1), a(this, "handlePlayerMount", (e) => {
      if (this.player) {
        this.progress();
        return;
      }
      this.player = e, this.player.load(this.props.url), this.progress();
    }), a(this, "getInternalPlayer", (e) => this.player ? this.player[e] : null), a(this, "progress", () => {
      if (this.props.url && this.player && this.isReady) {
        const e = this.getCurrentTime() || 0, t = this.getSecondsLoaded(), r = this.getDuration();
        if (r) {
          const s = {
            playedSeconds: e,
            played: e / r
          };
          t !== null && (s.loadedSeconds = t, s.loaded = t / r), (s.playedSeconds !== this.prevPlayed || s.loadedSeconds !== this.prevLoaded) && this.props.onProgress(s), this.prevPlayed = s.playedSeconds, this.prevLoaded = s.loadedSeconds;
        }
      }
      this.progressTimeout = setTimeout(this.progress, this.props.progressFrequency || this.props.progressInterval);
    }), a(this, "handleReady", () => {
      if (!this.mounted)
        return;
      this.isReady = !0, this.isLoading = !1;
      const { onReady: e, playing: t, volume: r, muted: s } = this.props;
      e(), !s && r !== null && this.player.setVolume(r), this.loadOnReady ? (this.player.load(this.loadOnReady, !0), this.loadOnReady = null) : t && this.player.play(), this.handleDurationCheck();
    }), a(this, "handlePlay", () => {
      this.isPlaying = !0, this.isLoading = !1;
      const { onStart: e, onPlay: t, playbackRate: r } = this.props;
      this.startOnPlay && (this.player.setPlaybackRate && r !== 1 && this.player.setPlaybackRate(r), e(), this.startOnPlay = !1), t(), this.seekOnPlay && (this.seekTo(this.seekOnPlay), this.seekOnPlay = null), this.handleDurationCheck();
    }), a(this, "handlePause", (e) => {
      this.isPlaying = !1, this.isLoading || this.props.onPause(e);
    }), a(this, "handleEnded", () => {
      const { activePlayer: e, loop: t, onEnded: r } = this.props;
      e.loopOnEnded && t && this.seekTo(0), t || (this.isPlaying = !1, r());
    }), a(this, "handleError", (...e) => {
      this.isLoading = !1, this.props.onError(...e);
    }), a(this, "handleDurationCheck", () => {
      clearTimeout(this.durationCheckTimeout);
      const e = this.getDuration();
      e ? this.onDurationCalled || (this.props.onDuration(e), this.onDurationCalled = !0) : this.durationCheckTimeout = setTimeout(this.handleDurationCheck, 100);
    }), a(this, "handleLoaded", () => {
      this.isLoading = !1;
    });
  }
  componentDidMount() {
    this.mounted = !0;
  }
  componentWillUnmount() {
    clearTimeout(this.progressTimeout), clearTimeout(this.durationCheckTimeout), this.isReady && this.props.stopOnUnmount && (this.player.stop(), this.player.disablePIP && this.player.disablePIP()), this.mounted = !1;
  }
  componentDidUpdate(e) {
    if (!this.player)
      return;
    const { url: t, playing: r, volume: s, muted: l, playbackRate: h, pip: d, loop: p, activePlayer: g, disableDeferredLoading: m } = this.props;
    if (!(0, w.default)(e.url, t)) {
      if (this.isLoading && !g.forceLoad && !m && !(0, I.isMediaStream)(t)) {
        console.warn(`ReactPlayer: the attempt to load ${t} is being deferred until the player has loaded`), this.loadOnReady = t;
        return;
      }
      this.isLoading = !0, this.startOnPlay = !0, this.onDurationCalled = !1, this.player.load(t, this.isReady);
    }
    !e.playing && r && !this.isPlaying && this.player.play(), e.playing && !r && this.isPlaying && this.player.pause(), !e.pip && d && this.player.enablePIP && this.player.enablePIP(), e.pip && !d && this.player.disablePIP && this.player.disablePIP(), e.volume !== s && s !== null && this.player.setVolume(s), e.muted !== l && (l ? this.player.mute() : (this.player.unmute(), s !== null && setTimeout(() => this.player.setVolume(s)))), e.playbackRate !== h && this.player.setPlaybackRate && this.player.setPlaybackRate(h), e.loop !== p && this.player.setLoop && this.player.setLoop(p);
  }
  getDuration() {
    return this.isReady ? this.player.getDuration() : null;
  }
  getCurrentTime() {
    return this.isReady ? this.player.getCurrentTime() : null;
  }
  getSecondsLoaded() {
    return this.isReady ? this.player.getSecondsLoaded() : null;
  }
  seekTo(e, t, r) {
    if (!this.isReady) {
      e !== 0 && (this.seekOnPlay = e, setTimeout(() => {
        this.seekOnPlay = null;
      }, M));
      return;
    }
    if (t ? t === "fraction" : e > 0 && e < 1) {
      const l = this.player.getDuration();
      if (!l) {
        console.warn("ReactPlayer: could not seek using fraction – duration not yet available");
        return;
      }
      this.player.seekTo(l * e, r);
      return;
    }
    this.player.seekTo(e, r);
  }
  render() {
    const e = this.props.activePlayer;
    return e ? /* @__PURE__ */ u.default.createElement(
      e,
      {
        ...this.props,
        onMount: this.handlePlayerMount,
        onReady: this.handleReady,
        onPlay: this.handlePlay,
        onPause: this.handlePause,
        onEnded: this.handleEnded,
        onLoaded: this.handleLoaded,
        onError: this.handleError
      }
    ) : null;
  }
}
a(n, "displayName", "Player");
a(n, "propTypes", P.propTypes);
a(n, "defaultProps", P.defaultProps);
export {
  U as P
};
//# sourceMappingURL=Player.mjs.map
