"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Slides = void 0;
var _web = require("@react-spring/web");
var _react = require("@use-gesture/react");
var _react2 = _interopRequireWildcard(require("react"));
var _bound = require("../../utils/bound");
var _convertPx = require("../../utils/convert-px");
var _slide = require("./slide");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-image-viewer`;
const Slides = exports.Slides = (0, _react2.forwardRef)((props, ref) => {
  const slideWidth = window.innerWidth + (0, _convertPx.convertPx)(16);
  const [{
    x
  }, api] = (0, _web.useSpring)(() => ({
    x: props.defaultIndex * slideWidth,
    config: {
      tension: 250,
      clamp: true
    }
  }));
  const count = props.images.length;
  function swipeTo(index, immediate = false) {
    var _a;
    const i = (0, _bound.bound)(index, 0, count - 1);
    (_a = props.onIndexChange) === null || _a === void 0 ? void 0 : _a.call(props, i);
    api.start({
      x: i * slideWidth,
      immediate
    });
  }
  (0, _react2.useImperativeHandle)(ref, () => ({
    swipeTo
  }));
  const dragLockRef = (0, _react2.useRef)(false);
  const bind = (0, _react.useDrag)(state => {
    if (dragLockRef.current) return;
    const [offsetX] = state.offset;
    if (state.last) {
      const minIndex = Math.floor(offsetX / slideWidth);
      const maxIndex = minIndex + 1;
      const velocityOffset = Math.min(state.velocity[0] * 2000, slideWidth) * state.direction[0];
      swipeTo((0, _bound.bound)(Math.round((offsetX + velocityOffset) / slideWidth), minIndex, maxIndex));
    } else {
      api.start({
        x: offsetX,
        immediate: true
      });
    }
  }, {
    transform: ([x, y]) => [-x, y],
    from: () => [x.get(), 0],
    bounds: () => ({
      left: 0,
      right: (count - 1) * slideWidth
    }),
    rubberband: true,
    axis: 'x',
    pointer: {
      touch: true
    }
  });
  return _react2.default.createElement("div", Object.assign({
    className: `${classPrefix}-slides`
  }, bind()), _react2.default.createElement(_web.animated.div, {
    className: `${classPrefix}-indicator`
  }, x.to(v => {
    const index = (0, _bound.bound)(Math.round(v / slideWidth), 0, count - 1);
    return `${index + 1} / ${count}`;
  })), _react2.default.createElement(_web.animated.div, {
    className: `${classPrefix}-slides-inner`,
    style: {
      x: x.to(x => -x)
    }
  }, props.images.map((image, index) => _react2.default.createElement(_slide.Slide, {
    key: index,
    image: image,
    onTap: props.onTap,
    maxZoom: props.maxZoom,
    imageRender: props.imageRender,
    index: index,
    onZoomChange: zoom => {
      if (zoom !== 1) {
        const index = Math.round(x.get() / slideWidth);
        api.start({
          x: index * slideWidth
        });
      }
    },
    dragLockRef: dragLockRef
  }))));
});