import React, { useEffect, useRef, useState } from 'react';
import { VideoWrap } from "./VideoWrap";
import { jsonProxy2, revokeJsonProxy } from "../..\\utils\\constants";
import { jsx as _jsx } from "react/jsx-runtime";
const Video = ({
  media,
  autoPlay,
  mediaVisible,
  getMedia,
  cssStyle: _cssStyle = ''
}) => {
  const [hasChangedProxy, setHasChangedProxy] = useState(false);
  const [hasRemovedProxy, setHasRemovedProxy] = useState(false);
  const video = useRef(null);
  useEffect(() => {
    getMedia === null || getMedia === void 0 ? void 0 : getMedia(video.current);
  }, [autoPlay, getMedia]);
  return /*#__PURE__*/_jsx(VideoWrap, {
    cssStyle: _cssStyle,
    children: /*#__PURE__*/_jsx("video", {
      src: media,
      ref: video,
      autoPlay: autoPlay,
      controls: Boolean(mediaVisible),
      className: "video",
      loop: true,
      playsInline: true,
      onError: el => {
        if (!hasRemovedProxy) {
          el.currentTarget.src = revokeJsonProxy(el.currentTarget.src);
          const el2 = el.currentTarget;
          const pNode = el.currentTarget.parentNode;
          el.currentTarget.remove();
          pNode === null || pNode === void 0 ? void 0 : pNode.append(el2);
          setHasRemovedProxy(true);
        } else if (!hasChangedProxy) {
          el.currentTarget.src = jsonProxy2(el.currentTarget.src);
          const el2 = el.currentTarget;
          const pNode = el.currentTarget.parentNode;
          el.currentTarget.remove();
          pNode === null || pNode === void 0 ? void 0 : pNode.append(el2);
          setHasChangedProxy(true);
        }
      },
      children: /*#__PURE__*/_jsx("source", {
        src: media,
        onError: el => {
          if (!hasRemovedProxy) {
            el.currentTarget.src = revokeJsonProxy(el.currentTarget.src);
            const el2 = el.currentTarget;
            const pNode = el.currentTarget.parentNode;
            el.currentTarget.remove();
            pNode === null || pNode === void 0 ? void 0 : pNode.append(el2);
            setHasRemovedProxy(true);
          } else if (!hasChangedProxy) {
            el.currentTarget.src = jsonProxy2(el.currentTarget.src);
            const el2 = el.currentTarget;
            const pNode = el.currentTarget.parentNode;
            el.currentTarget.remove();
            pNode === null || pNode === void 0 ? void 0 : pNode.append(el2);
            setHasChangedProxy(true);
          }
        }
      })
    })
  });
};
export default Video;