"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Tr = exports.Th = exports.Td = exports.THead = exports.TBody = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _get = _interopRequireDefault(require("lodash-es/get"));
var _loadingIcon = require("../loading-icon");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// eslint-disable-next-line you-dont-need-lodash-underscore/get

/** Custom table. */
function createComponent(compClass, displayName) {
  const comp = ({
    children,
    className,
    testId
  }) => /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)(compClass, className),
    "test-id": testId,
    children: children
  });
  comp.displayName = displayName;
  return comp;
}
const THead = exports.THead = createComponent('r-thead', 'THead');
const TBody = exports.TBody = createComponent('r-tbody', 'TBody');
const Tr = exports.Tr = createComponent('r-tr', 'Tr');
const Th = exports.Th = createComponent('r-th', 'Th');
const Td = exports.Td = createComponent('r-td', 'Td');
function renderHeader(column, data, rowProps) {
  return typeof column.header === 'function' ? column.header(data, rowProps) : column.header;
}
function renderCell(column, item, index, rowProps) {
  return column.cell ? /*#__PURE__*/_react.default.createElement(column.cell, {
    row: item,
    value: (0, _get.default)(item, column?.accessor),
    index,
    accessor: column.accessor,
    ...rowProps
  }) : (0, _get.default)(item, column?.accessor);
}

/** Advanced table component that supports custom layout
 * [CAPITALIZATION] Header must be in uppercase
 * @uxr-deprecated The component is deprecated for UXR, use TableReact instead
 */
const Table = /*#__PURE__*/_react.default.forwardRef(({
  columns,
  data,
  className,
  tHeadProps = {},
  tBodyProps = {},
  trProps = {},
  thProps = {},
  tdProps = {},
  rowProps = {},
  THeadComponent = THead,
  TBodyComponent = TBody,
  TrComponent = Tr,
  HeadTrComponent = Tr,
  ThComponent = Th,
  TdComponent = Td,
  rowIdName,
  testId,
  sublevel,
  listLayout = false,
  loading = false,
  loadingText
}, ref) => {
  const getThTdClassName = (column, item, isHeader, customClassName = '') => (0, _classnames.default)(isHeader && column.headerClassName, column.cellClassName, typeof column.className === 'function' ? column.className(item) : column.className, customClassName);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)('r-table', className, {
      sublevel,
      'list-layout': listLayout
    }),
    "test-id": testId,
    ref: ref,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(THeadComponent, {
      className: tHeadProps.className,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(HeadTrComponent, {
        className: trProps.className,
        children: columns.map(column => column && /*#__PURE__*/(0, _jsxRuntime.jsx)(ThComponent, {
          testId: column?.testId,
          column: column,
          ...thProps,
          className: getThTdClassName(column, '', true, thProps?.className),
          children: renderHeader(column, data, rowProps)
        }, column.id || column?.accessor))
      })
    }), loading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_loadingIcon.LoadingIcon, {
      testId: "table-loading-icon",
      className: "table-loading-icon",
      message: loadingText
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(TBodyComponent, {
      className: tBodyProps.className,
      ...tBodyProps,
      children: data.map((item, i) => /*#__PURE__*/(0, _jsxRuntime.jsx)(TrComponent, {
        item: item,
        ...(item.testId && {
          testId: item.testId
        }),
        ...trProps,
        children: columns.map(column => column && /*#__PURE__*/(0, _jsxRuntime.jsx)(TdComponent, {
          id: column?.id || column?.accessor,
          ...tdProps,
          ...(column?.tdTestId && {
            testId: column?.tdTestId
          }),
          className: getThTdClassName(column, item, false, tdProps?.className),
          children: renderCell(column, item, i, rowProps)
        }, column?.id || column?.accessor))
      }, rowIdName ? item[rowIdName] : false || i))
    })]
  });
});
Table.displayName = 'Table';
var _default = exports.default = Table;