import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["color", "expanded", "containerRef", "onExpand", "onCollapse"];

/* eslint-disable max-len */
import * as React from 'react';
import Box from '../../box/Box';
import Text from '../../text/Text';
import Button from '../../buttons/Button';
import Transition from '../Transition';
import Stage from './common/Stage';
import { useIsomorphicLayoutEffect } from '../../utils/useIsomorphicLayoutEffect';
import { useTransformationState } from './common/useTransformationState';
export var ExpandingDetails = function ExpandingDetails() {
  var containerRef = React.useRef(null);

  var _React$useState = React.useState(null),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      expandedIndex = _React$useState2[0],
      setExpandedIndex = _React$useState2[1];

  return /*#__PURE__*/React.createElement(Stage, {
    ref: containerRef,
    className: "sg-space-y-xs",
    format: "portrait"
  }, exampleData.map(function (dataProps, index) {
    return /*#__PURE__*/React.createElement(ExpandableBox, _extends({
      key: index,
      expanded: index === expandedIndex,
      containerRef: containerRef,
      onExpand: function onExpand() {
        return setExpandedIndex(index);
      },
      onCollapse: function onCollapse() {
        return setExpandedIndex(null);
      }
    }, dataProps));
  }));
};

var ExpandableBox = function ExpandableBox(_ref) {
  var color = _ref.color,
      expanded = _ref.expanded,
      containerRef = _ref.containerRef,
      onExpand = _ref.onExpand,
      onCollapse = _ref.onCollapse,
      contentProps = _objectWithoutProperties(_ref, _excluded);

  var _React$useState3 = React.useState(false),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      animating = _React$useState4[0],
      setAnimating = _React$useState4[1];

  var _React$useState5 = React.useState({
    expandingBox: null,
    appearingContent: null
  }),
      _React$useState6 = _slicedToArray(_React$useState5, 2),
      effects = _React$useState6[0],
      setEffects = _React$useState6[1];

  var elementRef = React.useRef(null);
  var transformation = useTransformationState({
    elementRef: elementRef,
    containerRef: containerRef,
    updateKey: expanded
  });
  var wrapperFixedHeight = React.useRef();

  var handleExpand = function handleExpand() {
    onExpand();

    if (elementRef.current) {
      wrapperFixedHeight.current = elementRef.current.clientHeight;
    }
  };

  var handleTransitionStart = function handleTransitionStart() {
    setAnimating(true);
  };

  var handleTransitionEnd = function handleTransitionEnd() {
    setAnimating(false);

    if (!expanded) {
      wrapperFixedHeight.current = 'auto';
    }
  };

  useIsomorphicLayoutEffect(function () {
    var _transformation$previ, _transformation$curre;

    if (transformation.diffHeight === 0) {
      return;
    }
    /**
     * https://css-tricks.com/animating-layouts-with-the-flip-technique/
     */


    setEffects({
      expandingBox: {
        initial: {
          transform: {
            translateY: -transformation.diffTop
          },
          height: (_transformation$previ = transformation.previousSnapshot) === null || _transformation$previ === void 0 ? void 0 : _transformation$previ.height
        },
        animate: {
          transform: {
            translateY: 0,
            origin: 'left top'
          },
          height: (_transformation$curre = transformation.currentSnapshot) === null || _transformation$curre === void 0 ? void 0 : _transformation$curre.height,
          duration: 'gentle1'
        }
      },
      appearingContent: {
        initial: {
          opacity: 0
        },
        animate: {
          opacity: 1,
          duration: 'gentle2',
          easing: 'entry'
        }
      }
    });
  }, [transformation]);
  return /*#__PURE__*/React.createElement("div", {
    style: {
      height: wrapperFixedHeight.current
    }
  }, /*#__PURE__*/React.createElement("div", {
    ref: elementRef,
    style: getElementStyle(expanded, animating)
  }, /*#__PURE__*/React.createElement(Transition, {
    active: true,
    effect: effects.expandingBox,
    onTransitionStart: handleTransitionStart,
    onTransitionEnd: handleTransitionEnd
  }, /*#__PURE__*/React.createElement(Box, _extends({}, getBoxStylingProps(color), {
    onClick: expanded ? undefined : handleExpand
  }), /*#__PURE__*/React.createElement(Transition, {
    active: true,
    effect: effects.appearingContent,
    fillMode: "backwards",
    delay: 80
  }, /*#__PURE__*/React.createElement(ExpandableBoxContent, _extends({}, contentProps, {
    expanded: expanded,
    onButtonClick: onCollapse
  })))))));
};

