function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
import React, { useRef, useMemo, useEffect, useContext } from "react";
import classnames from "classnames";
import { isDef, addUnit } from "../utils";
import Icon from "../icon";
import { useSetState, useUpdateEffect } from "../hooks";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const Image = (props) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("image", prefixCls);
  const [status, setStatus] = useSetState({
    loading: true,
    error: false
  });
  const imgRef = useRef(null);
  const unmountedRef = useRef(false);
  const {
    fit,
    errorIcon,
    loadingIcon,
    showError,
    showLoading,
    block
  } = props;
  const style = useMemo(() => {
    const internalStyle = {};
    if (isDef(props.width)) {
      internalStyle.width = addUnit(props.width);
    }
    if (isDef(props.height)) {
      internalStyle.height = addUnit(props.height);
    }
    if (isDef(props.radius)) {
      internalStyle.overflow = "hidden";
      internalStyle.borderRadius = addUnit(props.radius);
    }
    return internalStyle;
  }, []);
  useEffect(() => {
    if (imgRef.current && imgRef.current.complete) {
      setStatus({
        loading: false,
        error: false
      });
    }
    return () => {
      unmountedRef.current = true;
    };
  }, []);
  useUpdateEffect(() => {
    if (imgRef.current && !imgRef.current.complete) {
      setStatus({
        error: false,
        loading: true
      });
    }
  }, [props.src]);
  const onLoad = (e) => {
    var _a;
    if (!unmountedRef.current) {
      setStatus({
        loading: false
      });
      (_a = props.onLoad) == null ? void 0 : _a.call(props, e);
    }
  };
  const onError = (e) => {
    var _a;
    if (!unmountedRef.current) {
      setStatus({
        loading: false,
        error: true
      });
      (_a = props.onError) == null ? void 0 : _a.call(props, e);
    }
  };
  const renderLoadingIcon = () => {
    if (typeof loadingIcon !== "string")
      return loadingIcon;
    return /* @__PURE__ */ React.createElement(Icon, {
      size: props.iconSize,
      name: loadingIcon,
      className: classnames(bem("loading-icon")),
      classPrefix: props.iconPrefix
    });
  };
  const renderErrorIcon = () => {
    if (typeof errorIcon !== "string")
      return errorIcon;
    return /* @__PURE__ */ React.createElement(Icon, {
      size: props.iconSize,
      name: errorIcon,
      className: classnames(bem("error-icon")),
      classPrefix: props.iconPrefix
    });
  };
  const renderPlaceholder = () => {
    if (status.loading && showLoading) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("loading")),
        onClick: props.onClick
      }, renderLoadingIcon());
    }
    if (status.error && showError) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("error")),
        onClick: props.onClick
      }, renderErrorIcon());
    }
    return null;
  };
  const renderImage = () => {
    if (status.error || !props.src) {
      return null;
    }
    const attrs = {
      className: classnames(bem("img")),
      style: {
        objectFit: fit
      }
    };
    return /* @__PURE__ */ React.createElement("img", _extends({
      ref: imgRef,
      alt: props.alt || "img",
      src: props.src,
      onLoad,
      onError
    }, attrs));
  };
  return /* @__PURE__ */ React.createElement("div", {
    className: classnames(props.className, bem({
      block,
      round: props.round
    })),
    style,
    onClick: props.onClick
  }, renderImage(), renderPlaceholder(), props.children);
};
Image.defaultProps = {
  fit: "fill",
  errorIcon: "photo-fail",
  loadingIcon: "photo",
  showError: true,
  showLoading: true,
  block: true
};
var stdin_default = Image;
export {
  stdin_default as default
};
