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

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');

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

var transitionStatusToClassHash = (_transitionStatusToCl = {}, _transitionStatusToCl[_helper.TransitionStatuses.ENTERING] = 'collapsing', _transitionStatusToCl[_helper.TransitionStatuses.ENTERED] = 'collapse show', _transitionStatusToCl[_helper.TransitionStatuses.EXITING] = 'collapsing', _transitionStatusToCl[_helper.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 = (0, _react.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 = (0, _helper.pick)(otherProps, _helper.TransitionPropTypeKeys);
  var childProps = (0, _helper.omit)(otherProps, _helper.TransitionPropTypeKeys);

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

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

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

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