"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Image = void 0;
var _react = _interopRequireWildcard(require("react"));
var _stagedComponents = require("staged-components");
var _nativeProps = require("../../utils/native-props");
var _toCssLength = require("../../utils/to-css-length");
var _useIsomorphicUpdateLayoutEffect = require("../../utils/use-isomorphic-update-layout-effect");
var _withDefaultProps = require("../../utils/with-default-props");
var _brokenImageIcon = require("./broken-image-icon");
var _imageIcon = require("./image-icon");
var _lazyDetector = require("./lazy-detector");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-image`;
const defaultProps = {
  fit: 'fill',
  placeholder: _react.default.createElement("div", {
    className: `${classPrefix}-tip`
  }, _react.default.createElement(_imageIcon.ImageIcon, null)),
  fallback: _react.default.createElement("div", {
    className: `${classPrefix}-tip`
  }, _react.default.createElement(_brokenImageIcon.BrokenImageIcon, null)),
  lazy: false,
  draggable: false
};
const Image = exports.Image = (0, _stagedComponents.staged)(p => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const [loaded, setLoaded] = (0, _react.useState)(false);
  const [failed, setFailed] = (0, _react.useState)(false);
  const ref = (0, _react.useRef)(null);
  const imgRef = (0, _react.useRef)(null);
  let src = props.src;
  let srcSet = props.srcSet;
  const [initialized, setInitialized] = (0, _react.useState)(!props.lazy);
  src = initialized ? props.src : undefined;
  srcSet = initialized ? props.srcSet : undefined;
  (0, _useIsomorphicUpdateLayoutEffect.useIsomorphicUpdateLayoutEffect)(() => {
    setLoaded(false);
    setFailed(false);
  }, [src]);
  (0, _react.useEffect)(() => {
    var _a;
    // for nextjs ssr
    if ((_a = imgRef.current) === null || _a === void 0 ? void 0 : _a.complete) {
      setLoaded(true);
    }
  }, []);
  function renderInner() {
    if (failed || src === undefined && !srcSet) {
      return _react.default.createElement(_react.default.Fragment, null, props.fallback);
    }
    const img = _react.default.createElement("img", {
      ref: imgRef,
      id: props.id,
      className: `${classPrefix}-img`,
      src: src,
      alt: props.alt,
      onClick: props.onClick,
      onLoad: e => {
        var _a;
        setLoaded(true);
        (_a = props.onLoad) === null || _a === void 0 ? void 0 : _a.call(props, e);
      },
      onError: e => {
        var _a;
        setFailed(true);
        (_a = props.onError) === null || _a === void 0 ? void 0 : _a.call(props, e);
      },
      style: {
        objectFit: props.fit,
        display: loaded ? 'block' : 'none'
      },
      crossOrigin: props.crossOrigin,
      decoding: props.decoding,
      loading: props.loading,
      referrerPolicy: props.referrerPolicy,
      sizes: props.sizes,
      srcSet: srcSet,
      useMap: props.useMap,
      draggable: props.draggable
    });
    return _react.default.createElement(_react.default.Fragment, null, !loaded && props.placeholder, img);
  }
  const style = {};
  if (props.width) {
    style['--width'] = (0, _toCssLength.toCSSLength)(props.width);
    style['width'] = (0, _toCssLength.toCSSLength)(props.width);
  }
  if (props.height) {
    style['--height'] = (0, _toCssLength.toCSSLength)(props.height);
    style['height'] = (0, _toCssLength.toCSSLength)(props.height);
  }
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    ref: ref,
    className: classPrefix,
    style: style,
    onClick: props.onContainerClick
  }, props.lazy && !initialized && _react.default.createElement(_lazyDetector.LazyDetector, {
    onActive: () => {
      setInitialized(true);
    }
  }), renderInner()));
});