var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_utils = require("../utils");
var import_icon = __toESM(require("../icon"));
var import_hooks = require("../hooks");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
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);
}
const Image = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("image", prefixCls);
  const [status, setStatus] = (0, import_hooks.useSetState)({
    loading: true,
    error: false
  });
  const imgRef = (0, import_react.useRef)(null);
  const unmountedRef = (0, import_react.useRef)(false);
  const {
    fit,
    errorIcon,
    loadingIcon,
    showError,
    showLoading,
    block
  } = props;
  const style = (0, import_react.useMemo)(() => {
    const internalStyle = {};
    if ((0, import_utils.isDef)(props.width)) {
      internalStyle.width = (0, import_utils.addUnit)(props.width);
    }
    if ((0, import_utils.isDef)(props.height)) {
      internalStyle.height = (0, import_utils.addUnit)(props.height);
    }
    if ((0, import_utils.isDef)(props.radius)) {
      internalStyle.overflow = "hidden";
      internalStyle.borderRadius = (0, import_utils.addUnit)(props.radius);
    }
    return internalStyle;
  }, []);
  (0, import_react.useEffect)(() => {
    if (imgRef.current && imgRef.current.complete) {
      setStatus({
        loading: false,
        error: false
      });
    }
    return () => {
      unmountedRef.current = true;
    };
  }, []);
  (0, import_hooks.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__ */ import_react.default.createElement(import_icon.default, {
      size: props.iconSize,
      name: loadingIcon,
      className: (0, import_classnames.default)(bem("loading-icon")),
      classPrefix: props.iconPrefix
    });
  };
  const renderErrorIcon = () => {
    if (typeof errorIcon !== "string")
      return errorIcon;
    return /* @__PURE__ */ import_react.default.createElement(import_icon.default, {
      size: props.iconSize,
      name: errorIcon,
      className: (0, import_classnames.default)(bem("error-icon")),
      classPrefix: props.iconPrefix
    });
  };
  const renderPlaceholder = () => {
    if (status.loading && showLoading) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("loading")),
        onClick: props.onClick
      }, renderLoadingIcon());
    }
    if (status.error && showError) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("error")),
        onClick: props.onClick
      }, renderErrorIcon());
    }
    return null;
  };
  const renderImage = () => {
    if (status.error || !props.src) {
      return null;
    }
    const attrs = {
      className: (0, import_classnames.default)(bem("img")),
      style: {
        objectFit: fit
      }
    };
    return /* @__PURE__ */ import_react.default.createElement("img", _extends({
      ref: imgRef,
      alt: props.alt || "img",
      src: props.src,
      onLoad,
      onError
    }, attrs));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(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;
