"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
require("./carousel.scss");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
var Carousel = function Carousel(_ref) {
  var _ref$images = _ref.images,
    images = _ref$images === void 0 ? [] : _ref$images,
    _ref$interval = _ref.interval,
    interval = _ref$interval === void 0 ? 3000 : _ref$interval;
  // 如果没有图片则返回提示
  if (images.length === 0) {
    return /*#__PURE__*/_react["default"].createElement("div", null);
  }
  // 如果只有一张图片，则直接显示
  if (images.length === 1) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "carousel"
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: images[0],
      alt: "Single slide",
      className: "carousel-image"
    }));
  }
  // 添加复制的第一张和最后一张图片以实现无缝切换
  var extendedImages = [images[images.length - 1]].concat(images, [images[0]]);
  // 初始显示第二张图片
  var _useState = (0, _react.useState)(1),
    currentIndex = _useState[0],
    setCurrentIndex = _useState[1];
  // 切换到下一张图片
  var goToNext = function goToNext() {
    setCurrentIndex(function (prevIndex) {
      return prevIndex + 1;
    });
  };
  // 自动播放逻辑：在组件挂载时启动定时器
  (0, _react.useEffect)(function () {
    var timer = setInterval(function () {
      goToNext();
    }, interval);
    // 清除定时器，避免内存泄漏
    return function () {
      return clearInterval(timer);
    };
  }, [interval]);
  (0, _react.useEffect)(function () {
    // 当切换到最后一张复制的图片时，设置为第二张图片
    if (currentIndex === extendedImages.length - 1) {
      setTimeout(function () {
        // 跳转到第二张
        setCurrentIndex(1);
      }, 1000);
    }
  }, [currentIndex]);
  // 计算当前显示的图像索引
  var displayIndex = currentIndex > images.length ? currentIndex - images.length : currentIndex;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "carousel"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "carousel-track",
    style: {
      transform: "translateX(-" + currentIndex * 100 + "%)",
      // 只有在平滑切换时才应用过渡
      transition: currentIndex === 1 ? 'none' : 'transform 1s ease-in-out'
    }
  }, extendedImages.map(function (image, index) {
    return /*#__PURE__*/_react["default"].createElement("img", {
      key: index,
      src: image,
      alt: "Slide " + index,
      className: "carousel-image"
    });
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "carousel-counter"
  }, /*#__PURE__*/_react["default"].createElement("span", null, displayIndex), /*#__PURE__*/_react["default"].createElement("span", null, "/"), /*#__PURE__*/_react["default"].createElement("span", null, images.length)));
};
var _default = exports["default"] = Carousel;