import React, { useEffect, useRef, useState } from 'react';
import { jsonProxy2, revokeJsonProxy } from "../..\\utils\\constants";
import { jsx as _jsx } from "react/jsx-runtime";
const Audio = ({
  media,
  autoPlay,
  getMedia
}) => {
  const [hasChangedProxy, setHasChangedProxy] = useState(false);
  const [hasRemovedProxy, setHasRemovedProxy] = useState(false);
  const audio = useRef(null);
  useEffect(() => {
    getMedia === null || getMedia === void 0 ? void 0 : getMedia(audio.current);
  }, [getMedia]);
  return /*#__PURE__*/_jsx("div", {
    className: "audioContainer",
    children: /*#__PURE__*/_jsx("audio", {
      className: "audio",
      ref: audio,
      controls: true,
      loop: true,
      src: media,
      autoPlay: autoPlay,
      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 Audio;