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);
}
import React, { forwardRef, useContext, useRef, useImperativeHandle } from "react";
import classNames from "classnames";
import { useSpring, animated } from "@react-spring/web";
import { useDrag } from "@use-gesture/react";
import useClickAway from "../hooks/use-click-away";
import useMountedRef from "../hooks/use-mounted-ref";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import { callInterceptor } from "../utils/interceptor";
import { nearest } from "../utils/nearest";
import { isDef } from "../utils";
const SwipeCell = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("swipe-cell", prefixCls);
  const rootRef = useRef(null);
  const leftRef = useRef();
  const rightRef = useRef();
  const opened = useRef(false);
  const lockClick = useRef(false);
  const draggingRef = useRef(false);
  const mountedRef = useMountedRef();
  const getWidth = (elementRef) => elementRef.current ? elementRef.current.offsetWidth : 0;
  function getLeftWidth() {
    return isDef(props.leftWidth) ? +props.leftWidth : getWidth(leftRef);
  }
  function getRightWidth() {
    return isDef(props.rightWidth) ? +props.rightWidth : getWidth(rightRef);
  }
  const [{
    x
  }, api] = useSpring(() => ({
    x: 0,
    config: {
      tension: 200,
      friction: 30
    }
  }), []);
  const bind = 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 = 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) {
      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__ */ React.createElement("div", {
      ref: sideRef,
      className: classNames(bem(side)),
      onClick: getClickHandler(side, true)
    }, contentSlot) : null;
  };
  useImperativeHandle(ref, () => ({
    open: (side) => open(side),
    close
  }));
  useClickAway(rootRef, () => onClick("outside"), "touchstart", props.hideOnClickOutside);
  return /* @__PURE__ */ React.createElement("div", _extends({
    ref: rootRef
  }, bind(), {
    className: classNames(bem()),
    onClickCapture: (e) => {
      if (draggingRef.current) {
        e.stopPropagation();
        e.preventDefault();
      }
    },
    onClick: getClickHandler("cell")
  }), /* @__PURE__ */ React.createElement(animated.div, {
    className: classNames(bem("wrapper")),
    style: {
      x
    }
  }, renderSideContent("left", leftRef), /* @__PURE__ */ React.createElement("div", {
    className: classNames(bem("content")),
    onClickCapture: (e) => {
      if (x.goal !== 0) {
        e.preventDefault();
        e.stopPropagation();
        api.start({
          x: 0
        });
      }
    }
  }, /* @__PURE__ */ React.createElement(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;
export {
  stdin_default as default
};
