import React, { useState } from 'react';
import Loading from "../Loading";
import { ImageWrap } from "./ImageWrap";
import { jsonProxy2, revokeJsonProxy, revokeJsonProxy2 } from "../..\\utils\\constants";
import compress from "../..\\utils\\compress";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const Image = ({
  media,
  cssStyle: _cssStyle = '',
  imgCssStyle: _imgCssStyle = '',
  mediaMimeType,
  needCompress,
  onLoad,
  isDetail,
  height: _height = 'auto',
  width: _width = 300
}) => {
  const [hasChangedProxy, setHasChangedProxy] = useState(false);
  const [hasRemovedProxy, setHasRemovedProxy] = useState(false);
  const [isLoaded, setIsLoaded] = useState(false);
  const isGif = mediaMimeType === null || mediaMimeType === void 0 ? void 0 : mediaMimeType.includes('image/gif');
  return /*#__PURE__*/_jsx(ImageWrap, {
    cssStyle: _cssStyle,
    imgCssStyle: _imgCssStyle,
    height: _height,
    width: _width,
    children: /*#__PURE__*/_jsxs("div", {
      className: "imageContainer",
      children: [/*#__PURE__*/_jsx(Loading, {
        cssStyle: `
          display: ${isDetail && isLoaded && 'none !important'};
          ${_cssStyle}
        `
      }), /*#__PURE__*/_jsx("img", {
        className: "image",
        src: media,
        onLoad: el => {
          if (isGif || !needCompress || media.includes('https://images.mirror-media.xyz/')) {
            el.target.style.visibility = 'visible';
            onLoad === null || onLoad === void 0 ? void 0 : onLoad();
            setIsLoaded === null || setIsLoaded === void 0 ? void 0 : setIsLoaded(true);
            return;
          }
          compress(el.currentTarget.src, el.currentTarget, undefined, onLoad, setIsLoaded);
        },
        onError: el => {
          if (!hasChangedProxy) {
            const newImage = jsonProxy2(revokeJsonProxy(media));
            setHasChangedProxy(true);
            if (isGif || !needCompress) {
              el.currentTarget.src = newImage;
              return;
            }
            compress(newImage, el.currentTarget, true);
          } else if (!hasRemovedProxy) {
            const newImage = revokeJsonProxy2(media);
            setHasRemovedProxy(true);
            if (isGif || !needCompress) {
              el.currentTarget.src = newImage;
              return;
            }
            compress(newImage, el.currentTarget, true);
          }
        }
      })]
    })
  });
};
export default Image;