function _extends() { _extends = Object.assign || 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 clsx from 'clsx';
import { isDef, addUnit } from '../utils';
import { useSetState } from '../hooks';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var Image = function Image(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('image', prefixCls),
      bem = _createNamespace[0];

  var _useSetState = useSetState({
    loading: true,
    error: false
  }),
      status = _useSetState[0],
      setStatus = _useSetState[1];

  var imgRef = useRef(null);
  var fit = props.fit,
      errorIcon = props.errorIcon,
      loadingIcon = props.loadingIcon,
      showError = props.showError,
      showLoading = props.showLoading,
      block = props.block;
  var style = useMemo(function () {
    var internalStyle = _extends({}, props.style);

    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;
  }, [props.style]);
  useEffect(function () {
    var payload = {
      error: false,
      loading: true
    };

    if (imgRef.current) {
      if (imgRef.current.complete) {
        payload.loading = false;
      } else {
        payload.loading = true;
      }
    }

    setStatus(payload);
  }, [props.src]);

  var onLoad = function onLoad(e) {
    setStatus({
      loading: false
    });
    props.onLoad == null ? void 0 : props.onLoad(e);
  };

  var onError = function onError(e) {
    setStatus({
      error: true,
      loading: false
    });
    props.onLoad == null ? void 0 : props.onLoad(e);
  };

  var renderLoadingIcon = function renderLoadingIcon() {
    if (loadingIcon) return /*#__PURE__*/React.cloneElement(loadingIcon, {
      className: clsx(bem('loading-icon')),
      fontSize: props.iconSize
    });
    return null;
  };

  var renderErrorIcon = function renderErrorIcon() {
    if (errorIcon) {
      return /*#__PURE__*/React.cloneElement(errorIcon, {
        className: clsx(bem('error-icon')),
        fontSize: props.iconSize
      });
    }

    return null;
  };

  var renderPlaceholder = function renderPlaceholder() {
    if (status.loading && showLoading) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('loading')),
        onClick: props.onClick
      }, renderLoadingIcon());
    }

    if (status.error && showError) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('error')),
        onClick: props.onClick
      }, renderErrorIcon());
    }

    return null;
  };

  var renderImage = function renderImage() {
    if (status.error || !props.src) {
      return null;
    }

    var attrs = {
      className: clsx(bem('img')),
      style: {
        objectFit: fit
      }
    };
    return /*#__PURE__*/React.createElement("img", _extends({
      ref: imgRef,
      alt: props.alt || 'img',
      src: props.src,
      onLoad: onLoad,
      onError: onError
    }, attrs));
  };

  return /*#__PURE__*/React.createElement("div", {
    className: clsx(props.className, bem({
      block: block,
      round: props.round
    })),
    style: style,
    onClick: props.onClick
  }, renderImage(), renderPlaceholder(), props.children);
};

export default Image;