import { jsx } from "react/jsx-runtime";
import React, {
  forwardRef,
  useEffect,
  useImperativeHandle,
  useRef,
  useState
} from "react";
import { Animated, useWindowDimensions, View } from "react-native";
import { func, node, oneOf } from "prop-types";
import styled from "styled-components";
const durationDictionary = {
  fast: 4e3,
  default: 8e3,
  slow: 1e4,
  indefinite: -1
};
const Wrapper = styled(Animated.View)(
  () => `
    position: absolute;
    width: 100%;
    bottom: 0px;
  `
);
const SnackbarAnimationWrapper = forwardRef(
  ({ onSnackbarClose = void 0, children, duration }, ref) => {
    const { height: windowHeight } = useWindowDimensions();
    const [childrenHeight, setChildrenHeight] = useState();
    const translateY = useRef(new Animated.Value(windowHeight)).current;
    const timeoutRef = useRef();
    const childrenRef = useRef();
    const openAnimation = () => {
      Animated.spring(translateY, {
        toValue: 0,
        bounciness: 0,
        useNativeDriver: true
      }).start();
    };
    const closeAnimation = (callback) => {
      if (childrenHeight) {
        Animated.spring(translateY, {
          toValue: childrenHeight,
          bounciness: 0,
          useNativeDriver: true
        }).start(({ finished }) => {
          if (finished && callback) {
            callback();
          }
        });
        if (onSnackbarClose) {
          onSnackbarClose();
        }
      }
    };
    const setupSnackbarTimeout = () => {
      const timeoutDuration = durationDictionary[duration];
      const shouldCloseOnTimer = childrenHeight && timeoutDuration > 0;
      if (shouldCloseOnTimer) {
        timeoutRef.current = setTimeout(() => {
          clearTimeout(timeoutRef.current);
          closeAnimation();
        }, timeoutDuration);
      }
    };
    const openSnackbar = () => {
      openAnimation();
      setupSnackbarTimeout();
    };
    const closeSnackbar = (callback) => {
      clearTimeout(timeoutRef.current);
      closeAnimation(callback);
    };
    useImperativeHandle(ref, () => ({
      open: () => {
        openSnackbar();
      },
      close: (callback) => {
        closeSnackbar(callback);
      },
      translateY: (dy) => {
        translateY.setValue(dy);
      }
    }));
    useEffect(() => () => clearTimeout(timeoutRef.current), []);
    return /* @__PURE__ */ jsx(Wrapper, { style: { zIndex: 10, transform: [{ translateY }] }, children: /* @__PURE__ */ jsx(
      View,
      {
        testID: "wrapper",
        ref: childrenRef,
        onLayout: ({
          nativeEvent: {
            layout: { height }
          }
        }) => {
          setChildrenHeight(height);
          translateY.setValue(height);
        },
        children
      }
    ) });
  }
);
SnackbarAnimationWrapper.propTypes = {
  onSnackbarClose: func,
  children: node.isRequired,
  duration: oneOf(["fast", "default", "slow", "indefinite"]).isRequired
};
var SnackbarAnimationWrapper_default = SnackbarAnimationWrapper;
export {
  SnackbarAnimationWrapper_default as default
};
