"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
var React = _react;
require("./index.scss");
var _excluded = ["speed", "autoplay", "loop", "picUrls", "height", "delay"];
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 DCSlider = function DCSlider(_ref) {
  var _ref$speed = _ref.speed,
    speed = _ref$speed === void 0 ? 500 : _ref$speed,
    autoplay = _ref.autoplay,
    loop = _ref.loop,
    _ref$picUrls = _ref.picUrls,
    picUrls = _ref$picUrls === void 0 ? [] : _ref$picUrls,
    height = _ref.height,
    delay = _ref.delay,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _useState = (0, _react.useState)(0),
    currentIndex = _useState[0],
    setCurrentIndex = _useState[1];
  var autoplayRef = (0, _react.useRef)(null);

  // 切换到下一张
  var nextSlide = function nextSlide() {
    if (currentIndex < picUrls.length - 1) {
      setCurrentIndex(currentIndex + 1);
    } else if (loop) {
      setCurrentIndex(0);
    } else {
      stopAutoplay();
    }
  };

  // 启动自动播放
  var startAutoplay = function startAutoplay() {
    if (autoplay && !autoplayRef.current) {
      autoplayRef.current = setInterval(function () {
        nextSlide();
      }, delay);
    }
  };

  // 停止自动播放
  var stopAutoplay = function stopAutoplay() {
    if (autoplayRef.current) {
      clearInterval(autoplayRef.current);
      autoplayRef.current = null;
    }
  };
  (0, _react.useEffect)(function () {
    startAutoplay();
    return function () {
      return stopAutoplay();
    };
  }, [autoplay, currentIndex]);
  return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({
    className: "carousel",
    style: {
      height: height ? height + "px" : 'auto'
    }
  }, otherProps), /*#__PURE__*/React.createElement("div", {
    className: "carousel__wrapper",
    style: {
      transform: "translateX(-" + currentIndex * 100 + "%)",
      transitionDuration: speed + "ms"
    }
  }, picUrls.map(function (url, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: "carousel__slide"
    }, /*#__PURE__*/React.createElement("img", {
      src: url,
      alt: "Slide " + index,
      style: {
        height: height ? height + "px" : '100%'
      }
    }));
  })));
};
var _default = exports["default"] = DCSlider;