import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useState, useMemo } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useClassNames, useCustom, guid, ReactChildren, useTimeout } from '../utils';
var defaultProps = {
  as: 'div',
  classPrefix: 'carousel',
  autoplayInterval: 4000,
  placement: 'bottom',
  shape: 'dot'
};
var Carousel = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _sliderStyles, _ref;

  var Component = props.as,
      children = props.children,
      classPrefix = props.classPrefix,
      className = props.className,
      placement = props.placement,
      shape = props.shape,
      autoplay = props.autoplay,
      autoplayInterval = props.autoplayInterval,
      rest = _objectWithoutPropertiesLoose(props, ["as", "children", "classPrefix", "className", "placement", "shape", "autoplay", "autoplayInterval"]);

  var _useCustom = useCustom('Carousel'),
      rtl = _useCustom.rtl;

  var _useClassNames = useClassNames(classPrefix),
      prefix = _useClassNames.prefix,
      merge = _useClassNames.merge,
      withClassPrefix = _useClassNames.withClassPrefix;

  var count = ReactChildren.count(children);
  var labels = [];
  var vertical = placement === 'left' || placement === 'right';
  var lengthKey = vertical ? 'height' : 'width';

  var _useState = useState(0),
      activeIndex = _useState[0],
      setActiveIndex = _useState[1];

  var _useState2 = useState(0),
      lastIndex = _useState2[0],
      setLastIndex = _useState2[1];

  var handleAutoplay = function handleAutoplay(nextActiveIndex) {
    clear();
    var nextIndex = nextActiveIndex !== null && nextActiveIndex !== void 0 ? nextActiveIndex : activeIndex + 1; // When index is greater than count, start from 1 again.

    setActiveIndex(nextIndex % count);
    setLastIndex(nextActiveIndex == null ? activeIndex : nextIndex % count);
    reset();
  };

  var handleChange = function handleChange(event) {
    var activeIndex = +event.target.value;
    handleAutoplay(activeIndex);
  }; // Set a timer for automatic playback.
  // `autoplay` needs to be cast to boolean type to avoid undefined parameters.


  var _useTimeout = useTimeout(handleAutoplay, autoplayInterval, !!autoplay && count > 1),
      clear = _useTimeout.clear,
      reset = _useTimeout.reset;

  var uniqueId = useMemo(function () {
    return guid();
  }, []);
  var items = React.Children.map(children, function (child, index) {
    var _extends2;

    if (!child) {
      return;
    }

    var inputKey = "indicator_" + uniqueId + "_" + index;
    labels.push( /*#__PURE__*/React.createElement("li", {
      key: "label" + index,
      className: prefix('label-wrapper')
    }, /*#__PURE__*/React.createElement("input", {
      name: inputKey,
      id: inputKey,
      type: "radio",
      onChange: handleChange,
      value: index,
      checked: activeIndex === index
    }), /*#__PURE__*/React.createElement("label", {
      htmlFor: inputKey,
      className: prefix('label')
    })));
    return /*#__PURE__*/React.cloneElement(child, {
      key: "slider-item" + index,
      'aria-hidden': activeIndex !== index,
      style: _extends({}, child.props.style, (_extends2 = {}, _extends2[lengthKey] = 100 / count + "%", _extends2)),
      className: classNames(prefix('slider-item'), child.props.className)
    });
  });
  var classes = merge(className, withClassPrefix("placement-" + placement, "shape-" + shape));
  var positiveOrder = vertical || !rtl;
  var sign = positiveOrder ? '-' : '';
  var activeRatio = "" + sign + 100 / count * activeIndex + "%";
  var sliderStyles = (_sliderStyles = {}, _sliderStyles[lengthKey] = count * 100 + "%", _sliderStyles.transform = vertical ? "translate3d(0, " + activeRatio + " ,0)" : "translate3d(" + activeRatio + ", 0 ,0)", _sliderStyles);
  var showMask = count > 1 && activeIndex === 0 && activeIndex !== lastIndex;
  return /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/React.createElement("div", {
    className: prefix('content')
  }, /*#__PURE__*/React.createElement("div", {
    className: prefix('slider'),
    style: sliderStyles
  }, items), showMask && /*#__PURE__*/React.createElement("div", {
    className: prefix('slider-after', {
      'slider-after-vertical': vertical
    }),
    style: (_ref = {}, _ref[lengthKey] = '200%', _ref)
  }, [items[items.length - 1], items[0]].map(function (node) {
    var _extends3;

    return /*#__PURE__*/React.cloneElement(node, {
      key: node.key,
      style: _extends({}, node.props.style, (_extends3 = {}, _extends3[lengthKey] = '50%', _extends3))
    });
  }))), /*#__PURE__*/React.createElement("div", {
    className: prefix('toolbar')
  }, /*#__PURE__*/React.createElement("ul", null, labels)));
});
Carousel.displayName = 'Carousel';
Carousel.defaultProps = defaultProps;
Carousel.propTypes = {
  as: PropTypes.elementType,
  className: PropTypes.string,
  classPrefix: PropTypes.string,
  autoplay: PropTypes.bool,
  autoplayInterval: PropTypes.number,
  placement: PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
  shape: PropTypes.oneOf(['dot', 'bar'])
};
export default Carousel;