"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

function _react2() {
  const data = require("@use-gesture/react");

  _react2 = function () {
    return data;
  };

  return data;
}

function _web() {
  const data = require("@react-spring/web");

  _web = function () {
    return data;
  };

  return data;
}

var _image = _interopRequireDefault(require("../image"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const ImagePreviewItem = props => {
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('image-preview', prefixCls);
  const [{
    zoom,
    x,
    y
  }, api] = (0, _web().useSpring)(() => ({
    zoom: 1,
    x: 0,
    y: 0,
    config: {
      tension: 300
    }
  }));
  const bind = (0, _react2().useGesture)({
    onDrag: state => {
      if (state.tap && state.elapsedTime > 0) {
        // 判断点击时间>0是为了过滤掉非正常操作，例如用户长按选择图片之后的取消操作（也是一次点击）
        props.onTap();
        return;
      }

      const [swipeX, swipeY] = state.swipe;
      const currentZoom = zoom.get();

      if (currentZoom <= 1 || swipeX !== 0 || swipeY !== 0) {
        api.start({
          x: 0,
          y: 0,
          immediate: true
        });
      } else {
        const [offsetX, offsetY] = state.offset;
        api.start({
          x: offsetX,
          y: offsetY,
          immediate: true
        });
      }
    },
    onPinch: state => {
      var _a;

      const [d] = state.offset;
      const currentZoom = state.last ? Math.max(Math.min(d, props.maxZoom), 1) : d;
      api.start({
        zoom: currentZoom,
        immediate: !state.last
      });
      (_a = props.onZoomChange) === null || _a === void 0 ? void 0 : _a.call(props, currentZoom);

      if (state.last && currentZoom <= 1) {
        api.start({
          x: 0,
          y: 0,
          immediate: true
        });
      }
    }
  }, {
    drag: {
      from: () => [x.get(), y.get()]
    },
    pinch: {
      distanceBounds: {
        min: 1,
        max: props.maxZoom
      },
      rubberband: true,
      from: () => [zoom.get(), 0]
    }
  });
  return (0, _jsxRuntime().jsx)("div", Object.assign({
    className: (0, _clsx().default)(bem('slide')),
    onPointerMove: e => {
      if (zoom.get() !== 1) {
        e.stopPropagation();
      }
    }
  }, {
    children: (0, _jsxRuntime().jsx)("div", Object.assign({
      className: (0, _clsx().default)(bem('control'))
    }, bind(), {
      children: (0, _jsxRuntime().jsx)(_web().animated.div, Object.assign({
        className: (0, _clsx().default)(bem('image-wrapper')),
        style: {
          scale: zoom,
          x,
          y
        }
      }, {
        children: (0, _jsxRuntime().jsx)(_image.default, {
          fit: "contain",
          src: props.image,
          className: (0, _clsx().default)(bem('image')),
          lazyload: props.lazyload,
          onClick: () => {
            var _a;

            (_a = props.onTap) === null || _a === void 0 ? void 0 : _a.call(props);
          }
        })
      }))
    }))
  }));
};

var _default = ImagePreviewItem;
exports.default = _default;