import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useEffect, useRef, useState } from 'react';
import { jsonProxy2, revokeJsonProxy } from "../..\\utils\\constants";
import { jsx as _jsx } from "react/jsx-runtime";
var Audio = function Audio(_ref) {
  var media = _ref.media,
    autoPlay = _ref.autoPlay,
    getMedia = _ref.getMedia;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    hasChangedProxy = _useState2[0],
    setHasChangedProxy = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    hasRemovedProxy = _useState4[0],
    setHasRemovedProxy = _useState4[1];
  var audio = useRef(null);
  useEffect(function () {
    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: function onError(el) {
        if (!hasRemovedProxy) {
          el.currentTarget.src = revokeJsonProxy(el.currentTarget.src);
          var el2 = el.currentTarget;
          var 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);
          var _el = el.currentTarget;
          var _pNode = el.currentTarget.parentNode;
          el.currentTarget.remove();
          _pNode === null || _pNode === void 0 ? void 0 : _pNode.append(_el);
          setHasChangedProxy(true);
        }
      }
    })
  });
};
export default Audio;