var ExpandableBoxContent = function ExpandableBoxContent(_ref2) {
  var expanded = _ref2.expanded,
      shortContent = _ref2.shortContent,
      fullContent = _ref2.fullContent,
      onButtonClick = _ref2.onButtonClick;

  if (!expanded) {
    return /*#__PURE__*/React.createElement(Text, {
      size: "small"
    }, shortContent);
  }

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    size: "small",
    className: "sg-space-y-m"
  }, fullContent), /*#__PURE__*/React.createElement(Button, {
    variant: "solid",
    style: {
      position: 'absolute',
      left: 24,
      bottom: 24,
      right: 24
    },
    onClick: onButtonClick
  }, "close"));
};

var exampleData = [{
  color: 'red',
  shortContent: 'A hive of bees contains 27 bees when it is first discovered. After 3 days, there are 36 bees. It is determined that the population of bees increases exponentially.',
  fullContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    weight: "bold",
    inherited: true
  }, "A hive of bees contains 27 bees when it is first discovered. After 3 days, there are 36 bees. It is determined that the population of bees increases exponentially."), /*#__PURE__*/React.createElement(Text, {
    inherited: true
  }, "How many bees are will there be after 30 days? Express your answer as an exact value and rounded to the nearest whole number."), /*#__PURE__*/React.createElement(Text, {
    inherited: true
  }, "360. When the beehive was discovered, there were 27 bees. After 3 days, the numbers of bee increased to 36. Progression Type: Exponential (also knows as geometry). Number of bees after 30 days is unknown."))
}, {
  color: 'yellow',
  shortContent: 'Louis Napoleon Bonaparte was the nephew of Napoleon Bonaparte.',
  fullContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    weight: "bold",
    inherited: true
  }, "Louis Napoleon Bonaparte was the nephew of Napoleon Bonaparte."), /*#__PURE__*/React.createElement(Text, {
    inherited: true
  }, "Napoleon III, the nephew of Napoleon I, was the first elected President of France from 1848 to 1852. Louis Napoleon seized power in 1851 and served as Emperor of the French from 1852 to 1870. He founded the Second French Empire and was its only emperor until the French army was defeated and he was captured by Prussia and its allies in the Franco-Prussian War in 1870."), /*#__PURE__*/React.createElement(Text, {
    inherited: true
  }, "He aimed to modernize the French economy, rebuilt the center of Paris, expanded the overseas empire."))
}, {
  color: 'blue',
  shortContent: 'Biology is a branch of science that studies about life and living organisms. It includes physical and chemical structure, development, and evolution of all living things. It seeks to answer questions about all living things.',
  fullContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
    weight: "bold",
    inherited: true
  }, "Biology is a branch of science that studies about life and living organisms. It includes physical and chemical structure, development, and evolution of all living things. It seeks to answer questions about all living things."), /*#__PURE__*/React.createElement(Text, {
    inherited: true
  }, "A biologist may ask about how plants maintain life processes. In Christian biology, biblical knowledge and faith are integrated in learning the evolution of the earth and the living things that consist it. Likewise, it states that God's signature is written in his creation or the living world is a testament of God's actions."))
}];

var getElementStyle = function getElementStyle(expanded, animating) {
  if (expanded) {
    return {
      position: 'absolute',
      zIndex: 1,
      top: 16,
      left: 16,
      right: 16,
      bottom: 16
    };
  }

  if (animating) {
    return {
      height: '100%',
      position: 'relative',
      zIndex: 1
    };
  }

  return {
    cursor: 'pointer'
  };
};

var getBoxStylingProps = function getBoxStylingProps(color) {
  var colorsMap = {
    red: {
      backgroundColor: 'red-30',
      borderColor: 'red-40'
    },
    yellow: {
      backgroundColor: 'yellow-20',
      borderColor: 'yellow-40'
    },
    blue: {
      backgroundColor: 'blue-30',
      borderColor: 'blue-40'
    }
  };
  return {
    style: {
      height: '100%',
      position: 'relative',
      overflow: 'hidden'
    },
    color: colorsMap[color].backgroundColor,
    borderColor: colorsMap[color].borderColor,
    border: true
  };
};

ExpandingDetails.parameters = {
  layout: 'centered'
};