import { Illustration } from '@wise/art';
import { clsx } from 'clsx';
import { useState, useEffect } from 'react';
import Body from '../body/Body.mjs';
import '../common/theme.mjs';
import '../common/direction.mjs';
import '../common/propsValues/control.mjs';
import '../common/propsValues/breakpoint.mjs';
import '../common/propsValues/size.mjs';
import { Typography } from '../common/propsValues/typography.mjs';
import '../common/propsValues/width.mjs';
import '../common/propsValues/type.mjs';
import '../common/propsValues/dateMode.mjs';
import '../common/propsValues/monthFormat.mjs';
import '../common/propsValues/position.mjs';
import '../common/propsValues/layouts.mjs';
import '../common/propsValues/status.mjs';
import '../common/propsValues/sentiment.mjs';
import '../common/propsValues/profileType.mjs';
import '../common/propsValues/variant.mjs';
import '../common/propsValues/scroll.mjs';
import '../common/propsValues/markdownNodeType.mjs';
import '../common/fileType.mjs';
import { CloseButton } from '../common/closeButton/CloseButton.mjs';
import Link from '../link/Link.mjs';
import { Action } from '../common/action/Action.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';

const STORAGE_NAME = 'dismissedNudges';
const getLocalStorage = () => {
  try {
    const storageItem = localStorage.getItem(STORAGE_NAME);
    if (storageItem) {
      const storage = JSON.parse(storageItem);
      if (Array.isArray(storage)) {
        return storage.map(item => String(item));
      }
    }
    // eslint-disable-next-line unicorn/prefer-optional-catch-binding, no-empty
  } catch (error) {}
  return [];
};
const Nudge = ({
  media,
  mediaName,
  title,
  link,
  href,
  onClick,
  onDismiss,
  persistDismissal,
  isPreviouslyDismissed,
  id,
  className,
  action
}) => {
  const [isDismissed, setIsDismissed] = useState(false);
  const [isMounted, setIsMounted] = useState(false);
  const handleOnDismiss = () => {
    const dismissedNudgesStorage = getLocalStorage();
    if (persistDismissal && id) {
      try {
        localStorage.setItem(STORAGE_NAME, JSON.stringify([...dismissedNudgesStorage, id]));
        // eslint-disable-next-line unicorn/prefer-optional-catch-binding, no-empty
      } catch (error) {}
      setIsDismissed(true);
    }
    if (onDismiss) {
      onDismiss();
    }
  };
  useEffect(() => {
    if (persistDismissal && id) {
      const dismissedNudgesStorage = getLocalStorage();
      let isDismissed = false;
      if (dismissedNudgesStorage?.find(item => item === id)) {
        setIsDismissed(true);
        isDismissed = true;
      }
      if (isPreviouslyDismissed) {
        isPreviouslyDismissed(isDismissed);
      }
    }
    setIsMounted(true);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [id, persistDismissal]);
  if (persistDismissal && (isDismissed || !isMounted)) {
    return null;
  }
  return /*#__PURE__*/jsxs("div", {
    className: clsx('wds-nudge', className),
    id: id,
    children: [!!mediaName && /*#__PURE__*/jsx("div", {
      className: "wds-nudge-media",
      children: /*#__PURE__*/jsx(Illustration, {
        name: mediaName,
        className: clsx(`wds-nudge-media-${mediaName}`),
        size: "small",
        disablePadding: true,
        alt: ""
      })
    }), /*#__PURE__*/jsxs("div", {
      className: "wds-nudge-container",
      children: [/*#__PURE__*/jsxs("div", {
        className: "wds-nudge-content",
        children: [/*#__PURE__*/jsx(Body, {
          type: Typography.BODY_LARGE,
          className: clsx('wds-nudge-body'),
          children: title
        }), link && /*#__PURE__*/jsx(Link, {
          href: href,
          type: Typography.LINK_LARGE,
          className: "wds-nudge-link",
          onClick: onClick,
          children: link
        }), action && /*#__PURE__*/jsx(Action, {
          action: action,
          className: "m-t-1"
        })]
      }), onDismiss || persistDismissal ? /*#__PURE__*/jsx(CloseButton, {
        className: "wds-nudge-control",
        size: "sm",
        onClick: handleOnDismiss
      }) : null]
    })]
  });
};

export { STORAGE_NAME, Nudge as default };
//# sourceMappingURL=Nudge.mjs.map
