import { jsx as _jsx } from "react/jsx-runtime";
import React, { forwardRef, useImperativeHandle, useRef } from 'react';
import { useDrag } from '@use-gesture/react';
import { useSpring, animated } from '@react-spring/web';
import { Slide } from './slide';
import { bound } from '../utils/bound';
import { unitToPx } from '../utils';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import clsx from 'clsx';
export const Slides = forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = React.useContext(ConfigProviderContext);
  const [bem] = createNamespace('image-preview', prefixCls);
  const slideWidth = window.innerWidth + unitToPx(16);
  const [{
    x
  }, api] = useSpring(() => ({
    x: props.defaultIndex * slideWidth,
    config: {
      tension: 250,
      clamp: true
    }
  }));
  const count = props.images.length;

  function swipeTo(index, immediate = false) {
    var _a;

    const i = bound(index, 0, count - 1);
    (_a = props.onIndexChange) === null || _a === void 0 ? void 0 : _a.call(props, i);
    api.start({
      x: i * slideWidth,
      immediate
    });
  }

  useImperativeHandle(ref, () => ({
    swipeTo
  }));
  const dragLockRef = useRef(false);
  const bind = useDrag(state => {
    if (dragLockRef.current) return;
    const [offsetX] = state.offset;

    if (state.last) {
      const minIndex = Math.floor(offsetX / slideWidth);
      const maxIndex = minIndex + 1;
      const velocityOffset = Math.min(state.velocity[0] * 2000, slideWidth) * state.direction[0];
      swipeTo(bound(Math.round((offsetX + velocityOffset) / slideWidth), minIndex, maxIndex));
    } else {
      api.start({
        x: offsetX,
        immediate: true
      });
    }
  }, {
    transform: ([x, y]) => [-x, y],
    from: () => [x.get(), 0],
    bounds: () => {
      return {
        left: 0,
        right: (count - 1) * slideWidth
      };
    },
    rubberband: true,
    axis: 'x',
    pointer: {
      touch: true
    }
  });
  return _jsx("div", Object.assign({
    className: clsx(bem('slides'))
  }, bind(), {
    children: _jsx(animated.div, Object.assign({
      className: clsx(bem('slides-inner')),
      style: {
        x: x.to(x => -x)
      }
    }, {
      children: props.images.map(image => _jsx(Slide, {
        image: image,
        onTap: props.onTap,
        maxZoom: props.maxZoom,
        onZoomChange: zoom => {
          if (zoom !== 1) {
            const index = Math.round(x.get() / slideWidth);
            api.start({
              x: index * slideWidth
            });
          }
        },
        dragLockRef: dragLockRef
      }, image))
    }))
  }));
});