function _extends() { _extends = Object.assign || 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); }

import React, { useRef, useEffect, useContext } from 'react';
import clsx from 'clsx';
import Swiper from '../swiper';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var TabsContent = function TabsContent(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('tabs', prefixCls),
      bem = _createNamespace[0];

  var innerEffect = useRef(false);
  var animated = props.animated,
      swipeable = props.swipeable,
      duration = props.duration,
      swiperRef = props.swiperRef;
  var swiperProps = typeof swipeable === 'boolean' ? {} : swipeable;

  var renderChildren = function renderChildren() {
    if (animated || swipeable) {
      return /*#__PURE__*/React.createElement(Swiper, _extends({
        autoHeight: true
      }, swiperProps, {
        ref: swiperRef,
        rubberband: false,
        stuckAtBoundary: true,
        loop: false,
        autoplay: false,
        touchable: !!swipeable,
        className: clsx(bem('track')),
        duration: +duration,
        indicator: false,
        onChange: function onChange(idx) {
          if (innerEffect.current) {
            innerEffect.current = false;
            return;
          }

          if (props.onChange) props.onChange(idx);
        }
      }), React.Children.map(props.children, function (child) {
        return /*#__PURE__*/React.createElement(Swiper.Item, {
          style: {
            cursor: !swipeable ? 'auto' : undefined
          },
          className: clsx(bem('pane-wrapper'))
        }, child);
      }));
    }

    return props.children;
  };

  var swipeToCurrentTab = function swipeToCurrentTab(index) {
    var swipe = swiperRef.current;
    if (!swipe) return;

    if (swipe.activeIndex !== index) {
      innerEffect.current = true;
      swipe.swipeTo(index);
    }
  };

  useEffect(function () {
    swipeToCurrentTab(props.currentIndex);
  }, [props.currentIndex]);
  return /*#__PURE__*/React.createElement("div", {
    className: clsx(bem('content', {
      animated: animated || swipeable
    }))
  }, renderChildren());
};

export default TabsContent;