'use strict';

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

var TableStatusText = require('./TableStatusText.js');
var StatusIcon = require('../statusIcon/StatusIcon.js');
var art = require('@wise/art');
require('react');
var clsx = require('clsx');
var Body = require('../body/Body.js');
var Money = require('../money/Money.js');
var AvatarView = require('../avatarView/AvatarView.js');
var jsxRuntime = require('react/jsx-runtime');

const TableCell = ({
  cell,
  alignment = 'left',
  className,
  colSpan,
  children
}) => {
  const getContentMedia = () => {
    let mediaContent = null;
    if (cell?.type === 'leading' && cell?.avatar) {
      mediaContent = /*#__PURE__*/jsxRuntime.jsx(AvatarView.default, {
        size: 40,
        ...cell.avatar,
        imgSrc: cell.avatar.imgSrc ?? cell.avatar.src,
        children: cell.avatar.asset
      });
    }
    if (cell?.type === 'currency' && cell?.primaryCurrency?.currency && cell?.flag !== false) {
      mediaContent = /*#__PURE__*/jsxRuntime.jsx(art.Flag, {
        code: cell?.primaryCurrency?.currency?.toLowerCase(),
        intrinsicSize: 24
      });
    }
    if (cell?.type === 'status') {
      mediaContent = /*#__PURE__*/jsxRuntime.jsx(StatusIcon.default, {
        size: 24,
        sentiment: cell?.sentiment ?? 'neutral'
      });
    }
    if (cell?.type === 'media' && cell.media) {
      // eslint-disable-next-line jsx-a11y/alt-text
      mediaContent = /*#__PURE__*/jsxRuntime.jsx("img", {
        ...cell.media
      });
    }
    if (mediaContent) {
      return /*#__PURE__*/jsxRuntime.jsx("div", {
        "aria-hidden": "true",
        className: "np-table-content-media",
        "data-testid": "np-table-content-media",
        children: mediaContent
      });
    }
  };
  const formatCurrencyValue = currency => {
    if (currency) {
      return /*#__PURE__*/jsxRuntime.jsx(Money.default, {
        amount: currency.amount,
        currency: currency.currency,
        alwaysShowDecimals: true
      });
    }
    return '';
  };
  return /*#__PURE__*/jsxRuntime.jsxs("td", {
    className: clsx.clsx('np-table-cell', cell?.type ? `np-table-cell--${cell?.type}` : '', `np-table-cell--${alignment}`, className),
    colSpan: colSpan,
    children: [cell?.type === 'text' && cell?.text && /*#__PURE__*/jsxRuntime.jsx(TableStatusText.default, {
      text: cell?.text,
      status: cell?.status
    }), cell?.type && ['leading', 'currency', 'status', 'media'].includes(cell?.type) && /*#__PURE__*/jsxRuntime.jsxs("div", {
      className: clsx.clsx('np-table-content', {
        'np-table-content--reversed': alignment === 'right'
      }),
      "data-testid": "np-table-content",
      children: [getContentMedia(), /*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "np-table-content-body",
        children: [(cell?.primaryCurrency ?? cell?.primaryText) && /*#__PURE__*/jsxRuntime.jsx(TableStatusText.default, {
          text: cell?.type === 'currency' ? formatCurrencyValue(cell?.primaryCurrency) : cell?.primaryText ?? '',
          status: cell?.type !== 'status' ? cell?.status : undefined,
          typography: "default-bold"
        }), (cell?.secondaryCurrency ?? cell?.secondaryText) && /*#__PURE__*/jsxRuntime.jsx(Body.default, {
          children: cell?.type === 'currency' ? formatCurrencyValue(cell?.secondaryCurrency) : cell?.secondaryText
        })]
      })]
    }), children]
  });
};

exports.default = TableCell;
//# sourceMappingURL=TableCell.js.map
