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

const TabsContent = props => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('tabs', prefixCls);
  const innerEffect = useRef(false);
  const {
    animated,
    swipeable,
    duration,
    swiperRef
  } = props;
  const swiperProps = typeof swipeable === 'boolean' ? {} : swipeable;

  const renderChildren = () => {
    if (animated || swipeable) {
      return _jsx(Swiper, Object.assign({
        autoHeight: true
      }, swiperProps, {
        ref: swiperRef,
        rubberband: false,
        stuckAtBoundary: true,
        loop: false,
        autoplay: false,
        touchable: !!swipeable,
        className: clsx(bem('track')),
        duration: +duration,
        indicator: false,
        onChange: idx => {
          if (innerEffect.current) {
            innerEffect.current = false;
            return;
          }

          if (props.onChange) props.onChange(idx);
        }
      }, {
        children: React.Children.map(props.children, child => _jsx(Swiper.Item, Object.assign({
          style: {
            cursor: !swipeable ? 'auto' : undefined
          },
          className: clsx(bem('pane-wrapper'))
        }, {
          children: child
        })))
      }));
    }

    return props.children;
  };

  const swipeToCurrentTab = index => {
    const swipe = swiperRef.current;
    if (!swipe) return;

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

  useEffect(() => {
    swipeToCurrentTab(props.currentIndex);
  }, [props.currentIndex]);
  return _jsx("div", Object.assign({
    className: clsx(bem('content', {
      animated: animated || swipeable
    }))
  }, {
    children: renderChildren()
  }));
};

export default TabsContent;