import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
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';

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 {
    fit,
    errorIcon,
    loadingIcon,
    showError,
    showLoading,
    block
  } = props;
  const style = useMemo(() => {
    const internalStyle = Object.assign({}, 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(() => {
    const payload = {
      error: false,
      loading: true
    };

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

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

  const onLoad = e => {
    var _a;

    setStatus({
      loading: false
    });
    (_a = props.onLoad) === null || _a === void 0 ? void 0 : _a.call(props, e);
  };

  const onError = e => {
    var _a;

    setStatus({
      error: true,
      loading: false
    });
    (_a = props.onLoad) === null || _a === void 0 ? void 0 : _a.call(props, e);
  };

  const renderLoadingIcon = () => {
    if (loadingIcon) return React.cloneElement(loadingIcon, {
      className: clsx(bem('loading-icon')),
      fontSize: props.iconSize
    });
    return null;
  };

  const renderErrorIcon = () => {
    if (errorIcon) {
      return React.cloneElement(errorIcon, {
        className: clsx(bem('error-icon')),
        fontSize: props.iconSize
      });
    }

    return null;
  };

  const renderPlaceholder = () => {
    if (status.loading && showLoading) {
      return _jsx("div", Object.assign({
        className: clsx(bem('loading')),
        onClick: props.onClick
      }, {
        children: renderLoadingIcon()
      }));
    }

    if (status.error && showError) {
      return _jsx("div", Object.assign({
        className: clsx(bem('error')),
        onClick: props.onClick
      }, {
        children: renderErrorIcon()
      }));
    }

    return null;
  };

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

    const attrs = {
      className: clsx(bem('img')),
      style: {
        objectFit: fit
      }
    };
    return _jsx("img", Object.assign({
      ref: imgRef,
      alt: props.alt || 'img',
      src: props.src,
      onLoad: onLoad,
      onError: onError
    }, attrs));
  };

  return _jsxs("div", Object.assign({
    className: clsx(props.className, bem({
      block,
      round: props.round
    })),
    style: style,
    onClick: props.onClick
  }, {
    children: [renderImage(), renderPlaceholder(), props.children]
  }));
};

export default Image;