import React, { useEffect, useRef, useState } from "react";
import { ReactPlayer } from "./../wix-react-player/index.js";
import { isSSR } from "./../wix-rich-content-common/index.js";
import { pauseTrigger } from "./constants.js";
import { REACT_PLAYER_FILE } from "./data-hooks.js";
const VIMEO_SDK_URL = "https://player.vimeo.com/api/player.js";
const VIMEO_GLOBAL = "Vimeo";
const ReactPlayerWrapper = ({
  styles,
  disabled,
  onReady,
  onDuration,
  onError,
  onProgress,
  url,
  width,
  height,
  progressInterval,
  config,
  muted,
  isLoaded,
  controls,
  className,
  onContextMenu,
  loop,
  isCustomVideo,
  autoPlay,
  ...rest
}) => {
  const playerRef = useRef(null);
  const playerWrapper = useRef(null);
  const [vimeoLoaded, setVimeoLoaded] = useState(false);
  const [isPlaying, setIsPlaying] = useState(false);
  const [isAutoPlayPaused, setIsAutoPlayPaused] = useState(false);
  const isVimeoAndRequireJS = () => {
    const isVimeo = !isSSR() && isVimeoUrl(url) && !window[VIMEO_GLOBAL];
    return isVimeo && window.requirejs;
  };
  useEffect(() => {
    if (isVimeoAndRequireJS()) {
      window.require([VIMEO_SDK_URL], (player) => {
        window.Vimeo = { Player: player };
        setVimeoLoaded(true);
      });
    }
    const autoPlayCleanup = autoPlay ? handleAutoPlay() : void 0;
    return () => {
      autoPlayCleanup?.();
    };
  }, []);
  const handleAutoPlay = () => {
    if (!window?.IntersectionObserver || !playerRef.current || !playerWrapper.current) {
      return;
    }
    const intersectionObserver = new IntersectionObserver((entries) => {
      entries.forEach((entry) => {
        const startAutoPlay = entry.isIntersecting && !disabled && !isAutoPlayPaused;
        if (startAutoPlay) {
          if (rest.isLight && playerRef.current?.state.showPreview) {
            playerRef.current.handleClickPreview();
          }
          handlePlay();
        } else {
          handlePause(pauseTrigger.scroll);
        }
      });
    });
    intersectionObserver.observe(playerWrapper.current);
    return () => {
      intersectionObserver.disconnect();
    };
  };
  const handlePlay = () => {
    if (!isPlaying) {
      setIsPlaying(true);
    }
  };
  const handlePause = (trigger) => {
    if (isPlaying) {
      const isAutoPlayPaused2 = trigger !== pauseTrigger.scroll;
      setIsPlaying(false);
      setIsAutoPlayPaused(isAutoPlayPaused2);
    }
  };
  const onWrapperClick = () => {
    if (isCustomVideo && !controls) {
      isPlaying ? handlePause() : handlePlay();
    }
  };
  if (!vimeoLoaded && isVimeoAndRequireJS()) {
    return null;
  }
  const lightWeightMode = rest.isLight && rest.thumbnail;
  return /* @__PURE__ */ React.createElement("div", { ref: playerWrapper }, /* @__PURE__ */ React.createElement(
    ReactPlayer,
    {
      previewAriaLabel: "Play video",
      playing: isPlaying && !disabled,
      onPlay: handlePlay,
      onPause: handlePause,
      onError,
      controls,
      style: styles,
      light: lightWeightMode,
      onReady: () => {
        const skipAutoPlay = isTouchDevice() && isVimeoUrl(url);
        if (lightWeightMode && !skipAutoPlay) {
          handlePlay();
        }
        onReady?.(playerRef);
      },
      url,
      width,
      height,
      onDuration,
      onProgress,
      progressInterval,
      muted,
      "data-loaded": isLoaded,
      className,
      onContextMenu,
      ref: playerRef,
      loop,
      config: {
        ...config,
        file: {
          ...config?.file,
          // oxlint-disable-next-line no-new-object
          attributes: new Object({
            ...config?.file?.attributes,
            "data-hook": REACT_PLAYER_FILE,
            onClick: onWrapperClick,
            poster: rest.poster,
            onKeyDown: (e) => {
              if (e.code === "Enter" || e.code === "Space") {
                onWrapperClick();
              }
            }
          })
        }
      }
    }
  ));
};
function isTouchDevice() {
  if (isSSR()) {
    return false;
  }
  return "ontouchstart" in window || navigator.maxTouchPoints > 0;
}
function isVimeoUrl(url) {
  const MATCH_URL = /vimeo\.com\/.+/;
  const MATCH_FILE_URL = /vimeo\.com\/external\/[0-9]+\..+/;
  return !MATCH_FILE_URL.test(url) && MATCH_URL.test(url);
}
export {
  ReactPlayerWrapper
};
//# sourceMappingURL=react-player-wrapper.js.map
