"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // 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.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var player_exports = {};
__export(player_exports, {
  Player: () => Player
});
module.exports = __toCommonJS(player_exports);
var import_react = __toESM(require("react"));
var import_props = require("./props.js");
var import_utils = require("./utils.js");
const SEEK_ON_PLAY_EXPIRY = 5e3;
class Player extends import_react.Component {
  constructor() {
    super(...arguments);
    this.mounted = false;
    this.isReady = false;
    this.isPlaying = false;
    // Track playing state internally to prevent bugs
    this.isLoading = true;
    // Use isLoading to prevent onPause when switching URL
    this.loadOnReady = null;
    this.startOnPlay = true;
    this.seekOnPlay = null;
    this.onDurationCalled = false;
    this.getInternalPlayer = (key) => {
      if (!this.player) {
        return null;
      }
      return this.player[key];
    };
    this.progress = () => {
      if (this.props.url && this.player && this.isReady) {
        const playedSeconds = this.getCurrentTime() || 0;
        const loadedSeconds = this.getSecondsLoaded();
        const duration = this.getDuration();
        if (duration) {
          const progress = {
            playedSeconds,
            played: playedSeconds / duration
          };
          if (loadedSeconds !== null) {
            progress.loadedSeconds = loadedSeconds;
            progress.loaded = loadedSeconds / duration;
          }
          if (progress.playedSeconds !== this.prevPlayed || // @ts-expect-error
          progress.loadedSeconds !== this.prevLoaded) {
            this.props.onProgress(progress);
          }
          this.prevPlayed = progress.playedSeconds;
          this.prevLoaded = progress.loadedSeconds;
        }
      }
      this.progressTimeout = setTimeout(
        this.progress,
        this.props.progressFrequency || this.props.progressInterval
      );
    };
    this.handleReady = () => {
      if (!this.mounted) {
        return;
      }
      this.isReady = true;
      this.isLoading = false;
      const { onReady, playing, volume, muted } = this.props;
      onReady();
      if (!muted && volume !== null) {
        this.player.setVolume(volume);
      }
      if (this.loadOnReady) {
        this.player.load(this.loadOnReady, true);
        this.loadOnReady = null;
      } else if (playing) {
        this.player.play();
      }
      this.handleDurationCheck();
    };
    this.handlePlay = () => {
      this.isPlaying = true;
      this.isLoading = false;
      const { onStart, onPlay, playbackRate } = this.props;
      if (this.startOnPlay) {
        if (this.player.setPlaybackRate && playbackRate !== 1) {
          this.player.setPlaybackRate(playbackRate);
        }
        onStart();
        this.startOnPlay = false;
      }
      onPlay();
      if (this.seekOnPlay) {
        this.seekTo(this.seekOnPlay);
        this.seekOnPlay = null;
      }
      this.handleDurationCheck();
    };
    this.handlePause = (e) => {
      this.isPlaying = false;
      if (!this.isLoading) {
        this.props.onPause(e);
      }
    };
    this.handleEnded = () => {
      const { activePlayer, loop, onEnded } = this.props;
      if (activePlayer.loopOnEnded && loop) {
        this.seekTo(0);
      }
      if (!loop) {
        this.isPlaying = false;
        onEnded();
      }
    };
    this.handleError = (...args) => {
      this.isLoading = false;
      this.props.onError(...args);
    };
    this.handleDurationCheck = () => {
      clearTimeout(this.durationCheckTimeout);
      const duration = this.getDuration();
      if (duration) {
        if (!this.onDurationCalled) {
          this.props.onDuration(duration);
          this.onDurationCalled = true;
        }
      } else {
        this.durationCheckTimeout = setTimeout(this.handleDurationCheck, 100);
      }
    };
    this.handleLoaded = () => {
      this.isLoading = false;
    };
    this.ref = (player) => {
      if (player) {
        this.player = player;
      }
    };
  }
  componentDidMount() {
    this.mounted = true;
    this.player.load(this.props.url);
    this.progress();
  }
  componentWillUnmount() {
    clearTimeout(this.progressTimeout);
    clearTimeout(this.durationCheckTimeout);
    if (this.isReady) {
      this.player.stop();
    }
    if (this.player.disablePIP) {
      this.player.disablePIP();
    }
    this.mounted = false;
  }
  componentDidUpdate(prevProps) {
    const { url, playing, volume, muted, playbackRate, pip, loop, activePlayer } = this.props;
    if (!(0, import_utils.isEqual)(prevProps.url, url)) {
      if (this.isLoading && !activePlayer.forceLoad) {
        console.warn(
          `ReactPlayer: the attempt to load ${url} is being deferred until the player has loaded`
        );
        this.loadOnReady = url;
        return;
      }
      this.isLoading = true;
      this.startOnPlay = true;
      this.onDurationCalled = false;
      this.player.load(url, this.isReady);
    }
    if (!prevProps.playing && playing && !this.isPlaying) {
      this.player.play();
    }
    if (prevProps.playing && !playing && this.isPlaying) {
      this.player.pause();
    }
    if (!prevProps.pip && pip && this.player.enablePIP) {
      this.player.enablePIP();
    }
    if (prevProps.pip && !pip && this.player.disablePIP) {
      this.player.disablePIP();
    }
    if (prevProps.volume !== volume && volume !== null) {
      this.player.setVolume(volume);
    }
    if (prevProps.muted !== muted) {
      if (muted) {
        this.player.mute();
      } else {
        this.player.unmute();
        if (volume !== null) {
          setTimeout(() => this.player.setVolume(volume));
        }
      }
    }
    if (prevProps.playbackRate !== playbackRate && this.player.setPlaybackRate) {
      this.player.setPlaybackRate(playbackRate);
    }
    if (prevProps.loop !== loop && this.player.setLoop) {
      this.player.setLoop(loop);
    }
  }
  getDuration() {
    if (!this.isReady) {
      return null;
    }
    return this.player.getDuration();
  }
  getCurrentTime() {
    if (!this.isReady) {
      return null;
    }
    return this.player.getCurrentTime();
  }
  getSecondsLoaded() {
    if (!this.isReady) {
      return null;
    }
    return this.player.getSecondsLoaded();
  }
  seekTo(amount, type) {
    if (!this.isReady && amount !== 0) {
      this.seekOnPlay = amount;
      setTimeout(() => {
        this.seekOnPlay = null;
      }, SEEK_ON_PLAY_EXPIRY);
      return;
    }
    const isFraction = !type ? amount > 0 && amount < 1 : type === "fraction";
    if (isFraction) {
      const duration = this.player.getDuration();
      if (!duration) {
        console.warn("ReactPlayer: could not seek using fraction – duration not yet available");
        return;
      }
      this.player.seekTo(duration * amount);
      return;
    }
    this.player.seekTo(amount);
  }
  render() {
    const Player2 = this.props.activePlayer;
    if (!Player2) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement(
      Player2,
      {
        ...this.props,
        ref: this.ref,
        onReady: this.handleReady,
        onPlay: this.handlePlay,
        onPause: this.handlePause,
        onEnded: this.handleEnded,
        onLoaded: this.handleLoaded,
        onError: this.handleError
      }
    );
  }
}
Player.displayName = "Player";
Player.propTypes = import_props.propTypes;
Player.defaultProps = import_props.defaultProps;
//# sourceMappingURL=player.js.map
