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, { memo, useEffect, useRef } from "react";
import styled, { keyframes, withTheme } from "styled-components";
import { func, string, bool, number, oneOf } from "prop-types";
import { media } from "@gympass/yoga-helpers";
import { Close } from "@gympass/yoga-icons";
import { theme } from "../../Theme";
import Box from "../../Box";
import Button from "../../Button";
import Icon from "../../Icon";
import Text from "../../Text";
function getMaxZIndex() {
  return Math.max(
    ...Array.from(
      document.querySelectorAll("body *"),
      (el) => parseFloat(window.getComputedStyle(el).zIndex)
    ).filter((zIndex) => !Number.isNaN(zIndex)),
    0
  );
}
const IconButtonWrapper = styled.div`
  display: flex;
  align-items: center;

  background: none;
  color: inherit;
  border: none;
  padding: 0;
  outline: inherit;

  cursor: pointer;

  &:hover {
    svg {
      fill: ${theme.colors.text.secondary};
    }
  }
`;
const ActionsWrapper = styled.aside`
  display: flex;
  align-items: center;

  svg {
    margin-left: ${theme.spacing.small}px;
    margin-right: ${theme.spacing.zero};
  }
`;
const StyledSnackbar = styled.div`
  ${({
  variant,
  theme: {
    yoga: {
      components: { snackbar }
    }
  }
}) => `
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-sizing: border-box;

      position: fixed;
      bottom: ${snackbar.position.mobile.bottom}px;
      right: ${snackbar.position.mobile.right}px;
      left: ${snackbar.position.mobile.left}px;

      min-width: ${snackbar.minWidth.mobile}px;
      max-width: ${snackbar.maxWidth.default}px;

      min-height: ${snackbar.height.min}px;
      max-height: ${snackbar.height.max}px;

      padding: ${snackbar.padding.default}px;

      border-radius: ${snackbar.border.radius}px;

      box-shadow: ${snackbar.shadow.default};

      background-color: ${snackbar.variant.color[variant]};

      z-index: ${getMaxZIndex() + 1};
    `}

  ${media.md`
    left: auto;
    bottom: ${theme.components.snackbar.position.desktop.bottom}px;
    right: ${theme.components.snackbar.position.desktop.right}px;

    min-width: ${theme.components.snackbar.minWidth.desktop}px;
  `}
`;
const fadeIn = keyframes`
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
`;
const AnimatedSnackbar = styled(StyledSnackbar)`
  animation: ${fadeIn} 0.2s ease-in-out;
`;
const Snackbar = React.forwardRef(
  (_a, ref) => {
    var _b = _a, {
      open = false,
      duration,
      variant = "success",
      hideIcon = false,
      message,
      actionLabel,
      onAction,
      onClose,
      hideCloseButton = false,
      ariaLabelClose,
      dataTestId = "snackbar",
      theme: {
        yoga: {
          components: { snackbar }
        }
      }
    } = _b, props = __objRest(_b, [
      "open",
      "duration",
      "variant",
      "hideIcon",
      "message",
      "actionLabel",
      "onAction",
      "onClose",
      "hideCloseButton",
      "ariaLabelClose",
      "dataTestId",
      "theme"
    ]);
    const timeoutRef = useRef();
    useEffect(() => {
      const shouldCloseOnTimer = open && duration && onClose;
      if (shouldCloseOnTimer) {
        timeoutRef.current = setTimeout(() => {
          onClose();
        }, duration);
      }
      return () => clearTimeout(timeoutRef.current);
    }, [open, duration]);
    return open && /* @__PURE__ */ jsxs(
      AnimatedSnackbar,
      __spreadProps(__spreadValues({
        role: "alert",
        "aria-label": variant,
        variant,
        ref,
        "data-testid": dataTestId
      }, props), {
        children: [
          !hideIcon && /* @__PURE__ */ jsx(
            Box,
            {
              display: "flex",
              alignItems: "center",
              mr: "small",
              role: "img",
              "data-testid": "img",
              "aria-hidden": true,
              children: /* @__PURE__ */ jsx(
                Icon,
                {
                  as: snackbar.variant.icon[variant],
                  fill: "secondary",
                  width: "large",
                  height: "large"
                }
              )
            }
          ),
          /* @__PURE__ */ jsx(Text.Body2, { flex: 1, mr: "small", numberOfLines: 2, children: message }),
          /* @__PURE__ */ jsxs(ActionsWrapper, { children: [
            onAction && actionLabel && /* @__PURE__ */ jsx(Button.Link, { onClick: onAction, secondary: true, small: true, children: actionLabel }),
            !hideCloseButton && onClose && /* @__PURE__ */ jsx(
              IconButtonWrapper,
              {
                role: "button",
                onClick: onClose,
                "aria-label": ariaLabelClose,
                as: "button",
                children: /* @__PURE__ */ jsx(Icon, { as: Close, fill: "secondary", size: "medium" })
              }
            )
          ] })
        ]
      })
    );
  }
);
Snackbar.propTypes = {
  /** Controls the snackbar visibility. */
  open: bool,
  /** The message shown when snackbar is opened. */
  message: string.isRequired,
  /** Function to close the snackbar. */
  onClose: func.isRequired,
  /** A number in milliseconds to close snackbar automaticaly. */
  duration: number,
  /** Label for a custom action. */
  actionLabel: string,
  /** Controls the snackbar icon visibility. */
  hideIcon: bool,
  /** Function for the custom action. The `actionLabel` becomes required when passing this function. */
  onAction: func,
  /** The style variant, it may be "success", "failure" or "info". */
  variant: oneOf(["success", "failure", "info"]),
  /** Hides the close button. */
  hideCloseButton: bool,
  /** Custom aria label for close button. */
  ariaLabelClose: string,
  /* A unique identifier for the Snackbar component, used for testing purposes with tools like Jest and Testing Library.  */
  dataTestId: string
};
var Snackbar_default = memo(withTheme(Snackbar));
export {
  Snackbar_default as default
};
