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); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
Copyright (c) 2018-2019 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { StyledRoot, StyledTable, StyledTableHead, StyledTableHeadRow, StyledTableHeadCell, StyledTableHeadCellSortable, StyledTableBody, StyledTableBodyRow, StyledTableBodyCell, StyledSortAscIcon, StyledSortDescIcon, StyledSortNoneIcon } from './styled-components.js';
import { getOverrides } from '../helpers/overrides.js';
export default class TableBuilder extends React.Component {
  render() {
    const {
      overrides = {},
      children,
      data,
      horizontalScrollWidth,
      sortColumn,
      sortOrder = 'ASC',
      onSort,
      ...rest
    } = this.props;
    const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
    const [Table, tableProps] = getOverrides(overrides.Table, StyledTable);
    const [TableHead, tableHeadProps] = getOverrides(overrides.TableHead, StyledTableHead);
    const [TableHeadRow, tableHeadRowProps] = getOverrides(overrides.TableHeadRow, StyledTableHeadRow);
    const [TableHeadCell, tableHeadCellProps] = getOverrides(overrides.TableHeadCell, StyledTableHeadCell);
    const [TableHeadCellSortable, tableHeadCellSortableProps] = getOverrides(overrides.TableHeadCellSortable, StyledTableHeadCellSortable);
    const [TableBody, tableBodyProps] = getOverrides(overrides.TableBody, StyledTableBody);
    const [TableBodyRow, tableBodyRowProps] = getOverrides(overrides.TableBodyRow, StyledTableBodyRow);
    const [TableBodyCell, tableBodyCellProps] = getOverrides(overrides.TableBodyCell, StyledTableBodyCell);
    const [SortAscIcon, sortAscIconProps] = getOverrides(overrides.SortAscIcon, StyledSortAscIcon);
    const [SortDescIcon, sortDescIconProps] = getOverrides(overrides.SortDescIcon, StyledSortDescIcon);
    const [SortNoneIcon, sortNoneIconProps] = getOverrides(overrides.SortNoneIcon, StyledSortNoneIcon);
    const columns = React.Children.toArray(children).filter(Boolean).map(child => child.props);

    function renderHeader(col, colIndex) {
      const colOverrides = col.overrides || {};

      if (!col.sortable) {
        const [ColTableHeadCell, colTableHeadCellProps] = getOverrides(colOverrides.TableHeadCell, TableHeadCell);
        return React.createElement(ColTableHeadCell, _extends({
          key: colIndex
        }, tableHeadCellProps, colTableHeadCellProps), col.header);
      }

      const [ColTableHeadCellSortable, colTableHeadCellSortableProps] = getOverrides(colOverrides.TableHeadCellSortable, TableHeadCellSortable);
      let sortIcon = null;

      switch (col.id === sortColumn && sortOrder) {
        case 'ASC':
          sortIcon = React.createElement(SortAscIcon, sortAscIconProps);
          break;

        case 'DESC':
          sortIcon = React.createElement(SortDescIcon, sortDescIconProps);
          break;

        default:
          sortIcon = React.createElement(SortNoneIcon, sortNoneIconProps);
          break;
      }

      return React.createElement(ColTableHeadCellSortable, _extends({
        key: colIndex,
        role: "button",
        tabIndex: "0",
        onClick: () => onSort && onSort(col.id)
      }, tableHeadCellSortableProps, colTableHeadCellSortableProps), col.header, sortIcon);
    }

    function renderCell(col, colIndex, row, rowIndex) {
      const colOverrides = col.overrides || {};
      const [ColTableBodyCell, colTableBodyCellProps] = getOverrides(colOverrides.TableBodyCell, TableBodyCell);
      return React.createElement(ColTableBodyCell, _extends({
        key: colIndex,
        $isNumeric: col.numeric
      }, tableBodyCellProps, colTableBodyCellProps), col.children(row, rowIndex));
    }

    return React.createElement(Root, _extends({
      "data-baseweb": "table-builder-semantic"
    }, rootProps, rest), React.createElement(Table, _extends({
      $width: horizontalScrollWidth
    }, tableProps), React.createElement(TableHead, tableHeadProps, React.createElement(TableHeadRow, tableHeadRowProps, columns.map((col, colIndex) => renderHeader(col, colIndex)))), React.createElement(TableBody, tableBodyProps, data.map((row, rowIndex) => React.createElement(TableBodyRow, _extends({
      key: rowIndex
    }, tableBodyRowProps), columns.map((col, colIndex) => renderCell(col, colIndex, row, rowIndex)))))));
  }

}

_defineProperty(TableBuilder, "defaultProps", {
  data: []
});