"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_yoga_helpers = require("@gympass/yoga-helpers");
var import_yoga_icons = require("@gympass/yoga-icons");
var import_Theme = require("../../Theme");
var import_Box = __toESM(require("../../Box"));
var import_Button = __toESM(require("../../Button"));
var import_Icon = __toESM(require("../../Icon"));
var import_Text = __toESM(require("../../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 = import_styled_components.default.div`
  display: flex;
  align-items: center;

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

  cursor: pointer;

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

  svg {
    margin-left: ${import_Theme.theme.spacing.small}px;
    margin-right: ${import_Theme.theme.spacing.zero};
  }
`;
const StyledSnackbar = import_styled_components.default.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};
    `}

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

    min-width: ${import_Theme.theme.components.snackbar.minWidth.desktop}px;
  `}
`;
const fadeIn = import_styled_components.keyframes`
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
`;
const AnimatedSnackbar = (0, import_styled_components.default)(StyledSnackbar)`
  animation: ${fadeIn} 0.2s ease-in-out;
`;
const Snackbar = import_react.default.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 = (0, import_react.useRef)();
    (0, import_react.useEffect)(() => {
      const shouldCloseOnTimer = open && duration && onClose;
      if (shouldCloseOnTimer) {
        timeoutRef.current = setTimeout(() => {
          onClose();
        }, duration);
      }
      return () => clearTimeout(timeoutRef.current);
    }, [open, duration]);
    return open && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      AnimatedSnackbar,
      __spreadProps(__spreadValues({
        role: "alert",
        "aria-label": variant,
        variant,
        ref,
        "data-testid": dataTestId
      }, props), {
        children: [
          !hideIcon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_Box.default,
            {
              display: "flex",
              alignItems: "center",
              mr: "small",
              role: "img",
              "data-testid": "img",
              "aria-hidden": true,
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_Icon.default,
                {
                  as: snackbar.variant.icon[variant],
                  fill: "secondary",
                  width: "large",
                  height: "large"
                }
              )
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Text.default.Body2, { flex: 1, mr: "small", numberOfLines: 2, children: message }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ActionsWrapper, { children: [
            onAction && actionLabel && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Button.default.Link, { onClick: onAction, secondary: true, small: true, children: actionLabel }),
            !hideCloseButton && onClose && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              IconButtonWrapper,
              {
                role: "button",
                onClick: onClose,
                "aria-label": ariaLabelClose,
                as: "button",
                children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Icon.default, { as: import_yoga_icons.Close, fill: "secondary", size: "medium" })
              }
            )
          ] })
        ]
      })
    );
  }
);
Snackbar.propTypes = {
  /** Controls the snackbar visibility. */
  open: import_prop_types.bool,
  /** The message shown when snackbar is opened. */
  message: import_prop_types.string.isRequired,
  /** Function to close the snackbar. */
  onClose: import_prop_types.func.isRequired,
  /** A number in milliseconds to close snackbar automaticaly. */
  duration: import_prop_types.number,
  /** Label for a custom action. */
  actionLabel: import_prop_types.string,
  /** Controls the snackbar icon visibility. */
  hideIcon: import_prop_types.bool,
  /** Function for the custom action. The `actionLabel` becomes required when passing this function. */
  onAction: import_prop_types.func,
  /** The style variant, it may be "success", "failure" or "info". */
  variant: (0, import_prop_types.oneOf)(["success", "failure", "info"]),
  /** Hides the close button. */
  hideCloseButton: import_prop_types.bool,
  /** Custom aria label for close button. */
  ariaLabelClose: import_prop_types.string,
  /* 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 = (0, import_react.memo)((0, import_styled_components.withTheme)(Snackbar));
