var _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; };

import React, { useState, useRef } from 'react';
import PropTypes from 'prop-types';
import CCarouselCustom from './CCarouselCustom';
import CCarouselItem from './CCarouselItem';
import CCarouselControl from './CCarouselControl';
import CCarouselIndicators from './CCarouselIndicators';
import CCarouselCaption from './CCarouselCaption';

//component - CoreUI / CCarousel

var CCarousel = function CCarousel(props) {
  var custom = props.custom,
      autoPlay = props.autoPlay,
      indicators = props.indicators,
      controls = props.controls,
      items = props.items,
      goToIndex = props.goToIndex;

  var _useState = useState(props.defaultOpen || false),
      activeIndex = _useState[0],
      setActiveIndex = _useState[1];

  var fields = useRef({ animating: false }).current;

  if (!custom) {

    var onExiting = function onExiting() {
      fields.animating = true;
    };

    var onExited = function onExited() {
      fields.animating = false;
    };

    var next = function next() {
      if (fields.animating) return;
      var nextIndex = activeIndex === props.items.length - 1 ? 0 : activeIndex + 1;
      setActiveIndex(nextIndex);
    };

    var previous = function previous() {
      if (fields.animating) return;
      var nextIndex = activeIndex === 0 ? props.items.length - 1 : activeIndex - 1;
      setActiveIndex(nextIndex);
    };

    var toIndex = function toIndex(newIndex) {
      if (fields.animating) return;
      setActiveIndex(newIndex);
    };

    //render

    var slides = items.map(function (item) {
      return React.createElement(
        CCarouselItem,
        {
          onExiting: onExiting,
          onExited: onExited,
          key: item.src
        },
        React.createElement('img', { className: 'd-block w-100', src: item.src, alt: item.altText }),
        React.createElement(CCarouselCaption, { captionText: item.caption, captionHeader: item.header || item.caption })
      );
    });

    return React.createElement(
      CCarouselCustom,
      _extends({
        activeIndex: activeIndex,
        next: next,
        previous: previous,
        ride: autoPlay ? 'carousel' : undefined
      }, props),
      indicators && React.createElement(CCarouselIndicators, {
        items: items,
        activeIndex: props.activeIndex || activeIndex,
        onClickHandler: goToIndex || toIndex
      }),
      slides,
      controls && React.createElement(CCarouselControl, {
        direction: 'prev',
        directionText: 'Previous',
        onClickHandler: props.previous || previous
      }),
      controls && React.createElement(CCarouselControl, {
        direction: 'next',
        directionText: 'Next',
        onClickHandler: props.next || next
      })
    );
  }

  return React.createElement(CCarouselCustom, props);
};

CCarousel.propTypes = process.env.NODE_ENV !== "production" ? {
  custom: PropTypes.bool,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  items: PropTypes.array,
  indicators: PropTypes.bool,
  controls: PropTypes.bool,
  autoPlay: PropTypes.bool,
  activeIndex: PropTypes.number,
  next: PropTypes.func,
  previous: PropTypes.func,
  goToIndex: PropTypes.func
} : {};

CCarousel.defaultProps = {
  controls: true,
  indicators: true,
  autoPlay: true
};

export default CCarousel;