import { Check } from '@transferwise/icons';
import { clsx } from 'clsx';
import React__default, { forwardRef, useState, useId, useEffect } from 'react';
import Body from '../body/Body.mjs';
import Card from '../common/card/Card.mjs';
import Display from '../display/Display.mjs';
import Image from '../image/Image.mjs';
import Title from '../title/Title.mjs';
import { usePromoCardContext } from './PromoCardContext.mjs';
import PromoCardIndicator from './PromoCardIndicator.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';
import { Typography } from '../common/propsValues/typography.mjs';

const PromoCard = /*#__PURE__*/forwardRef(({
  className,
  description,
  defaultChecked,
  download,
  href,
  hrefLang,
  id,
  headingLevel = 'h3',
  imageAlt,
  imageClass,
  imageSource,
  indicatorLabel,
  indicatorIcon,
  isChecked,
  isDisabled,
  onClick,
  onKeyDown,
  rel,
  tabIndex,
  target,
  testId,
  title,
  type,
  value,
  isSmall,
  useDisplayFont = true,
  anchorRef,
  anchorId,
  ...props
}, ref) => {
  // Set the `checked` state to the value of `defaultChecked` if it is truthy,
  // or the value of `isChecked` if it is truthy, or `false` if neither
  // is truthy.
  const {
    state,
    onChange,
    isDisabled: contextIsDisabled
  } = usePromoCardContext();
  const [checked, setChecked] = useState(type === 'checkbox' ? defaultChecked ?? isChecked ?? false : false);
  const handleClick = () => {
    if (type === 'radio') {
      onChange(value || ''); // Update the context state for radio
    } else if (type === 'checkbox') {
      setChecked(!checked); // Update local state for checkbox
    }
  };
  const fallbackId = useId();
  const componentId = id || fallbackId;
  // Set the icon to `'arrow'` if `href` is truthy and `type` is falsy, or
  // `'download'` if `download` is truthy. If neither condition is true, set
  // `icon` to `undefined`.
  // Create a function to get icon type
  const getIconType = () => {
    if (indicatorIcon) {
      return indicatorIcon;
    }
    if (download) {
      return 'download';
    }
    if (href && !type) {
      return 'arrow';
    }
    return undefined;
  };
  // Define all class names string based on the values of the `href`, `type`,
  // `checked`, and `className` props.
  const commonClasses = clsx({
    'np-Card--promoCard': true,
    'np-Card--checked': !href && type,
    'np-Card--link': href && !type,
    'is-checked': type === 'radio' ? value === state : type === 'checkbox' ? checked : undefined
  }, className);
  // Object with common props that will be passed to the `Card` components
  const commonProps = {
    className: commonClasses,
    id: componentId,
    isDisabled: isDisabled || contextIsDisabled,
    onClick,
    onKeyDown,
    ref,
    'data-testid': testId,
    isSmall
  };
  // Object with Anchor props that will be passed to the `a` element. These
  // won't be refurned if set to `isDisabled`
  const anchorProps = href && !isDisabled ? {
    download,
    href: href || undefined,
    hrefLang,
    rel,
    target,
    ref: anchorRef,
    id: anchorId
  } : {};
  // Object of all Checked props that will be passed to the root `Card` component
  const checkedProps = (type === 'checkbox' || type === 'radio') && !href ? {
    ...commonProps,
    'aria-checked': type === 'radio' ? value === state : type === 'checkbox' ? checked : undefined,
    'aria-describedby': `${componentId}-title`,
    'aria-disabled': isDisabled,
    'data-value': value ?? undefined,
    role: type === 'checkbox' || type === 'radio' ? type : undefined,
    onClick: handleClick,
    onKeyDown: event => {
      if (event.key === 'Enter' || event.key === ' ') {
        handleClick();
      }
    },
    ref,
    tabIndex: 0
  } : {};
  const getTitle = () => {
    const titleContent = href && !type ? /*#__PURE__*/jsx("a", {
      className: "np-Card-titleLink",
      ...anchorProps,
      children: title
    }) : title;
    const titleProps = {
      id: `${componentId}-title`,
      as: headingLevel,
      className: 'np-Card-title'
    };
    return useDisplayFont ? /*#__PURE__*/jsx(Display, {
      type: Typography.DISPLAY_SMALL,
      ...titleProps,
      children: titleContent
    }) : /*#__PURE__*/jsx(Title, {
      type: Typography.TITLE_SUBSECTION,
      ...titleProps,
      children: titleContent
    });
  };
  useEffect(() => {
    setChecked(defaultChecked ?? isChecked ?? false);
  }, [defaultChecked, isChecked]);
  return /*#__PURE__*/jsxs(Card, {
    ...commonProps,
    ...checkedProps,
    ...props,
    children: [(value === state || checked) && /*#__PURE__*/jsx("span", {
      className: "np-Card-check",
      children: /*#__PURE__*/jsx(Check, {
        size: 24,
        "aria-hidden": "true"
      })
    }), getTitle(), /*#__PURE__*/jsx(Body, {
      className: "np-Card-description",
      children: description
    }), imageSource && /*#__PURE__*/jsx("div", {
      className: clsx('np-Card-image', {
        imageClass
      }),
      children: /*#__PURE__*/jsx(Image, {
        src: imageSource,
        alt: imageAlt || '',
        loading: "lazy"
      })
    }), /*#__PURE__*/jsx(PromoCardIndicator, {
      label: indicatorLabel,
      icon: getIconType(),
      isSmall: isSmall
    })]
  });
});
var PromoCard$1 = /*#__PURE__*/React__default.memo(PromoCard);

export { PromoCard$1 as default };
//# sourceMappingURL=PromoCard.mjs.map
