function _extends() { _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; }; return _extends.apply(this, arguments); }

import { isNil } from 'lodash';
import React, { useEffect, useRef } from 'react'; //import  also

import { LayoutAnimation } from 'react-native';
import { Box } from '../../primitives';

function usePrevious(value) {
  const ref = useRef();

  function updatePrevious(newVal) {
    ref.current = newVal;
  }

  useEffect(() => {
    updatePrevious(value);
  }, [value]);
  return {
    value: ref.current,
    updatePrevious
  };
}

const Collapse = ({
  endingHeight,
  startingHeight,
  duration,
  // animateOpacity,
  isOpen,
  onAnimationEnd,
  onAnimationStart,
  ...props
}) => {
  const CustomLayoutLinear = {
    duration: duration ? duration : 400,
    create: {
      type: LayoutAnimation.Types.linear,
      property: LayoutAnimation.Properties.opacity
    },
    update: {
      type: LayoutAnimation.Types.easeInEaseOut
    }
  };
  const defaultStartHeight = isOpen ? endingHeight : startingHeight ? startingHeight : 0;
  let animatedStyle = {
    height: defaultStartHeight
  };

  const animateView = () => {
    if (onAnimationStart) {
      onAnimationStart();
    }

    animatedStyle = {
      height: isOpen ? endingHeight : defaultStartHeight
    };
    let callback = onAnimationEnd ? onAnimationEnd : () => {};
    LayoutAnimation.configureNext(CustomLayoutLinear, callback());
  };

  let wasOpen = usePrevious(isOpen);

  if (!isNil(wasOpen.value) && wasOpen.value !== isOpen) {
    animateView();
    wasOpen.updatePrevious(isOpen);
  }

  return /*#__PURE__*/React.createElement(Box, {
    style: animatedStyle,
    overflow: "hidden"
  }, /*#__PURE__*/React.createElement(Box, _extends({
    overflow: "scroll"
  }, props)));
};

export default Collapse;
//# sourceMappingURL=index.js.map