import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _JSXStyle from "../styled-jsx.es.js";
import React, { useState } from 'react';
import { useTheme, useClasses } from '@helpdice/theme';
import { uniqueId } from 'lodash';
// import Placeholder from '../placeholder';
import TableCell from './table-cell';
import { useTableContext } from './table-context';

// import { DataTable } from '@helpdice/pro';

var TableBody = function TableBody(_ref) {
  var data = _ref.data,
    emptyText = _ref.emptyText,
    onRow = _ref.onRow,
    onCell = _ref.onCell,
    rowClassName = _ref.rowClassName,
    isLoading = _ref.isLoading,
    rowErrors = _ref.rowErrors,
    _ref$rowDraggable = _ref.rowDraggable,
    rowDraggable = _ref$rowDraggable === void 0 ? false : _ref$rowDraggable,
    _ref$readOnly = _ref.readOnly,
    readOnly = _ref$readOnly === void 0 ? false : _ref$readOnly,
    onSelected = _ref.onSelected;
  var theme = useTheme();
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selected = _useState2[0],
    setSelected = _useState2[1];
  var _useTableContext = useTableContext(),
    columns = _useTableContext.columns;
  // const rowClickHandler = (row: TableDataItem, index: number) => {
  //   onRow && onRow(row, index)
  // }

  var handleClick = function handleClick(_event, id) {
    if (readOnly) {
      return true;
    }
    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);
    onSelected && onSelected(newSelected);
  };

  // const handleSelectAllClick = (event: { target: { checked: any } }) => {
  //   if (event.target.checked) {
  //     const newSelecteds = data.map((n: any) => n._id);
  //     setSelected(newSelecteds as never[]);
  //     return;
  //   }
  //   setSelected([]);
  // };

  // Push Selected Rows
  // const SELECTED = useMemo(() => {
  //   if (selected.length > 0 && onSelected) {
  //     return onSelected(selected);
  //   }
  //   return <></>;
  // }, [selected]);

  function renderRow(cols, row, index) {
    var _row$style;
    // const urid = _.uniqueId();
    var className = rowClassName(row, index);
    var rw = _extends({}, row);
    var rowStyle = (_row$style = row === null || row === void 0 ? void 0 : row.style) !== null && _row$style !== void 0 ? _row$style : {};
    var onDragStart = row === null || row === void 0 ? void 0 : row.onDragStart;
    delete rw.table;
    delete rw.style;
    delete rw.onDragStart;
    // console.log(row);
    // const frow = Object.fromEntries(
    //   Object.entries(row).filter(([ky, _]) => ky !== '_id' && cols.map((col: any) => col.id).includes(ky))
    // ) as unknown as TableDataItem
    // console.log(frow);
    var isRowSelected = selected.indexOf(row) !== -1;
    var hoverColor = theme.palette.accents_1;
    var rowError = rowErrors === null || rowErrors === void 0 ? void 0 : rowErrors.get(row.id);
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("tr", {
      draggable: rowDraggable,
      role: "checkbox",
      onDragStart: onDragStart,
      "aria-checked": selected.indexOf(row) !== -1,
      tabIndex: -1,
      key: "table-row-".concat(rw.id),
      style: _extends({
        cursor: 'pointer',
        backgroundColor: isRowSelected ? hoverColor : ''
      }, rowStyle),
      onClick: function onClick(e) {
        if (!rowDraggable) {
          if (onRow) {
            onRow(row, index);
          } else {
            handleClick(e, row);
          }
        }
      },
      className: _JSXStyle.dynamic([["2988536338", [theme.palette.accents_1, theme.palette.warningLighter, theme.palette.error, theme.palette.border, rowError ? theme.type == "dark" ? theme.palette.accents_1 : theme.palette.accents_8 : theme.palette.accents_6]]]) + " " + (useClasses("".concat(rowDraggable ? 'draggable' : 'hover', " ").concat(rowError ? 'row-error' : '', " ").concat(className)) || "")
    }, /*#__PURE__*/React.createElement(TableCell, {
      columns: cols,
      row: rw,
      isLoading: isLoading,
      errors: rowError,
      rowIndex: index,
      emptyText: emptyText,
      onCellClick: onCell
    })), /*#__PURE__*/React.createElement(_JSXStyle, {
      id: "2988536338",
      dynamic: [theme.palette.accents_1, theme.palette.warningLighter, theme.palette.error, theme.palette.border, rowError ? theme.type == "dark" ? theme.palette.accents_1 : theme.palette.accents_8 : theme.palette.accents_6]
    }, "tr.__jsx-style-dynamic-selector{-webkit-transition:background-color 0.25s ease;transition:background-color 0.25s ease;font-size:inherit;}tr.hover.__jsx-style-dynamic-selector:hover{background-color:".concat(theme.palette.accents_1, ";}tr.draggable.__jsx-style-dynamic-selector:hover{border:2px dashed #dfdfdf;margin:3px;}tr.row-error.__jsx-style-dynamic-selector{background:").concat(theme.palette.warningLighter, " !important;border:2px dashed ").concat(theme.palette.error, ";margin:3px;}tr.__jsx-style-dynamic-selector td{padding:0 0.5em;border-bottom:1px solid ").concat(theme.palette.border, ";color:").concat(rowError ? theme.type == "dark" ? theme.palette.accents_1 : theme.palette.accents_8 : theme.palette.accents_6, ";font-size:calc(0.875 * var(--table-font-size));text-align:left;}tr.__jsx-style-dynamic-selector .cell{min-height:calc(2.125 * var(--table-font-size));display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-align:center;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-flow:row wrap;-ms-flex-flow:row wrap;flex-flow:row wrap;}")));
  }

  // if (data?.length === 0) {
  //   return (
  //     <tbody>
  //       <tr>
  //         <td colSpan={columns?.length}>
  //           <div>
  //              <Placeholder empty textOnly={textPlaceholder} msg="No Entries Found" />
  //           </div>
  //         </td>
  //       </tr>
  //     </tbody>
  //   )
  // }

  return /*#__PURE__*/React.createElement("tbody", {
    className: _JSXStyle.dynamic([["1422656197", [theme.palette.accents_1, theme.palette.border, theme.palette.accents_6]]])
  }, data.map(function (row, index) {
    // const qid = _.uniqueId();
    if (row === null || row === undefined) {
      var uid = uniqueId();
      return /*#__PURE__*/React.createElement("tr", {
        key: uid,
        className: _JSXStyle.dynamic([["1422656197", [theme.palette.accents_1, theme.palette.border, theme.palette.accents_6]]])
      }, /*#__PURE__*/React.createElement("td", {
        colSpan: columns.length,
        className: _JSXStyle.dynamic([["1422656197", [theme.palette.accents_1, theme.palette.border, theme.palette.accents_6]]])
      }, "\u2003"));
    }
    return /*#__PURE__*/React.createElement(React.Fragment, null, renderRow(columns, row, index));

    // return (
    //   <tr
    //     key={`tbody-row-${index}`}
    //     className={useClasses({ hover }, className)}
    //     onClick={() => rowClickHandler(row, index)}>
    //     <TableCell<TableDataItem>
    //       columns={columns}
    //       row={row}
    //       rowIndex={index}
    //       emptyText={emptyText}
    //       onCellClick={onCell}
    //     />
    //   </tr>
    // )
  }), /*#__PURE__*/React.createElement(_JSXStyle, {
    id: "1422656197",
    dynamic: [theme.palette.accents_1, theme.palette.border, theme.palette.accents_6]
  }, "tr.__jsx-style-dynamic-selector{-webkit-transition:background-color 0.25s ease;transition:background-color 0.25s ease;font-size:inherit;}tr.hover.__jsx-style-dynamic-selector:hover{background-color:".concat(theme.palette.accents_1, ";}tr.draggable.__jsx-style-dynamic-selector:hover{border:2px dashed #dfdfdf;margin:3px;}tr.__jsx-style-dynamic-selector td{padding:0 0.5em;border-bottom:1px solid ").concat(theme.palette.border, ";color:").concat(theme.palette.accents_6, ";font-size:calc(0.875 * var(--table-font-size));text-align:left;}tr.__jsx-style-dynamic-selector .cell{min-height:calc(3.125 * var(--table-font-size));display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-align:center;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-flex-flow:row wrap;-ms-flex-flow:row wrap;flex-flow:row wrap;}")));
};
TableBody.displayName = 'TableBody';
export default TableBody;