'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var art = require('@wise/art');
var clsx = require('clsx');
var React = require('react');
var Body = require('../body/Body.js');
require('../common/theme.js');
require('../common/direction.js');
require('../common/propsValues/control.js');
require('../common/propsValues/breakpoint.js');
require('../common/propsValues/size.js');
var typography = require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
require('../common/propsValues/monthFormat.js');
require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
require('../common/propsValues/sentiment.js');
require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
var CloseButton = require('../common/closeButton/CloseButton.js');
var Link = require('../link/Link.js');
var Action = require('../common/action/Action.js');
var jsxRuntime = require('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] = React.useState(false);
  const [isMounted, setIsMounted] = React.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();
    }
  };
  React.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__*/jsxRuntime.jsxs("div", {
    className: clsx.clsx('wds-nudge', className),
    id: id,
    children: [!!mediaName && /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "wds-nudge-media",
      children: /*#__PURE__*/jsxRuntime.jsx(art.Illustration, {
        name: mediaName,
        className: clsx.clsx(`wds-nudge-media-${mediaName}`),
        size: "small",
        disablePadding: true,
        alt: ""
      })
    }), /*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "wds-nudge-container",
      children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "wds-nudge-content",
        children: [/*#__PURE__*/jsxRuntime.jsx(Body, {
          type: typography.Typography.BODY_LARGE,
          className: clsx.clsx('wds-nudge-body'),
          children: title
        }), link && /*#__PURE__*/jsxRuntime.jsx(Link, {
          href: href,
          type: typography.Typography.LINK_LARGE,
          className: "wds-nudge-link",
          onClick: onClick,
          children: link
        }), action && /*#__PURE__*/jsxRuntime.jsx(Action.Action, {
          action: action,
          className: "m-t-1"
        })]
      }), onDismiss || persistDismissal ? /*#__PURE__*/jsxRuntime.jsx(CloseButton.CloseButton, {
        className: "wds-nudge-control",
        size: "sm",
        onClick: handleOnDismiss
      }) : null]
    })]
  });
};

exports.STORAGE_NAME = STORAGE_NAME;
exports.default = Nudge;
//# sourceMappingURL=Nudge.js.map
