"use strict";
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const assert = require("assert");
const styles_lib_components_Stack_Stack_cjs = require("../Stack/Stack.cjs");
const styles_lib_components_Accordion_AccordionItem_cjs = require("../Accordion/AccordionItem.cjs");
const styles_lib_components_Columns_Columns_cjs = require("../Columns/Columns.cjs");
const styles_lib_components_Column_Column_cjs = require("../Column/Column.cjs");
const styles_lib_components_ContentBlock_ContentBlock_cjs = require("../ContentBlock/ContentBlock.cjs");
const styles_lib_components_Box_Box_cjs = require("../Box/Box.cjs");
const styles_lib_components_private_Placeholder_Placeholder_cjs = require("../private/Placeholder/Placeholder.cjs");
const styles_lib_components_ButtonIcon_ButtonIcon_cjs = require("../ButtonIcon/ButtonIcon.cjs");
const styles_lib_css_lineHeightContainer_css_cjs = require("../../css/lineHeightContainer.css.cjs");
const styles_lib_components_Accordion_Accordion_cjs = require("../Accordion/Accordion.cjs");
const styles_lib_components_useToast_Toast_css_cjs = require("./Toast.css.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultCompat(React);
const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert);
const useTimeout = ({ onTimeout, duration }) => {
  const [activated, setActivated] = React.useState(true);
  const timeoutRef = React.useRef();
  const stopTimeout = React.useCallback(() => {
    window.clearTimeout(timeoutRef.current);
    setActivated(false);
  }, []);
  React.useEffect(() => {
    if (activated) {
      timeoutRef.current = window.setTimeout(() => {
        onTimeout();
      }, duration);
      return () => {
        stopTimeout();
      };
    }
  }, [onTimeout, activated, duration, stopTimeout]);
  const startTimeout = React.useCallback(() => {
    setActivated(true);
  }, []);
  return {
    stopTimeout,
    startTimeout
  };
};
const toneToIcon = {
  critical: styles_lib_components_Accordion_AccordionItem_cjs.IconCritical,
  positive: styles_lib_components_Accordion_AccordionItem_cjs.IconPositive
};
const toastDuration = 1e4;
const borderRadius = "large";
const Action = ({ label, onClick, removeToast }) => {
  const handleClick = React.useCallback(() => {
    removeToast();
    onClick();
  }, [removeToast, onClick]);
  return /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Accordion_AccordionItem_cjs.Text, { baseline: false, children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { component: "span", "aria-hidden": true, children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_private_Placeholder_Placeholder_cjs.TextLinkButton, { onClick: handleClick, hitArea: "large", children: label }) }) });
};
const ToastIcon = ({ tone, icon }) => {
  if (tone !== "neutral") {
    const Icon = toneToIcon[tone];
    return /* @__PURE__ */ jsxRuntime.jsx(Icon, { tone });
  }
  if (icon) {
    return React.cloneElement(icon, { tone });
  }
  return null;
};
const Toast = React__default.default.forwardRef(
  ({
    id,
    vanillaTheme,
    dedupeKey,
    message,
    description,
    tone,
    icon,
    onClose,
    closeLabel = "Close",
    action,
    shouldRemove,
    data,
    ...restProps
  }, ref) => {
    const remove = React.useCallback(
      () => onClose(dedupeKey, id),
      [onClose, dedupeKey, id]
    );
    const { stopTimeout, startTimeout } = useTimeout({
      duration: toastDuration,
      onTimeout: remove
    });
    React.useEffect(() => {
      if (shouldRemove) {
        stopTimeout();
        remove();
      }
    }, [shouldRemove, remove, stopTimeout]);
    assert__default.default(
      !icon || icon.props.size === void 0 && icon.props.tone === void 0,
      "Icons cannot set the 'size' or 'tone' prop when passed to a Toast component"
    );
    assert__default.default(
      !icon || icon && tone === "neutral",
      `Icons cannot be customised on a Toast component using '${tone}' tone`
    );
    const content = description ? /* @__PURE__ */ jsxRuntime.jsxs(styles_lib_components_Stack_Stack_cjs.Stack, { space: "xxsmall", children: [
      /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Accordion_AccordionItem_cjs.Text, { weight: "medium", tone, baseline: false, children: message }),
      description ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Accordion_AccordionItem_cjs.Text, { baseline: false, tone: "secondary", children: description }) : null,
      action ? /* @__PURE__ */ jsxRuntime.jsx(Action, { removeToast: remove, ...action }, action.label) : null
    ] }) : /* @__PURE__ */ jsxRuntime.jsxs(styles_lib_components_Accordion_AccordionItem_cjs.Inline, { space: "xxsmall", children: [
      /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { paddingRight: "medium", children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Accordion_AccordionItem_cjs.Text, { weight: "medium", tone, baseline: false, children: message }) }),
      action ? /* @__PURE__ */ jsxRuntime.jsx(Action, { removeToast: remove, ...action }, action.label) : null
    ] });
    return /* @__PURE__ */ jsxRuntime.jsx(
      styles_lib_components_Box_Box_cjs.Box,
      {
        display: "flex",
        justifyContent: "center",
        role: "alert",
        ref,
        onMouseEnter: stopTimeout,
        onMouseLeave: startTimeout,
        className: vanillaTheme,
        ...styles_lib_components_Accordion_Accordion_cjs.buildDataAttributes({ data, validateRestProps: restProps }),
        children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { boxShadow: "large", borderRadius, children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_ContentBlock_ContentBlock_cjs.ContentBlock, { width: "xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Box_Box_cjs.Box,
          {
            background: { lightMode: "surfaceDark", darkMode: "surface" },
            position: "relative",
            borderRadius,
            paddingY: "medium",
            paddingX: "gutter",
            overflow: "hidden",
            className: styles_lib_components_useToast_Toast_css_cjs.toast,
            children: /* @__PURE__ */ jsxRuntime.jsxs(styles_lib_components_Columns_Columns_cjs.Columns, { space: "none", children: [
              tone !== "neutral" || tone === "neutral" && icon ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Column_Column_cjs.Column, { width: "content", children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { paddingRight: "small", children: /* @__PURE__ */ jsxRuntime.jsx(ToastIcon, { tone, icon }) }) }) : null,
              /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Column_Column_cjs.Column, { children: content }),
              /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Column_Column_cjs.Column, { width: "content", children: /* @__PURE__ */ jsxRuntime.jsx(
                styles_lib_components_Box_Box_cjs.Box,
                {
                  width: "touchable",
                  display: "flex",
                  justifyContent: "flexEnd",
                  alignItems: "center",
                  className: styles_lib_css_lineHeightContainer_css_cjs.lineHeightContainer.standard,
                  "aria-hidden": true,
                  children: /* @__PURE__ */ jsxRuntime.jsx(
                    styles_lib_components_ButtonIcon_ButtonIcon_cjs.ButtonIcon,
                    {
                      id: `${dedupeKey}-clear`,
                      icon: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Accordion_AccordionItem_cjs.IconClear, {}),
                      tone: "secondary",
                      variant: "transparent",
                      onClick: remove,
                      label: closeLabel,
                      data: process.env.NODE_ENV !== "production" ? { testid: "clearToast" } : {}
                    }
                  )
                }
              ) })
            ] })
          }
        ) }) })
      }
    );
  }
);
const ToastComponent = Toast;
exports.ToastComponent = ToastComponent;
