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 Popup from '../popup';
import { Slides } from './slides';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import SwiperPagIndicator from '../swiper/SwiperPagIndicator';
const ImagePreview = React.forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('image-preview', prefixCls);
  const slidesRef = useRef(null);
  const [active, setActive] = useState(() => props.startPosition);
  const currentImage = React.useMemo(() => props.images[active], [active, props.images]);

  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 = () => {
    var _a;

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

  const renderContent = () => _jsx("div", Object.assign({
    className: cls(bem('content'))
  }, {
    children: props.images && _jsx(Slides, {
      ref: slidesRef,
      defaultIndex: active,
      onIndexChange: onSwipeChange,
      images: props.images,
      onTap: () => {
        if (!props.closeOnlyClickCloseIcon) {
          onClose();
        }
      },
      maxZoom: props.maxZoom
    })
  }));

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

  const renderIndicator = () => {
    if (props.showIndicators) {
      return _jsx("div", Object.assign({
        className: cls(bem('indicator'))
      }, {
        children: _jsx(SwiperPagIndicator, {
          total: props.images.length,
          current: active
        })
      }));
    }

    return null;
  };

  React.useImperativeHandle(ref, () => ({
    swipeTo: (index, immediate) => {
      var _a;

      setActive(index);
      (_a = slidesRef.current) === null || _a === void 0 ? void 0 : _a.swipeTo(index, immediate);
    }
  }));
  return _jsxs(Popup, Object.assign({
    className: cls(bem(), props.className),
    overlayClass: cls(bem('overlay'))
  }, pick(props, ['visible', 'overlayStyle', 'closeOnPopstate', 'onClosed', 'beforeClose', 'teleport']), {
    children: [renderClose(), renderContent(), renderIndex(), renderIndicator()]
  }));
});
ImagePreview.defaultProps = {
  loop: true,
  overlay: true,
  showIndex: true,
  images: [],
  swipeDuration: 300,
  startPosition: 0,
  closeIconPosition: 'top-right',
  showIndicators: false,
  closeOnlyClickCloseIcon: false,
  maxZoom: 3
};
export default ImagePreview;