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, { useContext, useRef, useState } from 'react';
import cls from 'clsx';
import { pick } from '../utils';
import Swiper from '../swiper';
import Popup from '../popup';
import ImagePreviewItem from './ImagePreviewItem';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

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

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

  var swiperRef = useRef(null);

  var _useState = useState(function () {
    return props.startPosition;
  }),
      active = _useState[0],
      setActive = _useState[1];

  var onSwipeChange = function onSwipeChange(idx) {
    if (active !== idx) {
      setActive(idx);
      props.onChange == null ? void 0 : props.onChange(idx);
    }
  };

  var onClose = function onClose(image, i) {
    props.onClose && (props.onClose == null ? void 0 : props.onClose({
      url: image,
      index: i
    }));
  };

  var renderImages = function renderImages() {
    return /*#__PURE__*/React.createElement(Swiper, {
      ref: swiperRef,
      className: cls(bem('swipe')),
      loop: props.loop,
      duration: props.swipeDuration,
      initialSwipe: active,
      onChange: onSwipeChange,
      indicator: props.showIndicators
    }, props.images.map(function (image, i) {
      return /*#__PURE__*/React.createElement(Swiper.Item, {
        className: cls(bem('item')),
        key: image,
        onClick: function onClick() {
          onClose(image, i);
        }
      }, /*#__PURE__*/React.createElement(ImagePreviewItem, {
        maxZoom: props.maxZoom,
        image: image,
        lazyload: props.lazyload,
        onTap: function onTap() {
          onClose(image, i);
        },
        onZoomChange: function onZoomChange(zoom) {
          if (zoom !== 1) {
            var _swiperRef$current;

            (_swiperRef$current = swiperRef.current) == null ? void 0 : _swiperRef$current.disable();
          } else {
            var _swiperRef$current2;

            (_swiperRef$current2 = swiperRef.current) == null ? void 0 : _swiperRef$current2.enable();
          }
        }
      }));
    }));
  };

  var renderClose = function renderClose() {
    if (props.closeable) {
      return /*#__PURE__*/React.cloneElement(props.closeIcon, {
        className: cls(bem('close-icon', props.closeIconPosition)),
        onClick: props.onClose
      });
    }

    return null;
  };

  var renderIndex = function renderIndex() {
    if (props.showIndex) {
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('index'))
      }, props.indexRender ? props.indexRender({
        index: active,
        len: props.images.length
      }) : active + 1 + " / " + props.images.length);
    }

    return null;
  };

  return /*#__PURE__*/React.createElement(Popup, _extends({
    className: cls(bem(), props.className),
    overlayClass: cls(bem('overlay')),
    beforeClose: props.beforeClose
  }, pick(props, ['visible', 'overlayStyle', 'closeOnPopstate', 'onClose', 'onClosed'])), renderClose(), renderImages(), renderIndex());
};

ImagePreview.defaultProps = {
  loop: true,
  overlay: true,
  showIndex: true,
  images: [],
  swipeDuration: 300,
  startPosition: 0,
  closeIconPosition: 'top-right',
  showIndicators: false,
  maxZoom: 3
};
export default ImagePreview;