var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import { jsx, jsxs } from "react/jsx-runtime";
import React, {
  forwardRef,
  useEffect,
  useImperativeHandle,
  useRef,
  useState
} from "react";
import styled from "styled-components";
import { string, oneOf, func, elementType, number } from "prop-types";
import { PanResponder } from "react-native";
import Box from "../../Box";
import Button from "../../Button";
import Icon from "../../Icon";
import Text from "../../Text";
import SnackbarAnimationWrapper from "./SnackbarAnimationWrapper";
const SWIPE_THRESHOLD = 32;
const SnackbarContainer = styled.View`
  ${({
  bottomOffset,
  variant,
  theme: {
    yoga: {
      colors: { feedback },
      components: {
        snackbar: {
          padding,
          margin,
          variant: {
            color: { [variant]: backgroundColor = feedback.success.light }
          },
          border
        }
      }
    }
  }
}) => `
    padding: ${padding.vertical}px ${padding.horizontal}px;
    margin-horizontal: ${margin.horizontal}px
    margin-bottom: ${margin.bottom + bottomOffset}px;
    background-color: ${backgroundColor};
    border-radius: ${border.radius}px;
    flex-direction: row;
    align-items: center;
  `}
`;
const Snackbar = forwardRef((props, ref) => {
  const _a = props, {
    icon = void 0,
    message = "",
    actionLabel = void 0,
    onAction = void 0,
    variant = "success",
    onSnackbarClose = void 0,
    duration = "default",
    bottomOffset = 0,
    dataTestId = "snackbar"
  } = _a, rest = __objRest(_a, [
    "icon",
    "message",
    "actionLabel",
    "onAction",
    "variant",
    "onSnackbarClose",
    "duration",
    "bottomOffset",
    "dataTestId"
  ]);
  const wrapperRef = useRef();
  const [currentProps, setCurrentProps] = useState({
    icon,
    message,
    actionLabel,
    onAction,
    variant
  });
  useImperativeHandle(ref, () => ({
    open: () => {
      wrapperRef.current.open();
    },
    close: () => {
      wrapperRef.current.close();
    }
  }));
  useEffect(() => {
    if (wrapperRef.current && currentProps.message) {
      wrapperRef.current.close(() => {
        setCurrentProps({
          icon,
          message,
          actionLabel,
          onAction,
          variant
        });
        wrapperRef.current.open();
      });
    } else {
      setCurrentProps({
        icon,
        message,
        actionLabel,
        onAction,
        variant
      });
    }
  }, [icon, message, actionLabel, onAction, variant]);
  const handlePanResponderRelease = (_evt, gestureState) => {
    if (gestureState.dy > SWIPE_THRESHOLD) {
      wrapperRef.current.close();
    } else {
      wrapperRef.current.open();
    }
  };
  const panResponder = PanResponder.create({
    onPanResponderMove: (_, gestureState) => {
      if (gestureState.dy > 0) {
        wrapperRef.current.translateY(gestureState.dy);
      }
    },
    onPanResponderRelease: handlePanResponderRelease,
    onPanResponderTerminate: handlePanResponderRelease,
    onStartShouldSetPanResponder: () => true,
    onMoveShouldSetPanResponder: (_, gestureState) => {
      return !(gestureState.dy <= 2 && gestureState.dy >= -2);
    },
    onMoveShouldSetPanResponderCapture: () => true,
    onPanResponderTerminationRequest: () => true,
    onShouldBlockNativeResponder: () => true
  });
  const handleOnAction = () => {
    wrapperRef.current.close();
    if (currentProps.onAction) {
      currentProps.onAction();
    }
  };
  return /* @__PURE__ */ jsx(
    SnackbarAnimationWrapper,
    {
      onSnackbarClose,
      duration,
      ref: wrapperRef,
      children: /* @__PURE__ */ jsxs(
        SnackbarContainer,
        __spreadProps(__spreadValues(__spreadValues({
          variant: currentProps.variant,
          bottomOffset,
          testID: dataTestId
        }, rest), panResponder.panHandlers), {
          children: [
            currentProps.icon && /* @__PURE__ */ jsx(
              Icon,
              {
                as: currentProps.icon,
                fill: "secondary",
                size: "large",
                marginRight: "xxsmall"
              }
            ),
            /* @__PURE__ */ jsx(
              Text.Body2,
              {
                flex: 1,
                marginTop: "xxxsmall",
                marginBottom: "xxxsmall",
                numberOfLines: 2,
                children: currentProps.message
              }
            ),
            currentProps.actionLabel && /* @__PURE__ */ jsx(
              Box,
              {
                as: Button.Text,
                small: true,
                secondary: true,
                marginLeft: "xxsmall",
                onPress: handleOnAction,
                children: currentProps.actionLabel
              }
            )
          ]
        })
      )
    }
  );
});
Snackbar.propTypes = {
  /** The style variant change the color of the component, it may be "success", "informative" or "attention". */
  variant: oneOf(["success", "informative", "attention"]),
  /** Can be any icon of yoga-icons. */
  icon: elementType,
  /** The message shown when snackbar is opened. The maximum number of lines is two. */
  message: string,
  /** Label for a custom action. */
  actionLabel: string,
  /** Function for the custom action. The `actionLabel` becomes required when passing this function. */
  onAction: func,
  /** Callback function triggered by the Snackbar close action. Can be used for events, for example. */
  onSnackbarClose: func,
  /** The duration sets how long it will take to close snackbar automatically, it may be "fast" (4 seconds), "default" (8 seconds), "slow" (10 seconds) or "indefinite" (it doesn't close automatically). */
  duration: oneOf(["fast", "default", "slow", "indefinite"]),
  /** Add extra margin to Snackbar. Can be useful for SafeAreaView or button cases. */
  bottomOffset: number,
  /* A unique identifier for the Snackbar component, used for testing purposes with tools like Jest and Testing Library.  */
  dataTestId: string
};
var Snackbar_default = Snackbar;
export {
  Snackbar_default as default
};
