"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Snackbar_exports = {};
__export(Snackbar_exports, {
  default: () => Snackbar_default
});
module.exports = __toCommonJS(Snackbar_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_prop_types = require("prop-types");
var import_react_native = require("react-native");
var import_Box = __toESM(require("../../Box"));
var import_Button = __toESM(require("../../Button"));
var import_Icon = __toESM(require("../../Icon"));
var import_Text = __toESM(require("../../Text"));
var import_SnackbarAnimationWrapper = __toESM(require("./SnackbarAnimationWrapper"));
const SWIPE_THRESHOLD = 32;
const SnackbarContainer = import_styled_components.default.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 = (0, import_react.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 = (0, import_react.useRef)();
  const [currentProps, setCurrentProps] = (0, import_react.useState)({
    icon,
    message,
    actionLabel,
    onAction,
    variant
  });
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: () => {
      wrapperRef.current.open();
    },
    close: () => {
      wrapperRef.current.close();
    }
  }));
  (0, import_react.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 = import_react_native.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__ */ (0, import_jsx_runtime.jsx)(
    import_SnackbarAnimationWrapper.default,
    {
      onSnackbarClose,
      duration,
      ref: wrapperRef,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
        SnackbarContainer,
        __spreadProps(__spreadValues(__spreadValues({
          variant: currentProps.variant,
          bottomOffset,
          testID: dataTestId
        }, rest), panResponder.panHandlers), {
          children: [
            currentProps.icon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_Icon.default,
              {
                as: currentProps.icon,
                fill: "secondary",
                size: "large",
                marginRight: "xxsmall"
              }
            ),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_Text.default.Body2,
              {
                flex: 1,
                marginTop: "xxxsmall",
                marginBottom: "xxxsmall",
                numberOfLines: 2,
                children: currentProps.message
              }
            ),
            currentProps.actionLabel && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_Box.default,
              {
                as: import_Button.default.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: (0, import_prop_types.oneOf)(["success", "informative", "attention"]),
  /** Can be any icon of yoga-icons. */
  icon: import_prop_types.elementType,
  /** The message shown when snackbar is opened. The maximum number of lines is two. */
  message: import_prop_types.string,
  /** Label for a custom action. */
  actionLabel: import_prop_types.string,
  /** Function for the custom action. The `actionLabel` becomes required when passing this function. */
  onAction: import_prop_types.func,
  /** Callback function triggered by the Snackbar close action. Can be used for events, for example. */
  onSnackbarClose: import_prop_types.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: (0, import_prop_types.oneOf)(["fast", "default", "slow", "indefinite"]),
  /** Add extra margin to Snackbar. Can be useful for SafeAreaView or button cases. */
  bottomOffset: import_prop_types.number,
  /* A unique identifier for the Snackbar component, used for testing purposes with tools like Jest and Testing Library.  */
  dataTestId: import_prop_types.string
};
var Snackbar_default = Snackbar;
