"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Loading = _interopRequireDefault(require("../Loading"));
var _ImageWrap = require("./ImageWrap");
var _constants = require("../..\\utils\\constants");
var _compress = _interopRequireDefault(require("../..\\utils\\compress"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const Image = ({
  media,
  cssStyle: _cssStyle = '',
  imgCssStyle: _imgCssStyle = '',
  mediaMimeType,
  needCompress,
  onLoad,
  isDetail,
  height: _height = 'auto',
  width: _width = 300
}) => {
  const [hasChangedProxy, setHasChangedProxy] = (0, _react.useState)(false);
  const [hasRemovedProxy, setHasRemovedProxy] = (0, _react.useState)(false);
  const [isLoaded, setIsLoaded] = (0, _react.useState)(false);
  const isGif = mediaMimeType === null || mediaMimeType === void 0 ? void 0 : mediaMimeType.includes('image/gif');
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ImageWrap.ImageWrap, {
    cssStyle: _cssStyle,
    imgCssStyle: _imgCssStyle,
    height: _height,
    width: _width,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "imageContainer",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Loading.default, {
        cssStyle: `
          display: ${isDetail && isLoaded && 'none !important'};
          ${_cssStyle}
        `
      }), /*#__PURE__*/(0, _jsxRuntime.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;
          }
          (0, _compress.default)(el.currentTarget.src, el.currentTarget, undefined, onLoad, setIsLoaded);
        },
        onError: el => {
          if (!hasChangedProxy) {
            const newImage = (0, _constants.jsonProxy2)((0, _constants.revokeJsonProxy)(media));
            setHasChangedProxy(true);
            if (isGif || !needCompress) {
              el.currentTarget.src = newImage;
              return;
            }
            (0, _compress.default)(newImage, el.currentTarget, true);
          } else if (!hasRemovedProxy) {
            const newImage = (0, _constants.revokeJsonProxy2)(media);
            setHasRemovedProxy(true);
            if (isGif || !needCompress) {
              el.currentTarget.src = newImage;
              return;
            }
            (0, _compress.default)(newImage, el.currentTarget, true);
          }
        }
      })]
    })
  });
};
var _default = Image;
exports.default = _default;