var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
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_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_page_indicator = require("./page-indicator");
var import_SwiperItem = __toESM(require("./SwiperItem"));
var import_use_ref_state = __toESM(require("../hooks/use-ref-state"));
var import_bound = require("../utils/bound");
var import_dev_log = require("../utils/dev-log");
var import_utils = require("../utils");
var import_get_rect = require("../hooks/get-rect");
var import_use_mounted_ref = __toESM(require("../hooks/use-mounted-ref"));
var import_with_func_props = require("../utils/with-func-props");
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);
}
function modulus(value, division) {
  const remainder = value % division;
  return remainder < 0 ? remainder + division : remainder;
}
const eventToPropRecord = {
  mousedown: "onMouseDown",
  mousemove: "onMouseMove",
  mouseup: "onMouseUp"
};
let currentUid;
const Swiper = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("swiper", prefixCls);
  const {
    loop: outerLoop,
    autoplay,
    direction,
    autoplayInterval
  } = props;
  const [uid] = (0, import_react.useState)({});
  const lock = (0, import_react.useRef)(false);
  const trackRef = (0, import_react.useRef)(null);
  const [root, setRoot] = (0, import_react.useState)(null);
  const [current, setCurrent] = (0, import_react.useState)(props.defaultIndex);
  const [dragging, setDragging, draggingRef] = (0, import_use_ref_state.default)(false);
  const isVertical = direction === "vertical";
  const axis = (0, import_react.useMemo)(() => isVertical ? "y" : "x", [isVertical]);
  const slideRatio = props.slideSize / 100;
  const offsetRatio = props.trackOffset / 100;
  const computedStyle = (0, import_react.useMemo)(() => {
    return __spreadValues({
      [`--${prefixCls}-swipe-slide-size`]: `${props.slideSize}%`,
      [`--${prefixCls}-swipe-track-offset`]: `${props.trackOffset}%`
    }, props.style);
  }, [props.style, props.slideSize, props.trackOffset]);
  const axisDistance = (0, import_react.useMemo)(() => {
    if (!isVertical)
      return 100;
    const rect = (0, import_get_rect.getRect)(root);
    return rect.height;
  }, [isVertical, root]);
  const {
    validChildren,
    count
  } = (0, import_react.useMemo)(() => {
    let innerCount = 0;
    const innerValidChildren = import_react.default.Children.map(props.children, (child) => {
      if (!/* @__PURE__ */ import_react.default.isValidElement(child))
        return null;
      if (child.type !== import_SwiperItem.default) {
        (0, import_dev_log.devWarning)("Swiper", "The children of `Swiper` must be `Swiper.Item` components.");
        return null;
      }
      innerCount++;
      return child;
    });
    return {
      validChildren: innerValidChildren,
      count: innerCount
    };
  }, [props.children]);
  const loop = (0, import_react.useMemo)(() => {
    if (count <= 1)
      return false;
    return outerLoop;
  }, [count, outerLoop]);
  const mountedRef = (0, import_use_mounted_ref.default)();
  const getSlidePixels = () => {
    const track = trackRef.current;
    if (!track)
      return 0;
    const trackPixels = isVertical ? track.offsetHeight : track.offsetWidth;
    return trackPixels * props.slideSize / 100;
  };
  const boundIndex = (cur) => {
    let min = 0;
    let max = count - 1;
    if (props.stuckAtBoundary) {
      min += (1 - slideRatio - offsetRatio) / slideRatio;
      max -= (1 - slideRatio - offsetRatio) / slideRatio;
    }
    return (0, import_bound.bound)(cur, min, max);
  };
  const [{
    position
  }, api] = (0, import_web.useSpring)(() => ({
    position: boundIndex(current) * 100,
    config: {
      tension: 200,
      friction: 30,
      autoplayInterval
    },
    onRest: () => {
      if (draggingRef.current)
        return;
      const rawX = position.get();
      const totalWidth = 100 * count;
      const standardPosition = modulus(rawX, totalWidth);
      if (standardPosition === rawX)
        return;
      api.start({
        position: standardPosition,
        immediate: true
      });
    }
  }), [count]);
  const dragCancelRef = (0, import_react.useRef)(null);
  function forceCancelDrag() {
    var _a;
    (_a = dragCancelRef.current) == null ? void 0 : _a.call(dragCancelRef);
    draggingRef.current = false;
  }
  const bind = (0, import_react2.useDrag)((state) => {
    dragCancelRef.current = state.cancel;
    if (!state.intentional)
      return;
    if (state.first && !currentUid) {
      currentUid = uid;
    }
    if (currentUid !== uid)
      return;
    currentUid = state.last ? void 0 : uid;
    const slidePixels = getSlidePixels();
    if (lock.current)
      return;
    if (!slidePixels)
      return;
    const paramIndex = isVertical ? 1 : 0;
    const offset = state.offset[paramIndex];
    const itemDirection = state.direction[paramIndex];
    const velocity = state.velocity[paramIndex];
    setDragging(true);
    if (!state.last) {
      api.start({
        position: offset * 100 / slidePixels,
        immediate: true
      });
    } else {
      const index = Math.round((offset + Math.min(velocity * 2e3, slidePixels) * itemDirection) / slidePixels);
      swipeTo(index);
      window.setTimeout(() => {
        if (mountedRef.current) {
          setDragging(false);
        }
      });
    }
  }, {
    transform: ([x, y]) => [-x, -y],
    from: () => {
      const slidePixels = getSlidePixels();
      return [position.get() / 100 * slidePixels, position.get() / 100 * slidePixels];
    },
    bounds: () => {
      if (loop)
        return {};
      const slidePixels = getSlidePixels();
      const lowerBound = boundIndex(0) * slidePixels;
      const upperBound = boundIndex(count - 1) * slidePixels;
      return isVertical ? {
        top: lowerBound,
        bottom: upperBound
      } : {
        left: lowerBound,
        right: upperBound
      };
    },
    rubberband: true,
    axis,
    preventScroll: !isVertical,
    pointer: {
      touch: true
    }
  });
  const renderIndicator = () => {
    if (props.indicator === void 0 || props.indicator === true) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("indicator", {
          vertical: isVertical
        }))
      }, /* @__PURE__ */ import_react.default.createElement(import_page_indicator.PageIndicator, _extends({}, props.indicatorProps, {
        direction: props.direction,
        total: count,
        current
      })));
    }
    if (typeof props.indicator === "function") {
      return props.indicator(count, current);
    }
    return null;
  };
  const onClickCapture = (e) => {
    if (draggingRef.current) {
      e.stopPropagation();
    }
    forceCancelDrag();
  };
  function swipeTo(index, immediate = false) {
    var _a, _b;
    if (loop) {
      const i = modulus(index, count);
      setCurrent(i);
      (_a = props.onIndexChange) == null ? void 0 : _a.call(props, i);
      api.start({
        position: index * 100,
        immediate
      });
    } else {
      const i = (0, import_bound.bound)(index, 0, count - 1);
      setCurrent(i);
      (_b = props.onIndexChange) == null ? void 0 : _b.call(props, i);
      api.start({
        position: boundIndex(i) * 100,
        immediate
      });
    }
  }
  const swipeNext = () => {
    swipeTo(Math.round(position.get() / 100) + 1);
  };
  const swipePrev = () => {
    swipeTo(Math.round(position.get() / 100) - 1);
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    activeIndex: current,
    swipeTo,
    swipeNext,
    swipePrev,
    lock: () => {
      lock.current = true;
    },
    unlock: () => {
      lock.current = false;
    }
  }));
  (0, import_react.useEffect)(() => {
    if (!autoplay || dragging)
      return import_utils.noop;
    const interval = window.setInterval(() => {
      swipeNext();
    }, autoplayInterval);
    return () => {
      window.clearInterval(interval);
    };
  }, [autoplay, dragging, axisDistance]);
  if (count === 0) {
    (0, import_dev_log.devWarning)("Swiper", "`Swiper` needs at least one child.");
  }
  const dragProps = __spreadValues({}, props.allowTouchMove ? bind() : {});
  const stopPropagationProps = {};
  props.stopPropagation.forEach((key) => {
    const prop = eventToPropRecord[key];
    stopPropagationProps[prop] = function(e) {
      e.stopPropagation();
    };
  });
  const mergedProps = (0, import_with_func_props.mergeFuncProps)(dragProps, stopPropagationProps);
  return /* @__PURE__ */ import_react.default.createElement("div", {
    ref: setRoot,
    className: (0, import_classnames.default)(props.className, bem({
      vertical: isVertical
    })),
    style: computedStyle
  }, /* @__PURE__ */ import_react.default.createElement("div", _extends({
    ref: trackRef,
    className: (0, import_classnames.default)(bem("track", {
      "allow-touch-move": props.allowTouchMove
    })),
    onClickCapture
  }, mergedProps), /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("track-inner", {
      vertical: isVertical
    }))
  }, import_react.default.Children.map(validChildren, (child, index) => {
    return /* @__PURE__ */ import_react.default.createElement(import_web.animated.div, {
      className: (0, import_classnames.default)(bem("slide")),
      style: {
        [axis]: position.to((pos) => {
          let finalPosition = -pos + index * 100;
          const totalWidth = count * 100;
          const flagWidth = totalWidth / 2;
          finalPosition = modulus(finalPosition + flagWidth, totalWidth) - flagWidth;
          return `${finalPosition}%`;
        }),
        left: `-${index * 100}%`
      }
    }, child);
  }))), renderIndicator());
});
Swiper.defaultProps = {
  autoplay: false,
  autoplayInterval: 3e3,
  allowTouchMove: true,
  defaultIndex: 0,
  loop: true,
  slideSize: 100,
  stuckAtBoundary: false,
  trackOffset: 0,
  stopPropagation: []
};
Swiper.displayName = "Swiper";
var stdin_default = Swiper;
