"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SimpleTable = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _useTableKeyboardNavigation = require("../hooks/use-table-keyboard-navigation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function HeaderCell({
  column,
  columnIndex
}) {
  const cellRef = (0, _react.useRef)(null);
  const keyboardNavigationContext = (0, _react.useContext)(_useTableKeyboardNavigation.TableKeyboardNavigationContext);
  keyboardNavigationContext?.registerCell({
    cellRef,
    cellRowIndex: -1,
    cellColumnIndex: columnIndex
  });
  const isCurrentCellFocused = keyboardNavigationContext?.focusedCell === (0, _useTableKeyboardNavigation.getCellId)({
    cellRowIndex: -1,
    cellColumnIndex: columnIndex
  });
  const a11yProps = {
    role: 'columnheader',
    tabIndex: columnIndex === 0 && !keyboardNavigationContext?.focusedCell || isCurrentCellFocused ? 0 : -1,
    onFocus: columnIndex === 0 && !keyboardNavigationContext?.focusedCell ? () => keyboardNavigationContext?.setFocusedCell(`-1-${columnIndex}`) : undefined
  };
  const columnDef = column;
  if (columnDef?.label) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("th", {
      ref: cellRef,
      "test-id": columnDef.testId,
      className: columnDef.className,
      "aria-hidden": !columnDef.label,
      ...a11yProps,
      children: columnDef.label
    });
  }
  if (typeof column === 'function') {
    const thValue = column();
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("th", {
      "aria-hidden": !thValue,
      ref: cellRef,
      ...a11yProps,
      children: /*#__PURE__*/_react.default.cloneElement(thValue, {
        className: `${thValue.props.className || ''} th-header`.trim()
      })
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("th", {
    "aria-hidden": !column,
    ref: cellRef,
    ...a11yProps,
    children: column
  });
}
function BodyCell({
  rowIndex,
  columnIndex,
  columnDef,
  children
}) {
  const cellRef = (0, _react.useRef)(null);
  const keyboardNavigationContext = (0, _react.useContext)(_useTableKeyboardNavigation.TableKeyboardNavigationContext);
  keyboardNavigationContext?.registerCell({
    cellRef,
    cellRowIndex: rowIndex,
    cellColumnIndex: columnIndex
  });
  const isCurrentCellFocused = keyboardNavigationContext?.focusedCell === (0, _useTableKeyboardNavigation.getCellId)({
    cellRowIndex: rowIndex,
    cellColumnIndex: columnIndex
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("td", {
    ref: cellRef,
    className: columnDef?.className,
    "test-id": columnDef?.testId,
    role: "gridcell",
    tabIndex: isCurrentCellFocused ? 0 : -1,
    children: children
  }, `tbody-td-${rowIndex}-${columnIndex}`);
}

/** A simplified table version for cases where we do not need complex
 *  interfaces Table component provides
 *  @midnight-gray-supported
 *  @alpine-light-supported
 */
const SimpleTable = ({
  columns,
  data,
  className,
  testId,
  sublevel,
  tableAriaLabel
}) => {
  const tableRef = (0, _react.useRef)(null);
  const keyboardNavigationContext = (0, _useTableKeyboardNavigation.useTableKeyboardNavigation)({
    tableRef,
    columnsCount: columns.length,
    rowsCount: data.length
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_useTableKeyboardNavigation.TableKeyboardNavigationContext.Provider, {
    value: keyboardNavigationContext,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("table", {
      className: (0, _classnames.default)('simple-table', className, {
        sublevel
      }),
      "test-id": testId,
      ref: tableRef,
      role: "grid",
      onKeyDown: keyboardNavigationContext.onTableKeyDownHandler,
      "aria-label": tableAriaLabel,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("thead", {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
          children: columns.map((column, columnIndex) => /*#__PURE__*/(0, _jsxRuntime.jsx)(HeaderCell, {
            column: column,
            columnIndex: columnIndex
          }, `thead-tr-${columnIndex}`))
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("tbody", {
        children: data.map((row, rowIndex) => /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
          role: "row",
          children: row.map((item, columnIndex) => {
            const isColumnObject = columns[columnIndex]?.label;
            const columnDef = isColumnObject ? columns[columnIndex] : undefined;
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(BodyCell, {
              rowIndex: rowIndex,
              columnIndex: columnIndex,
              columnDef: columnDef,
              children: typeof item === 'function' ? item() : item
            }, `tbody-td-${rowIndex}-${columnIndex}`);
          })
        }, `tbody-tr-${rowIndex}`))
      })]
    })
  });
};
exports.SimpleTable = SimpleTable;