var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_react2 = require("@use-gesture/react");
var import_web = require("@react-spring/web");
var import_image = __toESM(require("../image"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : 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);
}
const ImagePreviewItem = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("image-preview", prefixCls);
  const [{
    zoom,
    x,
    y
  }, api] = (0, import_web.useSpring)(() => ({
    zoom: 1,
    x: 0,
    y: 0,
    config: {
      tension: 300
    }
  }));
  const bind = (0, import_react2.useGesture)({
    onDrag: (state) => {
      if (state.tap && state.elapsedTime > 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 [offsetX] = state.offset;
      const currentZoom = state.last ? Math.max(Math.min(offsetX, props.maxZoom), 1) : offsetX;
      api.start({
        zoom: currentZoom,
        immediate: !state.last
      });
      (_a = props.onZoomChange) == null ? 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 /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("slide")),
    "data-testid": "slide",
    onPointerMove: (e) => {
      if (zoom.get() !== 1) {
        e.stopPropagation();
      }
    }
  }, /* @__PURE__ */ import_react.default.createElement("div", _extends({
    className: (0, import_classnames.default)(bem("control"))
  }, bind(), {
    "data-testid": "control"
  }), /* @__PURE__ */ import_react.default.createElement(import_web.animated.div, {
    className: (0, import_classnames.default)(bem("image-wrapper")),
    style: {
      scale: zoom,
      x,
      y
    }
  }, /* @__PURE__ */ import_react.default.createElement(import_image.default, {
    fit: "contain",
    src: props.image,
    className: (0, import_classnames.default)(bem("image")),
    onClick: () => {
      var _a;
      (_a = props.onTap) == null ? void 0 : _a.call(props);
    }
  }))));
};
var stdin_default = ImagePreviewItem;
