import { commonjsGlobal } from "../../../../../../_virtual/_commonjsHelpers.js";
import React__default from "react";
import { __require as requireCjs } from "../../../../deepmerge@4.3.1/node_modules/deepmerge/dist/cjs.js";
import require$$2 from "../../../../../../_virtual/memoize-one.esm.js";
import { __require as requireReactFastCompare } from "../../../../react-fast-compare@3.2.2/node_modules/react-fast-compare/index.js";
import { __require as requireProps } from "./props.js";
import { __require as requireUtils } from "./utils.js";
import { __require as requirePlayer } from "./Player.js";
var ReactPlayer;
var hasRequiredReactPlayer;
function requireReactPlayer() {
  if (hasRequiredReactPlayer) return ReactPlayer;
  hasRequiredReactPlayer = 1;
  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 __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  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.
    !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
    mod
  ));
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  var __publicField = (obj, key, value) => {
    __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
    return value;
  };
  var ReactPlayer_exports = {};
  __export(ReactPlayer_exports, {
    createReactPlayer: () => createReactPlayer
  });
  ReactPlayer = __toCommonJS(ReactPlayer_exports);
  var import_react2 = __toESM(React__default);
  var import_deepmerge = __toESM(requireCjs());
  var import_memoize_one = __toESM(require$$2);
  var import_react_fast_compare2 = __toESM(requireReactFastCompare());
  var import_props2 = /* @__PURE__ */ requireProps();
  var import_utils2 = /* @__PURE__ */ requireUtils();
  var import_Player2 = __toESM(/* @__PURE__ */ requirePlayer());
  const Preview = (0, import_utils2.lazy)(() => import(
    /* webpackChunkName: 'reactPlayerPreview' */
    "../../../../../../_virtual/Preview.js"
  ).then((n) => n.P));
  const IS_BROWSER = typeof window !== "undefined" && window.document && typeof document !== "undefined";
  const IS_GLOBAL = typeof commonjsGlobal !== "undefined" && commonjsGlobal.window && commonjsGlobal.window.document;
  const SUPPORTED_PROPS = Object.keys(import_props2.propTypes);
  const UniversalSuspense = IS_BROWSER || IS_GLOBAL ? import_react2.Suspense : () => null;
  const customPlayers = [];
  const createReactPlayer = (players, fallback) => {
    var _a;
    return _a = class extends import_react2.Component {
      constructor() {
        super(...arguments);
        __publicField(this, "state", {
          showPreview: !!this.props.light
        });
        __publicField(this, "references", {
          wrapper: (wrapper) => {
            this.wrapper = wrapper;
          },
          player: (player) => {
            this.player = player;
          }
        });
        __publicField(this, "handleClickPreview", (e) => {
          this.setState({ showPreview: false });
          this.props.onClickPreview(e);
        });
        __publicField(this, "showPreview", () => {
          this.setState({ showPreview: true });
        });
        __publicField(this, "getDuration", () => {
          if (!this.player)
            return null;
          return this.player.getDuration();
        });
        __publicField(this, "getCurrentTime", () => {
          if (!this.player)
            return null;
          return this.player.getCurrentTime();
        });
        __publicField(this, "getSecondsLoaded", () => {
          if (!this.player)
            return null;
          return this.player.getSecondsLoaded();
        });
        __publicField(this, "getInternalPlayer", (key = "player") => {
          if (!this.player)
            return null;
          return this.player.getInternalPlayer(key);
        });
        __publicField(this, "seekTo", (fraction, type, keepPlaying) => {
          if (!this.player)
            return null;
          this.player.seekTo(fraction, type, keepPlaying);
        });
        __publicField(this, "handleReady", () => {
          this.props.onReady(this);
        });
        __publicField(this, "getActivePlayer", (0, import_memoize_one.default)((url) => {
          for (const player of [...customPlayers, ...players]) {
            if (player.canPlay(url)) {
              return player;
            }
          }
          if (fallback) {
            return fallback;
          }
          return null;
        }));
        __publicField(this, "getConfig", (0, import_memoize_one.default)((url, key) => {
          const { config } = this.props;
          return import_deepmerge.default.all([
            import_props2.defaultProps.config,
            import_props2.defaultProps.config[key] || {},
            config,
            config[key] || {}
          ]);
        }));
        __publicField(this, "getAttributes", (0, import_memoize_one.default)((url) => {
          return (0, import_utils2.omit)(this.props, SUPPORTED_PROPS);
        }));
        __publicField(this, "renderActivePlayer", (url) => {
          if (!url)
            return null;
          const player = this.getActivePlayer(url);
          if (!player)
            return null;
          const config = this.getConfig(url, player.key);
          return /* @__PURE__ */ import_react2.default.createElement(
            import_Player2.default,
            {
              ...this.props,
              key: player.key,
              ref: this.references.player,
              config,
              activePlayer: player.lazyPlayer || player,
              onReady: this.handleReady
            }
          );
        });
      }
      shouldComponentUpdate(nextProps, nextState) {
        return !(0, import_react_fast_compare2.default)(this.props, nextProps) || !(0, import_react_fast_compare2.default)(this.state, nextState);
      }
      componentDidUpdate(prevProps) {
        const { light } = this.props;
        if (!prevProps.light && light) {
          this.setState({ showPreview: true });
        }
        if (prevProps.light && !light) {
          this.setState({ showPreview: false });
        }
      }
      renderPreview(url) {
        if (!url)
          return null;
        const { light, playIcon, previewTabIndex, oEmbedUrl, previewAriaLabel } = this.props;
        return /* @__PURE__ */ import_react2.default.createElement(
          Preview,
          {
            url,
            light,
            playIcon,
            previewTabIndex,
            previewAriaLabel,
            oEmbedUrl,
            onClick: this.handleClickPreview
          }
        );
      }
      render() {
        const { url, style, width, height, fallback: fallback2, wrapper: Wrapper } = this.props;
        const { showPreview } = this.state;
        const attributes = this.getAttributes(url);
        const wrapperRef = typeof Wrapper === "string" ? this.references.wrapper : void 0;
        return /* @__PURE__ */ import_react2.default.createElement(Wrapper, { ref: wrapperRef, style: { ...style, width, height }, ...attributes }, /* @__PURE__ */ import_react2.default.createElement(UniversalSuspense, { fallback: fallback2 }, showPreview ? this.renderPreview(url) : this.renderActivePlayer(url)));
      }
    }, __publicField(_a, "displayName", "ReactPlayer"), __publicField(_a, "propTypes", import_props2.propTypes), __publicField(_a, "defaultProps", import_props2.defaultProps), __publicField(_a, "addCustomPlayer", (player) => {
      customPlayers.push(player);
    }), __publicField(_a, "removeCustomPlayers", () => {
      customPlayers.length = 0;
    }), __publicField(_a, "canPlay", (url) => {
      for (const Player2 of [...customPlayers, ...players]) {
        if (Player2.canPlay(url)) {
          return true;
        }
      }
      return false;
    }), __publicField(_a, "canEnablePIP", (url) => {
      for (const Player2 of [...customPlayers, ...players]) {
        if (Player2.canEnablePIP && Player2.canEnablePIP(url)) {
          return true;
        }
      }
      return false;
    }), _a;
  };
  return ReactPlayer;
}
export {
  requireReactPlayer as __require
};
//# sourceMappingURL=ReactPlayer.js.map
