var _Object$assign;

var _excluded = ["lazyload"];

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); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { useContext } from 'react';
import clsx from 'clsx';
import { Photo, PhotoFail } from '@react-vant/icons';
import Image from './Image';
import { Lazyload } from '../lazyload';
import { COMPONENT_TYPE_KEY } from '../utils/constant';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
export var getLazyImagePlaceholder = function getLazyImagePlaceholder(bem) {
  return /*#__PURE__*/React.createElement("div", {
    className: clsx(bem('loading'))
  }, /*#__PURE__*/React.createElement(Photo, {
    className: clsx(bem('loading-icon'))
  }));
};

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

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

  var lazyload = props.lazyload,
      imageProps = _objectWithoutPropertiesLoose(props, _excluded);

  var renderPlaceholder = function renderPlaceholder() {
    if (typeof lazyload === 'boolean') return getLazyImagePlaceholder(bem);
    return lazyload.placeholder || getLazyImagePlaceholder(bem);
  };

  if (lazyload) {
    var className = imageProps.className,
        style = imageProps.style,
        height = imageProps.height,
        width = imageProps.width;
    var attrs = {
      className: clsx(className, bem({
        block: imageProps.block
      })),
      style: _extends({}, style, {
        height: height,
        width: width
      })
    };
    return /*#__PURE__*/React.createElement(Lazyload, _extends({}, attrs, {
      placeholder: renderPlaceholder()
    }), /*#__PURE__*/React.createElement(Image, imageProps));
  }

  return /*#__PURE__*/React.createElement(Image, imageProps);
};

LazyImage.defaultProps = {
  fit: 'fill',
  errorIcon: /*#__PURE__*/React.createElement(PhotoFail, null),
  loadingIcon: /*#__PURE__*/React.createElement(Photo, null),
  showError: true,
  showLoading: true,
  block: true
};
export var IMAGE_KEY = Symbol('image');
var ImageNamespace = Object.assign(LazyImage, (_Object$assign = {}, _Object$assign[COMPONENT_TYPE_KEY] = IMAGE_KEY, _Object$assign));
export default ImageNamespace;