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 } from 'react';
import cls from 'clsx';
import { useGesture } from '@use-gesture/react';
import { useSpring, animated } from '@react-spring/web';
import Image from '../image';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

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

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

  var _useSpring = useSpring(function () {
    return {
      zoom: 1,
      x: 0,
      y: 0,
      config: {
        tension: 300
      }
    };
  }),
      _useSpring$ = _useSpring[0],
      zoom = _useSpring$.zoom,
      x = _useSpring$.x,
      y = _useSpring$.y,
      api = _useSpring[1];

  var bind = useGesture({
    onDrag: function onDrag(state) {
      if (state.tap && state.elapsedTime > 0) {
        // 判断点击时间>0是为了过滤掉非正常操作，例如用户长按选择图片之后的取消操作（也是一次点击）
        props.onTap();
        return;
      }

      var _state$swipe = state.swipe,
          swipeX = _state$swipe[0],
          swipeY = _state$swipe[1];
      var currentZoom = zoom.get();

      if (currentZoom <= 1 || swipeX !== 0 || swipeY !== 0) {
        api.start({
          x: 0,
          y: 0,
          immediate: true
        });
      } else {
        var _state$offset = state.offset,
            offsetX = _state$offset[0],
            offsetY = _state$offset[1];
        api.start({
          x: offsetX,
          y: offsetY,
          immediate: true
        });
      }
    },
    onPinch: function onPinch(state) {
      var _state$offset2 = state.offset,
          d = _state$offset2[0];
      var currentZoom = state.last ? Math.max(Math.min(d, props.maxZoom), 1) : d;
      api.start({
        zoom: currentZoom,
        immediate: !state.last
      });
      props.onZoomChange == null ? void 0 : props.onZoomChange(currentZoom);

      if (state.last && currentZoom <= 1) {
        api.start({
          x: 0,
          y: 0,
          immediate: true
        });
      }
    }
  }, {
    drag: {
      from: function from() {
        return [x.get(), y.get()];
      }
    },
    pinch: {
      distanceBounds: {
        min: 1,
        max: props.maxZoom
      },
      rubberband: true,
      from: function from() {
        return [zoom.get(), 0];
      }
    }
  });
  return /*#__PURE__*/React.createElement("div", {
    className: cls(bem('slide')),
    onPointerMove: function onPointerMove(e) {
      if (zoom.get() !== 1) {
        e.stopPropagation();
      }
    }
  }, /*#__PURE__*/React.createElement("div", _extends({
    className: cls(bem('control'))
  }, bind()), /*#__PURE__*/React.createElement(animated.div, {
    className: cls(bem('image-wrapper')),
    style: {
      scale: zoom,
      x: x,
      y: y
    }
  }, /*#__PURE__*/React.createElement(Image, {
    fit: "contain",
    src: props.image,
    className: cls(bem('image')),
    lazyload: props.lazyload,
    onClick: function onClick() {
      props.onTap == null ? void 0 : props.onTap();
    }
  }))));
};

export default ImagePreviewItem;