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

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 } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, omit, pick, TransitionTimeouts, TransitionPropTypeKeys, TransitionStatuses, tagPropType } from './Shared/helper.js';
import { Transition } from 'react-transition-group';

var transitionStatusToClassHash = (_transitionStatusToCl = {}, _transitionStatusToCl[TransitionStatuses.ENTERING] = 'collapsing', _transitionStatusToCl[TransitionStatuses.ENTERED] = 'collapse show', _transitionStatusToCl[TransitionStatuses.EXITING] = 'collapsing', _transitionStatusToCl[TransitionStatuses.EXITED] = 'collapse', _transitionStatusToCl);

var getTransitionClass = function getTransitionClass(status) {
  return transitionStatusToClassHash[status] || 'collapse';
};

var getHeight = function getHeight(node) {
  return node.scrollHeight;
};

//component - CoreUI / CCollapseCustom

var CCollapseCustom = function CCollapseCustom(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      show = props.show,
      navbar = props.navbar,
      otherProps = _objectWithoutProperties(props, ['tag', 'children', 'className', 'cssModule', 'innerRef', 'show', 'navbar']);

  var _useState = useState(null),
      height = _useState[0],
      setHeight = _useState[1];

  var onEntering = function onEntering(node, isAppearing) {
    setHeight(getHeight(node));
    props.onEntering(node, isAppearing);
  };

  var onEntered = function onEntered(node, isAppearing) {
    setHeight(null);
    props.onEntered(node, isAppearing);
  };

  var onExit = function onExit(node) {
    setHeight(getHeight(node));
    props.onExit(node);
  };

  var onExiting = function onExiting(node) {
    var _unused = node.offsetHeight; // eslint-disable-line no-unused-vars
    setHeight(0);
    props.onExiting(node);
  };

  var onExited = function onExited(node) {
    setHeight(null);
    props.onExited(node);
  };

  //render

  var transitionProps = pick(otherProps, TransitionPropTypeKeys);
  var childProps = omit(otherProps, TransitionPropTypeKeys);

  return React.createElement(
    Transition,
    _extends({}, transitionProps, {
      'in': show,
      onEntering: onEntering,
      onEntered: onEntered,
      onExit: onExit,
      onExiting: onExiting,
      onExited: onExited
    }),
    function (status) {
      var collapseClass = getTransitionClass(status);
      var classes = mapToCssModules(classNames(className, collapseClass, navbar && 'navbar-collapse'), cssModule);
      var style = height === null ? null : { height: height };
      return React.createElement(
        Tag,
        _extends({}, childProps, {
          style: _extends({}, childProps.style, style),
          className: classes,
          ref: props.innerRef
        }),
        children
      );
    }
  );
};

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

CCollapseCustom.defaultProps = _extends({}, Transition.defaultProps, {
  show: false,
  appear: false,
  enter: true,
  exit: true,
  tag: 'div',
  timeout: TransitionTimeouts.Collapse
});

export default CCollapseCustom;