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_web = require("@react-spring/web");
var import_react2 = require("@use-gesture/react");
var import_use_click_away = __toESM(require("../hooks/use-click-away"));
var import_use_mounted_ref = __toESM(require("../hooks/use-mounted-ref"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_interceptor = require("../utils/interceptor");
var import_nearest = require("../utils/nearest");
var import_utils = require("../utils");
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 SwipeCell = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("swipe-cell", prefixCls);
  const rootRef = (0, import_react.useRef)(null);
  const leftRef = (0, import_react.useRef)();
  const rightRef = (0, import_react.useRef)();
  const opened = (0, import_react.useRef)(false);
  const lockClick = (0, import_react.useRef)(false);
  const draggingRef = (0, import_react.useRef)(false);
  const mountedRef = (0, import_use_mounted_ref.default)();
  const getWidth = (elementRef) => elementRef.current ? elementRef.current.offsetWidth : 0;
  function getLeftWidth() {
    return (0, import_utils.isDef)(props.leftWidth) ? +props.leftWidth : getWidth(leftRef);
  }
  function getRightWidth() {
    return (0, import_utils.isDef)(props.rightWidth) ? +props.rightWidth : getWidth(rightRef);
  }
  const [{
    x
  }, api] = (0, import_web.useSpring)(() => ({
    x: 0,
    config: {
      tension: 200,
      friction: 30
    }
  }), []);
  const bind = (0, import_react2.useDrag)((state) => {
    if (!props.disabled) {
      lockClick.current = true;
      draggingRef.current = true;
      const [offsetX] = state.offset;
      if (state.last) {
        const leftWidth = getLeftWidth();
        const rightWidth = getRightWidth();
        let position = offsetX + state.velocity[0] * state.direction[0] * 50;
        if (offsetX > 0) {
          position = Math.max(0, position);
        } else {
          position = Math.min(0, position);
        }
        const nearestPosition = (0, import_nearest.nearest)([-rightWidth, 0, leftWidth], position);
        api.start({
          x: nearestPosition
        });
        opened.current = nearestPosition !== 0;
        window.setTimeout(() => {
          if (mountedRef.current) {
            draggingRef.current = false;
            lockClick.current = false;
          }
        });
      } else {
        api.start({
          x: offsetX,
          immediate: true
        });
      }
    }
  }, {
    from: () => [x.get(), 0],
    bounds: () => {
      const leftWidth = getLeftWidth();
      const rightWidth = getRightWidth();
      return {
        left: -rightWidth,
        right: leftWidth
      };
    },
    axis: "x",
    preventScroll: true,
    pointer: {
      touch: true
    }
  });
  const open = (side) => {
    var _a;
    opened.current = true;
    const offset = side === "left" ? getLeftWidth() : -getRightWidth();
    api.start({
      x: offset
    });
    (_a = props.onOpen) == null ? void 0 : _a.call(props, {
      position: side,
      name: props.name
    });
  };
  const close = (position) => {
    var _a;
    api.start({
      x: 0
    });
    if (opened.current) {
      opened.current = false;
      (_a = props.onClose) == null ? void 0 : _a.call(props, {
        position,
        name: props.name
      });
    }
  };
  const onClick = (position = "outside") => {
    var _a;
    (_a = props.onClick) == null ? void 0 : _a.call(props, position);
    if (opened.current && !lockClick.current) {
      (0, import_interceptor.callInterceptor)({
        interceptor: props.beforeClose,
        args: [{
          name: props.name,
          position,
          instance: {
            close: () => close(position)
          }
        }],
        done: () => close(position)
      });
    }
  };
  const getClickHandler = (position, stop) => (event) => {
    if (stop) {
      event.preventDefault();
      event.stopPropagation();
    }
    onClick(position);
  };
  const renderSideContent = (side, sideRef) => {
    const contentSlot = props[side];
    return contentSlot ? /* @__PURE__ */ import_react.default.createElement("div", {
      ref: sideRef,
      className: (0, import_classnames.default)(bem(side)),
      onClick: getClickHandler(side, true)
    }, contentSlot) : null;
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: (side) => open(side),
    close
  }));
  (0, import_use_click_away.default)(rootRef, () => onClick("outside"), "touchstart", props.hideOnClickOutside);
  return /* @__PURE__ */ import_react.default.createElement("div", _extends({
    ref: rootRef
  }, bind(), {
    className: (0, import_classnames.default)(bem()),
    onClickCapture: (e) => {
      if (draggingRef.current) {
        e.stopPropagation();
        e.preventDefault();
      }
    },
    onClick: getClickHandler("cell")
  }), /* @__PURE__ */ import_react.default.createElement(import_web.animated.div, {
    className: (0, import_classnames.default)(bem("wrapper")),
    style: {
      x
    }
  }, renderSideContent("left", leftRef), /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("content")),
    onClickCapture: (e) => {
      if (x.goal !== 0) {
        e.preventDefault();
        e.stopPropagation();
        api.start({
          x: 0
        });
      }
    }
  }, /* @__PURE__ */ import_react.default.createElement(import_web.animated.div, {
    style: {
      pointerEvents: x.to((v) => v !== 0 && x.goal !== 0 ? "none" : "unset")
    }
  }, props.children)), renderSideContent("right", rightRef)));
});
SwipeCell.defaultProps = {
  disabled: false,
  hideOnClickOutside: true
};
var stdin_default = SwipeCell;
