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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React, { useState, useContext } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, TransitionTimeouts, TransitionStatuses, tagPropType } from './Shared/helper.js';
import { Transition } from 'react-transition-group';
import { Context } from './CCarouselCustom';

//component - CoreUI / CCarouselItem

var CCarouselItem = function CCarouselItem(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      isIn = props.in,
      slide = props.slide,
      itemProps = props.itemProps,
      transitionProps = _objectWithoutProperties(props, ['tag', 'children', 'className', 'cssModule', 'innerRef', 'in', 'slide', 'itemProps']);

  var context = useContext(Context);

  var _useState = useState(false),
      startAnimation = _useState[0],
      setStartAnimation = _useState[1];

  var onEnter = function onEnter(node, isAppearing) {
    setStartAnimation(false);
    props.onEnter(node, isAppearing);
  };

  var onEntering = function onEntering(node, isAppearing) {
    var offsetHeight = node.offsetHeight;
    setStartAnimation(true);
    props.onEntering(node, isAppearing);
    return offsetHeight;
  };

  var onExit = function onExit(node) {
    setStartAnimation(false);
    props.onExit(node);
  };

  var onExiting = function onExiting(node) {
    setStartAnimation(true);
    node.dispatchEvent(new CustomEvent('slide.bs.carousel'));
    props.onExiting(node);
  };

  var onExited = function onExited(node) {
    node.dispatchEvent(new CustomEvent('slid.bs.carousel'));
    props.onExited(node);
  };

  //render

  return React.createElement(
    Transition,
    _extends({}, transitionProps, {
      enter: slide,
      exit: slide,
      'in': isIn,
      onEnter: onEnter,
      onEntering: onEntering,
      onExit: onExit,
      onExiting: onExiting,
      onExited: onExited
    }),
    function (status) {
      var direction = context.direction;

      var isActive = status === TransitionStatuses.ENTERED || status === TransitionStatuses.EXITING;
      var directionClassName = (status === TransitionStatuses.ENTERING || status === TransitionStatuses.EXITING) && startAnimation && (direction === 'right' ? 'carousel-item-left' : 'carousel-item-right');
      var orderClassName = status === TransitionStatuses.ENTERING && (direction === 'right' ? 'carousel-item-next' : 'carousel-item-prev');
      var itemClasses = mapToCssModules(classNames(className, 'carousel-item', isActive && 'active', directionClassName, orderClassName), cssModule);
      return React.createElement(
        Tag,
        _extends({}, itemProps, { className: itemClasses, ref: innerRef }),
        children
      );
    }
  );
};

CCarouselItem.propTypes = process.env.NODE_ENV !== "production" ? _extends({}, Transition.propTypes, {
  tag: tagPropType,
  children: PropTypes.node,
  className: PropTypes.string,
  cssModule: PropTypes.object,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  in: PropTypes.bool,
  slide: PropTypes.bool,
  itemProps: PropTypes.object
}) : {};

CCarouselItem.defaultProps = _extends({}, Transition.defaultProps, {
  tag: 'div',
  timeout: TransitionTimeouts.Carousel,
  slide: true
});

export default CCarouselItem;