"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _react2 = require("@use-gesture/react");

var _web = require("@react-spring/web");

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 _getRequireWildcardCache(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; }

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); }

var ImagePreviewItem = function ImagePreviewItem(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var _useSpring = (0, _web.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 = (0, _react2.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.default.createElement("div", {
    className: (0, _clsx.default)(bem('slide')),
    onPointerMove: function onPointerMove(e) {
      if (zoom.get() !== 1) {
        e.stopPropagation();
      }
    }
  }, /*#__PURE__*/_react.default.createElement("div", _extends({
    className: (0, _clsx.default)(bem('control'))
  }, bind()), /*#__PURE__*/_react.default.createElement(_web.animated.div, {
    className: (0, _clsx.default)(bem('image-wrapper')),
    style: {
      scale: zoom,
      x: x,
      y: y
    }
  }, /*#__PURE__*/_react.default.createElement(_image.default, {
    fit: "contain",
    src: props.image,
    className: (0, _clsx.default)(bem('image')),
    lazyload: props.lazyload,
    onClick: function onClick() {
      props.onTap == null ? void 0 : props.onTap();
    }
  }))));
};

var _default = ImagePreviewItem;
exports.default = _default;