"use strict";

import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { StyleSheet, View, Animated } from 'react-native';
import { Shadow, Spacing } from "../Consts/index.js";
import { SizedBox } from "../SizedBox/index.js";
import { Toast } from "../Toast/index.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default /*#__PURE__*/forwardRef((_, ref) => {
  const toasts = useRef([]);
  const [update, setUpdate] = useState();
  useImperativeHandle(ref, () => ({
    showToast,
    hideToast
  }));

  /**
   * show loading
   * @param item
   */
  const showToast = item => {
    const key = Date.now();
    const opacity = new Animated.Value(0);
    const scale = new Animated.Value(0.8);

    // Animation for showing toast
    Animated.parallel([Animated.timing(opacity, {
      toValue: 1,
      duration: 300,
      useNativeDriver: true
    }), Animated.timing(scale, {
      toValue: 1,
      duration: 300,
      useNativeDriver: true
    })]).start();
    const hideWithAnimation = () => {
      Animated.parallel([Animated.timing(opacity, {
        toValue: 0,
        duration: 200,
        useNativeDriver: true
      }), Animated.timing(scale, {
        toValue: 0.8,
        duration: 200,
        useNativeDriver: true
      })]).start(() => {
        item.onDismiss?.();
        toasts.current = toasts.current.filter(data => data.key !== key);
        setUpdate(Date.now());
      });
    };
    const timeout = setTimeout(() => {
      hideWithAnimation();
    }, item.duration ?? 2000);
    const element = {
      item,
      job: timeout,
      key,
      opacity,
      scale,
      hideWithAnimation
    };
    toasts.current.push(element);
    setUpdate(Date.now());
  };

  /**
   * on dismiss a toast
   */
  const onDismiss = element => {
    clearTimeout(element.job);
    element.hideWithAnimation();
  };

  /**
   *hidden loading
   */
  const hideToast = () => {
    const animations = toasts.current.map(toast => {
      return Animated.parallel([Animated.timing(toast.opacity, {
        toValue: 0,
        duration: 200,
        useNativeDriver: true
      }), Animated.timing(toast.scale, {
        toValue: 0.8,
        duration: 200,
        useNativeDriver: true
      })]);
    });
    if (animations.length > 0) {
      Animated.parallel(animations).start(() => {
        toasts.current = [];
        setUpdate(Date.now());
      });
    } else {
      toasts.current = [];
      setUpdate(Date.now());
    }
  };
  if (toasts.current.length > 0) {
    return /*#__PURE__*/_jsx(View, {
      style: [styles.container, Shadow.dark],
      children: toasts.current.map((data, index) => {
        return /*#__PURE__*/_jsxs(Animated.View, {
          style: {
            opacity: data.opacity,
            transform: [{
              scale: data.scale
            }]
          },
          children: [index !== 0 && /*#__PURE__*/_jsx(SizedBox, {
            height: Spacing.S
          }), /*#__PURE__*/_jsx(Toast, {
            ...data.item,
            action: {
              ...data.item.action,
              onPress: () => {
                data.item.action?.onPress?.();
                onDismiss(data);
              }
            }
          })]
        }, `Toast${index}`);
      })
    }, `Toast${update}`);
  }
  return null;
});
const styles = StyleSheet.create({
  container: {
    position: 'absolute',
    width: '100%',
    padding: Spacing.M,
    bottom: 80
  }
});
//# sourceMappingURL=ToastView.js.map