import { slicedToArray as _slicedToArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import * as React from 'react';
import Pagination from '@mui/material/Pagination';
import Dropdown from '../../Dropdown/Dropdown/index.js';
import { TableWrapper, TableHeadingWrapper, TableHeaderHeadingWrapper, FilterContainer, FilterResultWrapper, FilterOptionsWrapper, FilterDropdownWrapper, FilterSearchWrapper, CustomTableContainer, TablePaginationWrapper } from './style.js';
import { ThemeProvider } from 'styled-components';
import theme from '../../../Globals/theme.js';
import globalStyle from '../../../Globals/global-styles.js';
import SearchField from '../../SearchField/SearchField/index.js';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import Checkbox from '../../Checkbox/Checkbox/index.js';
import TableRow from '@mui/material/TableRow';
import TableSortLabel from '@mui/material/TableSortLabel';
import '../../Icons/IconWrapper/index.js';
import '@mui/material/CircularProgress';
import '../../Icons/WrapperCircularProgress/ChargedCircularProgress/style.js';
import '../../Icons/WrapperCircularProgress/ChargedCircularProgressContainer/style.js';
import '../../Icons/WrapperCircularProgress/NeedsChargeCircularProgress/style.js';
import '../../Icons/WrapperCircularProgress/NeedsChargeCircularProgressContainer/style.js';
import '../../Icons/WrapperCircularProgress/BatteryLowCircularProgress/style.js';
import '../../Icons/WrapperCircularProgress/BatteryLowCircularProgressContainer/style.js';
import '../../Icons/WrapperVehicleMarks/index.js';
import '../../Icons/WrapperLockSignIcon/index.js';
import '../../Icons/UsernameIcon/style.js';
import '../../Icons/WrapperVehicleGroupNumber/index.js';
import '../../Icons/WrapperVehicleGroupIcon/index.js';
import '../../Icons/WrapperVehicleMarkerAgencyPickup/index.js';
import '../../Icons/WrapperVehicleMarkerFlags/index.js';
import '../../Icons/WrapperVehicleMarkerMaintainance/index.js';
import '../../Icons/WrapperVehicleMarkerRebalance/index.js';
import '../../Icons/WrapperVehicleMarkerTicket/index.js';
import '../../Icons/WrapperAddIcon/index.js';
import '../../Icons/WrapperAgencyPickupIcon/index.js';
import '../../Icons/WrapperFlagIcon/index.js';
import '../../Icons/WrapperLockIcon/index.js';
import '../../Icons/WrapperMaintananceIcon/index.js';
import '../../Icons/WrapperOptionIcon/index.js';
import '../../Icons/WrapperRebelanceIcon/index.js';
import '../../Icons/WrapperTicketIcon/index.js';
import CheckboxIndeterminateIcon from '../../Icons/CheckboxIndeterminateIcon/index.js';

function EnhancedTable(_ref) {
  var rows = _ref.rows,
      columns = _ref.columns,
      rowsPerPageOptions = _ref.rowsPerPageOptions,
      tableHeader = _ref.tableHeader,
      tableActions = _ref.tableActions,
      rowClick = _ref.rowClick;

  var _React$useState = React.useState(rowsPerPageOptions[0].options),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      rowsPerPage = _React$useState2[0],
      setRowsPerPage = _React$useState2[1];

  var _React$useState3 = React.useState(0),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      page = _React$useState4[0],
      setPage = _React$useState4[1];

  var _React$useState5 = React.useState("asc"),
      _React$useState6 = _slicedToArray(_React$useState5, 2),
      order = _React$useState6[0],
      setOrder = _React$useState6[1];

  var _React$useState7 = React.useState("calories"),
      _React$useState8 = _slicedToArray(_React$useState7, 2),
      orderBy = _React$useState8[0],
      setOrderBy = _React$useState8[1];

  var _React$useState9 = React.useState([]),
      _React$useState10 = _slicedToArray(_React$useState9, 2),
      selected = _React$useState10[0],
      setSelected = _React$useState10[1];

  function descendingComparator(a, b, orderBy) {
    if (b[orderBy] < a[orderBy]) {
      return -1;
    }

    if (b[orderBy] > a[orderBy]) {
      return 1;
    }

    return 0;
  }

  function getComparator(order, orderBy) {
    return order === "desc" ? function (a, b) {
      return descendingComparator(a, b, orderBy);
    } : function (a, b) {
      return -descendingComparator(a, b, orderBy);
    };
  }

  function EnhancedTableHead(props) {
    var onSelectAllClick = props.onSelectAllClick,
        numSelected = props.numSelected,
        rowCount = props.rowCount,
        onRequestSort = props.onRequestSort;

    var createSortHandler = function createSortHandler(property) {
      return function (event) {
        onRequestSort(event, property);
      };
    };

    return /*#__PURE__*/React.createElement(TableHead, null, /*#__PURE__*/React.createElement(TableRow, null, /*#__PURE__*/React.createElement(TableCell, null, /*#__PURE__*/React.createElement("div", {
      className: "checkbox-head"
    }, /*#__PURE__*/React.createElement(Checkbox, {
      color: "primary",
      indeterminate: numSelected > 0 && numSelected < rowCount,
      indeterminateIcon: /*#__PURE__*/React.createElement(CheckboxIndeterminateIcon, null),
      checked: rowCount > 0 && numSelected === rowCount,
      onChange: onSelectAllClick
    }))), columns.map(function (column) {
      return /*#__PURE__*/React.createElement(TableCell, {
        className: "column-pinned-".concat(column.pinColPosition),
        key: column.field
      }, /*#__PURE__*/React.createElement(TableSortLabel, {
        onClick: createSortHandler(column.field)
      }, column.headerName));
    })));
  }

  var handleChangeRowsPerPage = function handleChangeRowsPerPage(selectedList, selectedItem) {
    setPage(0);
    setRowsPerPage(selectedItem.options);
  };

  var handleChangePage = function handleChangePage(event, newPage) {
    setPage(newPage - 1);
  };

  var CustomPagination = function CustomPagination() {
    return /*#__PURE__*/React.createElement(TablePaginationWrapper, null, /*#__PURE__*/React.createElement(Pagination, {
      count: Math.ceil(rows.length / rowsPerPage),
      page: page + 1,
      onChange: handleChangePage,
      variant: "outlined",
      shape: "rounded"
    }));
  };

  var handleRequestSort = function handleRequestSort(event, property) {
    var isAsc = orderBy === property && order === "asc";
    setOrder(isAsc ? "desc" : "asc");
    setOrderBy(property);
  };

  var handleSelectAllClick = function handleSelectAllClick(event) {
    if (event.target.checked) {
      var newSelected = rows.map(function (n) {
        return n.id;
      });
      setSelected(newSelected);
      return;
    }

    setSelected([]);
  };

  var handleClick = function handleClick(evt, id) {
    evt.stopPropagation();
    evt.preventDefault();
    var selectedIndex = selected.indexOf(id);
    var newSelected = [];

    if (selectedIndex === -1) {
      newSelected = newSelected.concat(selected, id);
    } else if (selectedIndex === 0) {
      newSelected = newSelected.concat(selected.slice(1));
    } else if (selectedIndex === selected.length - 1) {
      newSelected = newSelected.concat(selected.slice(0, -1));
    } else if (selectedIndex > 0) {
      newSelected = newSelected.concat(selected.slice(0, selectedIndex), selected.slice(selectedIndex + 1));
    }

    setSelected(newSelected);
  };

  var onRowClick = function onRowClick(row) {
    rowClick(row);
  };

  var isSelected = function isSelected(id) {
    return selected.indexOf(id) !== -1;
  };

  var generateRows = function generateRows() {
    var sortedRows = rows.sort(getComparator(order, orderBy)).slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage);
    var headers = columns.map(function (el) {
      return {
        field: el.field,
        pinColPosition: el.pinColPosition,
        renderCell: el.renderCell ? el.renderCell : undefined
      };
    });
    return sortedRows.map(function (row, index) {
      var isItemSelected = isSelected(row.id);
      var labelId = "enhanced-table-checkbox-".concat(index);
      return /*#__PURE__*/React.createElement(TableRow, {
        onClick: function onClick() {
          return onRowClick(row);
        },
        "aria-checked": isItemSelected,
        tabIndex: -1,
        key: row.id,
        selected: isItemSelected
      }, /*#__PURE__*/React.createElement(TableCell, null, /*#__PURE__*/React.createElement("div", {
        className: "checkbox-body"
      }, /*#__PURE__*/React.createElement(Checkbox, {
        checked: isItemSelected,
        onClick: function onClick(evt) {
          return handleClick(evt, row.id);
        },
        inputProps: {
          "aria-labelledby": labelId
        }
      }))), headers.map(function (el, index) {
        return typeof el.renderCell === "function" ? /*#__PURE__*/React.createElement(TableCell, {
          className: "column-pinned-".concat(el.pinColPosition),
          key: index
        }, el.renderCell(row[el.field])) : /*#__PURE__*/React.createElement(TableCell, {
          key: index
        }, row[el.field]);
      }));
    });
  };

  return /*#__PURE__*/React.createElement(ThemeProvider, {
    theme: theme
  }, /*#__PURE__*/React.createElement(globalStyle, null), /*#__PURE__*/React.createElement(TableWrapper, null, /*#__PURE__*/React.createElement(TableHeadingWrapper, null, /*#__PURE__*/React.createElement(TableHeaderHeadingWrapper, null, tableHeader), tableActions), /*#__PURE__*/React.createElement(FilterContainer, null, /*#__PURE__*/React.createElement(FilterResultWrapper, null, "Showing Results ", (page + 1) * rowsPerPage - rowsPerPage + 1, " -", " ", (page + 1) * rowsPerPage > rows.length ? rows.length : (page + 1) * rowsPerPage, " ", "of ", rows.length, " Results"), /*#__PURE__*/React.createElement(FilterOptionsWrapper, null, /*#__PURE__*/React.createElement(FilterDropdownWrapper, null, /*#__PURE__*/React.createElement(Dropdown, {
    placeholder: rowsPerPage,
    items: rowsPerPageOptions,
    onChange: handleChangeRowsPerPage,
    value: rowsPerPage[0]
  })), /*#__PURE__*/React.createElement(FilterResultWrapper, null, "Results per page"), /*#__PURE__*/React.createElement(FilterSearchWrapper, null, /*#__PURE__*/React.createElement(SearchField, null)))), /*#__PURE__*/React.createElement(CustomTableContainer, null, /*#__PURE__*/React.createElement(TableContainer, null, /*#__PURE__*/React.createElement(Table, null, /*#__PURE__*/React.createElement(EnhancedTableHead, {
    numSelected: selected.length,
    order: order,
    orderBy: orderBy,
    onSelectAllClick: handleSelectAllClick,
    onRequestSort: handleRequestSort,
    rowCount: rows.length
  }), /*#__PURE__*/React.createElement(TableBody, null, generateRows()))), CustomPagination())));
}

export { EnhancedTable as default };
//# sourceMappingURL=index.js.map
