function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 * as React from "react";
import { Animated, SafeAreaView, StyleSheet, Easing } from "react-native";
import Button from "../Button";
import Surface from "../Surface";
import Text from "../Typography/Text";
import { withTheme } from "../../core/theming";
import { colors } from "../../styles/tokens";
import IconButton from "../IconButton/IconButton";
import color from "color";
const DURATION_SHORT = 4000;
const DURATION_MEDIUM = 7000;
const DURATION_LONG = 10000;
const Toast = _ref => {
  let {
    visible,
    onPress: onPressAction,
    duration = DURATION_MEDIUM,
    onDismiss,
    children,
    wrapperStyle,
    style,
    theme,
    ...rest
  } = _ref;
  const {
    current: opacity
  } = React.useRef(new Animated.Value(0.0));
  const [hidden, setHidden] = React.useState(!visible);
  const hideTimeout = React.useRef(undefined);
  const {
    scale
  } = theme.animation;
  React.useEffect(() => {
    return () => {
      if (hideTimeout.current) clearTimeout(hideTimeout.current);
    };
  }, []);
  React.useLayoutEffect(() => {
    if (visible) {
      // show
      if (hideTimeout.current) clearTimeout(hideTimeout.current);
      setHidden(false);
      Animated.timing(opacity, {
        toValue: 1,
        duration: 200 * scale,
        easing: Easing.out(Easing.ease),
        useNativeDriver: true
      }).start(_ref2 => {
        let {
          finished
        } = _ref2;
        if (finished) {
          const isInfinity = duration === Number.POSITIVE_INFINITY || duration === Number.NEGATIVE_INFINITY;
          if (finished && !isInfinity) {
            hideTimeout.current = setTimeout(onDismiss, duration);
          }
        }
      });
    } else {
      // hide
      if (hideTimeout.current) clearTimeout(hideTimeout.current);
      Animated.timing(opacity, {
        toValue: 0,
        duration: 100 * scale,
        useNativeDriver: true
      }).start(_ref3 => {
        let {
          finished
        } = _ref3;
        if (finished) setHidden(true);
      });
    }
  }, [visible, duration, opacity, scale, onDismiss]);
  if (hidden) return null;
  const marginRight = 16;
  const textColor = colors.white;
  const getColorForToast = () => {
    switch (rest.color) {
      case "primary":
        return theme.colors.primary.default;
      case "secondary":
        return theme.colors.secondary.default;
      case "success":
        return theme.colors.success.default;
      case "warning":
        return theme.colors.warning.default;
      case "error":
        return theme.colors.error.default;
      case "info":
        return theme.colors.info.default;
      case "light":
        return colors.gray100;
      case "dark":
        return colors.gray800;
      default:
        return theme.colors.primary.default;
    }
  };
  return /*#__PURE__*/React.createElement(SafeAreaView, {
    pointerEvents: "box-none",
    style: [styles.wrapper, wrapperStyle]
  }, /*#__PURE__*/React.createElement(Surface, _extends({
    pointerEvents: "box-none",
    accessibilityLiveRegion: "polite",
    style: [styles.elevation, styles.container, {
      borderRadius: 8,
      opacity: opacity,
      transform: [{
        scale: visible ? opacity.interpolate({
          inputRange: [0, 1],
          outputRange: [0.9, 1]
        }) : 1
      }]
    }, {
      backgroundColor: getColorForToast()
    }, style],
    elevation: 2
  }, rest), /*#__PURE__*/React.createElement(Text, {
    style: [styles.content, {
      marginRight,
      color: colors.white
    }]
  }, children), /*#__PURE__*/React.createElement(IconButton, {
    icon: "close",
    iconColor: color(getColorForToast()).isDark() ? "white" : "black",
    size: 20,
    onPress: onDismiss,
    containerColor: "transparent"
  }), typeof onPressAction === "function" ? /*#__PURE__*/React.createElement(Button
  // onPress={() => {
  //   onPressAction?.();
  //   onDismiss();
  // }}
  , {
    style: [styles.button],
    textColor: textColor,
    compact: true,
    mode: "text"
  }, "Close") : null));
};

/**
 * Show the Toast for a short duration.
 */
Toast.DURATION_SHORT = DURATION_SHORT;

/**
 * Show the Toast for a medium duration.
 */
Toast.DURATION_MEDIUM = DURATION_MEDIUM;

/**
 * Show the Toast for a long duration.
 */
Toast.DURATION_LONG = DURATION_LONG;
const styles = StyleSheet.create({
  wrapper: {
    position: "absolute",
    bottom: 0,
    width: "100%"
  },
  container: {
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "center",
    margin: 8,
    borderRadius: 4
  },
  content: {
    marginLeft: 16,
    marginVertical: 14,
    flexWrap: "wrap",
    flex: 1
  },
  button: {
    marginHorizontal: 8,
    marginVertical: 6
  },
  elevation: {
    elevation: 6
  }
});
export default withTheme(Toast);
//# sourceMappingURL=Toast.js.map