'use strict';

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

var reactIntl = require('react-intl');
var TableCell = require('./TableCell.js');
var TableHeader = require('./TableHeader.js');
var TableRow = require('./TableRow.js');
var Alert = require('../alert/Alert.js');
var Table_messages = require('./Table.messages.js');
var Loader = require('../loader/Loader.js');
require('../common/theme.js');
require('../common/direction.js');
require('../common/propsValues/control.js');
require('../common/propsValues/breakpoint.js');
var size = require('../common/propsValues/size.js');
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');
var sentiment = 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');
require('@transferwise/icons');
var clsx = require('clsx');
require('react');
require('../common/closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');
var StatusIcon = require('../statusIcon/StatusIcon.js');
var componentsTheming = require('@wise/components-theming');
var Body = require('../body/Body.js');

const Table = ({
  'aria-labelledby': ariaLabelledBy,
  data,
  loading,
  className,
  fullWidth = true,
  error
}) => {
  const {
    formatMessage
  } = reactIntl.useIntl();
  const {
    theme
  } = componentsTheming.useTheme();
  const isEmptyHeader = loading ?? (data?.headers && !data?.headers.length);
  const getRowLength = () => {
    const columnsLength = data?.headers?.length ?? 0;
    return data?.onRowClick ? columnsLength + 1 : columnsLength;
  };
  const getTableContent = () => {
    if (loading) {
      return /*#__PURE__*/jsxRuntime.jsx(TableRow.default, {
        children: /*#__PURE__*/jsxRuntime.jsx(TableCell.default, {
          children: /*#__PURE__*/jsxRuntime.jsx(Loader.default, {
            "data-testid": "np-table-loader"
          })
        })
      });
    }
    // Shows the `emptyData` message when there is no data to display
    if (!data?.rows?.length) {
      return /*#__PURE__*/jsxRuntime.jsx(TableRow.default, {
        children: /*#__PURE__*/jsxRuntime.jsx(TableCell.default, {
          colSpan: data?.headers?.length,
          children: /*#__PURE__*/jsxRuntime.jsxs("div", {
            className: "np-table-empty-data",
            "data-testid": "np-table-empty-data",
            children: [/*#__PURE__*/jsxRuntime.jsx(StatusIcon.default, {
              sentiment: sentiment.Sentiment.WARNING,
              size: size.Size.MEDIUM
            }), /*#__PURE__*/jsxRuntime.jsx(Body.default, {
              type: "body-default-bold",
              children: formatMessage(Table_messages.default.emptyData)
            })]
          })
        })
      });
    }
    return data?.rows?.map((rowData, rowIndex) => {
      return /*#__PURE__*/jsxRuntime.jsx(TableRow.default, {
        rowData: rowData,
        hasSeparator: data?.rows?.length ? data.rows.length - 1 !== rowIndex : false,
        onRowClick: data?.onRowClick
      }, 'table-row-'.concat(rowIndex.toString()));
    });
  };
  if (error) {
    return /*#__PURE__*/jsxRuntime.jsx(Alert.default, {
      className: "np-table-error",
      message: error.message,
      type: sentiment.Sentiment.NEGATIVE,
      action: {
        href: error?.action?.href ?? '/',
        text: error?.action?.text ?? formatMessage(Table_messages.default.refreshPage)
      },
      "data-testid": "np-table-error"
    });
  }
  return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
    children: [/*#__PURE__*/jsxRuntime.jsx("div", {
      "aria-live": "polite",
      className: "sr-only",
      children: formatMessage(Table_messages.default[loading ? 'loading' : 'loaded'])
    }), /*#__PURE__*/jsxRuntime.jsx("div", {
      role: loading ? 'presentation' : 'region',
      "aria-labelledby": ariaLabelledBy,
      className: clsx.clsx('np-table-outer-container', className, {
        'np-theme-personal': theme === 'bright-green',
        'np-table-outer-container--center': !fullWidth,
        'np-table-outer-container--full-width': fullWidth
      }),
      tabIndex: 0,
      "data-testid": "np-table-outer-container",
      children: /*#__PURE__*/jsxRuntime.jsx("div", {
        className: clsx.clsx('np-table-container', {
          'np-table-container--loading': loading
        }),
        "data-testid": "np-table-container",
        children: /*#__PURE__*/jsxRuntime.jsx("div", {
          className: "np-table-inner-container",
          children: /*#__PURE__*/jsxRuntime.jsxs("table", {
            className: "np-table",
            children: [/*#__PURE__*/jsxRuntime.jsx("thead", {
              "aria-hidden": isEmptyHeader,
              children: /*#__PURE__*/jsxRuntime.jsxs("tr", {
                children: [isEmptyHeader ? /*#__PURE__*/jsxRuntime.jsx(TableHeader.default, {}) : data?.headers?.map((headerItem, index) => /*#__PURE__*/jsxRuntime.jsx(TableHeader.default, {
                  ...headerItem
                }, headerItem.header?.concat(index.toString()))), data?.onRowClick && /*#__PURE__*/jsxRuntime.jsx(TableHeader.default, {
                  isActionHeader: true
                })]
              })
            }), /*#__PURE__*/jsxRuntime.jsxs("tbody", {
              children: [/*#__PURE__*/jsxRuntime.jsx("tr", {
                "aria-hidden": "true",
                className: "np-table-row np-table-row--cosmetic",
                children: /*#__PURE__*/jsxRuntime.jsx("td", {
                  className: "np-table-cell",
                  colSpan: getRowLength()
                })
              }, "first-np-table-row--cosmetic"), getTableContent(), /*#__PURE__*/jsxRuntime.jsx("tr", {
                "aria-hidden": "true",
                className: "np-table-row np-table-row--cosmetic",
                children: /*#__PURE__*/jsxRuntime.jsx("td", {
                  className: "np-table-cell",
                  colSpan: getRowLength()
                })
              }, "last-np-table-row--cosmetic")]
            })]
          })
        })
      })
    })]
  });
};

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