'use strict';

exports.__esModule = true;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _CCarouselCustom = require('./CCarouselCustom');

var _CCarouselCustom2 = _interopRequireDefault(_CCarouselCustom);

var _CCarouselItem = require('./CCarouselItem');

var _CCarouselItem2 = _interopRequireDefault(_CCarouselItem);

var _CCarouselControl = require('./CCarouselControl');

var _CCarouselControl2 = _interopRequireDefault(_CCarouselControl);

var _CCarouselIndicators = require('./CCarouselIndicators');

var _CCarouselIndicators2 = _interopRequireDefault(_CCarouselIndicators);

var _CCarouselCaption = require('./CCarouselCaption');

var _CCarouselCaption2 = _interopRequireDefault(_CCarouselCaption);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

//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 = (0, _react.useState)(props.defaultOpen || false),
      activeIndex = _useState[0],
      setActiveIndex = _useState[1];

  var fields = (0, _react.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 _react2.default.createElement(
        _CCarouselItem2.default,
        {
          onExiting: onExiting,
          onExited: onExited,
          key: item.src
        },
        _react2.default.createElement('img', { className: 'd-block w-100', src: item.src, alt: item.altText }),
        _react2.default.createElement(_CCarouselCaption2.default, { captionText: item.caption, captionHeader: item.header || item.caption })
      );
    });

    return _react2.default.createElement(
      _CCarouselCustom2.default,
      _extends({
        activeIndex: activeIndex,
        next: next,
        previous: previous,
        ride: autoPlay ? 'carousel' : undefined
      }, props),
      indicators && _react2.default.createElement(_CCarouselIndicators2.default, {
        items: items,
        activeIndex: props.activeIndex || activeIndex,
        onClickHandler: goToIndex || toIndex
      }),
      slides,
      controls && _react2.default.createElement(_CCarouselControl2.default, {
        direction: 'prev',
        directionText: 'Previous',
        onClickHandler: props.previous || previous
      }),
      controls && _react2.default.createElement(_CCarouselControl2.default, {
        direction: 'next',
        directionText: 'Next',
        onClickHandler: props.next || next
      })
    );
  }

  return _react2.default.createElement(_CCarouselCustom2.default, props);
};

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

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

exports.default = CCarousel;
module.exports = exports['default'];