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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  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_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_renderToContainer = require("../utils/dom/renderToContainer");
var import_icon = require("../icon");
var import_get_rect = __toESM(require("../hooks/get-rect"));
var import_use_window_size = __toESM(require("../hooks/use-window-size"));
var import_utils = require("../utils");
var import_use_touch = require("../hooks/use-touch");
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_closest = require("../utils/closest");
var import_hooks = require("../hooks");
const FloatingBubble = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("floating-bubble", prefixCls);
  const {
    offset,
    axis,
    gap,
    magnetic,
    icon,
    onClick,
    teleport,
    children
  } = props;
  const windowSize = (0, import_use_window_size.default)();
  const touch = (0, import_use_touch.useTouch)();
  const rootRef = (0, import_react.useRef)();
  const prevX = (0, import_react.useRef)(0);
  const prevY = (0, import_react.useRef)(0);
  const [state, setState] = (0, import_hooks.useSetState)({
    x: 0,
    y: 0,
    width: 0,
    height: 0
  });
  const [dragging, setDragging] = (0, import_react.useState)(false);
  const [show, setShow] = (0, import_react.useState)(true);
  const [initialized, setInitialized] = (0, import_react.useState)(false);
  const boundary = (0, import_react.useMemo)(() => {
    return {
      top: gap,
      right: windowSize.width - state.width - gap,
      bottom: windowSize.height - state.height - gap,
      left: gap
    };
  }, [windowSize, gap, state]);
  const rootStyle = (0, import_react.useMemo)(() => {
    const style = {};
    const x = (0, import_utils.addUnit)(state.x);
    const y = (0, import_utils.addUnit)(state.y);
    style.transform = `translate3d(${x}, ${y}, 0)`;
    if (dragging || !initialized) {
      style.transition = "none";
    }
    return style;
  }, [state, dragging, initialized]);
  const updateState = () => {
    const {
      width,
      height
    } = (0, import_get_rect.default)(rootRef.current);
    setState({
      x: offset.x > 1 ? offset.x : windowSize.width - width - gap,
      y: offset.y > 1 ? offset.y : windowSize.height - height - gap,
      width,
      height
    });
  };
  const onTouchStart = (event) => {
    touch.start(event);
    setDragging(true);
    prevX.current = state.x;
    prevY.current = state.y;
  };
  const onTouchMove = (event) => {
    event.preventDefault();
    touch.move(event);
    if (axis === "lock")
      return;
    if (!touch.isTap.current) {
      if (axis === "x" || axis === "xy") {
        let nextX = prevX.current + touch.deltaX.current;
        if (nextX < boundary.left)
          nextX = boundary.left;
        if (nextX > boundary.right)
          nextX = boundary.right;
        setState({
          x: nextX
        });
      }
      if (axis === "y" || axis === "xy") {
        let nextY = prevY.current + touch.deltaY.current;
        if (nextY < boundary.top)
          nextY = boundary.top;
        if (nextY > boundary.bottom)
          nextY = boundary.bottom;
        setState({
          y: nextY
        });
      }
    }
  };
  (0, import_use_event_listener.default)("touchmove", onTouchMove, {
    target: rootRef.current
  });
  const onTouchEnd = () => {
    var _a;
    setDragging(false);
    if (magnetic === "x") {
      const nextX = (0, import_closest.closest)([boundary.left, boundary.right], state.x);
      setState({
        x: nextX
      });
    }
    if (magnetic === "y") {
      const nextY = (0, import_closest.closest)([boundary.top, boundary.bottom], state.y);
      setState({
        y: nextY
      });
    }
    if (!touch.isTap.current) {
      const updateOffset = (0, import_utils.pick)(state, ["x", "y"]);
      if (prevX.current !== updateOffset.x || prevY.current !== updateOffset.y) {
        (_a = props == null ? void 0 : props.onOffsetChange) == null ? void 0 : _a.call(props, updateOffset);
      }
    }
  };
  (0, import_react.useEffect)(() => {
    updateState();
    setInitialized(true);
    return () => {
      if (teleport)
        setShow(false);
    };
  }, []);
  const renderContent = () => {
    return show && /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem()),
      ref: rootRef,
      onTouchStart,
      onTouchEnd,
      onTouchCancel: onTouchEnd,
      onClick: (e) => {
        if (touch.isTap.current) {
          onClick == null ? void 0 : onClick(e);
        }
      },
      style: rootStyle
    }, children || /* @__PURE__ */ import_react.default.createElement(import_icon.Icon, {
      name: icon,
      className: (0, import_classnames.default)(bem("icon"))
    }));
  };
  return teleport ? (0, import_renderToContainer.renderToContainer)(teleport, renderContent()) : renderContent();
};
FloatingBubble.defaultProps = {
  gap: 24,
  axis: "y",
  offset: {
    x: -1,
    y: -1
  },
  teleport: () => document.body
};
var stdin_default = FloatingBubble;
