function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import * as React from 'react';
import classNames from 'classnames';
import { createUseStyles } from '@fluent-ui/styles';
import { styles } from './Table.styled';
import { TablePropTypes } from './Table.type';
import Head from './components/Head';
import Body from './components/Body';
import Row from './components/Row';
import Cell from './components/Cell';

function getColumnsChildren(columns, data) {
  const theHeadChildren = columns.map(cell => {
    return React.createElement(Cell, {
      key: cell.field
    }, cell.title);
  });
  const theBodyChildren = data.map((row, index) => React.createElement(Row, {
    key: row.key
  }, columns.map(cell => {
    const text = row[cell.field];
    const content = typeof cell.render === 'function' ? cell.render(text, row, index) : text;
    return React.createElement(Cell, {
      key: text
    }, content);
  })));
  return React.createElement(React.Fragment, null, React.createElement(Head, null, React.createElement(Row, null, theHeadChildren)), React.createElement(Body, null, theBodyChildren));
}

export const name = 'Table';
const useStyles = createUseStyles(styles, {
  name
});

function Table(props) {
  const {
    className: classNameProp,
    data,
    columns,
    children,
    ...rest
  } = props;
  const theChildren = columns ? getColumnsChildren(columns, data) : children;
  const classes = useStyles(props);
  const className = classNames(classes.root, classNameProp);
  return React.createElement("table", _extends({
    className: className
  }, rest), theChildren);
}

Table.displayName = `F${name}`;
Table.propTypes = TablePropTypes;
export default Table;