'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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _helper = require('./Shared/helper.js');

var _reactTransitionGroup = require('react-transition-group');

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

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

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

//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 = (0, _react.useContext)(_CCarouselCustom.Context);

  var _useState = (0, _react.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 _react2.default.createElement(
    _reactTransitionGroup.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 === _helper.TransitionStatuses.ENTERED || status === _helper.TransitionStatuses.EXITING;
      var directionClassName = (status === _helper.TransitionStatuses.ENTERING || status === _helper.TransitionStatuses.EXITING) && startAnimation && (direction === 'right' ? 'carousel-item-left' : 'carousel-item-right');
      var orderClassName = status === _helper.TransitionStatuses.ENTERING && (direction === 'right' ? 'carousel-item-next' : 'carousel-item-prev');
      var itemClasses = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, 'carousel-item', isActive && 'active', directionClassName, orderClassName), cssModule);
      return _react2.default.createElement(
        Tag,
        _extends({}, itemProps, { className: itemClasses, ref: innerRef }),
        children
      );
    }
  );
};

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

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

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