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

const ImagePreview = props => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('image-preview', prefixCls);
  const swiperRef = useRef(null);
  const [active, setActive] = useState(() => props.startPosition);

  const onSwipeChange = idx => {
    var _a;

    if (active !== idx) {
      setActive(idx);
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, idx);
    }
  };

  const onClose = (image, i) => {
    var _a;

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

  const renderImages = () => _jsx(Swiper, Object.assign({
    ref: swiperRef,
    className: cls(bem('swipe')),
    loop: props.loop,
    duration: props.swipeDuration,
    initialSwipe: active,
    onChange: onSwipeChange,
    indicator: props.showIndicators
  }, {
    children: props.images.map((image, i) => _jsx(Swiper.Item, Object.assign({
      className: cls(bem('item')),
      onClick: () => {
        onClose(image, i);
      }
    }, {
      children: _jsx(ImagePreviewItem, {
        maxZoom: props.maxZoom,
        image: image,
        lazyload: props.lazyload,
        onTap: () => {
          onClose(image, i);
        },
        onZoomChange: zoom => {
          var _a, _b;

          if (zoom !== 1) {
            (_a = swiperRef.current) === null || _a === void 0 ? void 0 : _a.disable();
          } else {
            (_b = swiperRef.current) === null || _b === void 0 ? void 0 : _b.enable();
          }
        }
      })
    }), image))
  }));

  const renderClose = () => {
    if (props.closeable) {
      return React.cloneElement(props.closeIcon, {
        className: cls(bem('close-icon', props.closeIconPosition)),
        onClick: props.onClose
      });
    }

    return null;
  };

  const renderIndex = () => {
    if (props.showIndex) {
      return _jsx("div", Object.assign({
        className: cls(bem('index'))
      }, {
        children: props.indexRender ? props.indexRender({
          index: active,
          len: props.images.length
        }) : `${active + 1} / ${props.images.length}`
      }));
    }

    return null;
  };

  return _jsxs(Popup, Object.assign({
    className: cls(bem(), props.className),
    overlayClass: cls(bem('overlay')),
    beforeClose: props.beforeClose
  }, pick(props, ['visible', 'overlayStyle', 'closeOnPopstate', 'onClose', 'onClosed']), {
    children: [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;