"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConfigListTable = void 0;

var _ti = require("react-icons/ti");

var _reactTable = require("react-table");

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["columns", "data", "handleRowClick", "tableStyle", "isLoading"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var tableStyles = {
  outerDivClass: "border border-b-0 border-gray-300",
  tableClass: "min-w-full border-separate",
  theadClass: "bg-white z-10",
  theadRowClass: "z-10",
  theadHeaderClass: "px-5 py-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-gray-300",
  tbodyClass: "rounded-md",
  tbodyRowClass: "border cursor-pointer text-sm",
  tbodyDataClass: "whitespace-nowrap border-gray-300 border-b p-2"
};

var ConfigListTable = function ConfigListTable(_ref) {
  var columns = _ref.columns,
      data = _ref.data,
      handleRowClick = _ref.handleRowClick,
      tableStyle = _ref.tableStyle,
      isLoading = _ref.isLoading,
      rest = _objectWithoutProperties(_ref, _excluded);

  var tableInstance = (0, _reactTable.useTable)({
    columns: columns,
    data: data
  }, _reactTable.useSortBy);
  var getTableProps = tableInstance.getTableProps,
      getTableBodyProps = tableInstance.getTableBodyProps,
      headerGroups = tableInstance.headerGroups,
      rows = tableInstance.rows,
      prepareRow = tableInstance.prepareRow;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({
    className: tableStyles.outerDivClass
  }, rest), {}, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("table", _objectSpread(_objectSpread({
      className: tableStyles.tableClass,
      style: tableStyle
    }, getTableProps()), {}, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("thead", {
        className: tableStyles.theadClass,
        children: headerGroups.map(function (headerGroup) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", _objectSpread(_objectSpread({
            className: tableStyles.theadRowClass
          }, headerGroup.getHeaderGroupProps()), {}, {
            children: headerGroup.headers.map(function (column) {
              return /*#__PURE__*/(0, _jsxRuntime.jsx)("th", _objectSpread(_objectSpread({
                className: tableStyles.theadHeaderClass
              }, column.getHeaderProps(column.getSortByToggleProps())), {}, {
                children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
                  className: "flex select-none",
                  children: [column.render("Header"), column.isSorted ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                    className: "ml-2",
                    children: column.isSortedDesc ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_ti.TiArrowSortedUp, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_ti.TiArrowSortedDown, {})
                  }) : ""]
                })
              }), column.Header);
            })
          }), headerGroup.getHeaderGroupProps().key);
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("tbody", _objectSpread(_objectSpread({
        className: tableStyles.tbodyClass
      }, getTableBodyProps()), {}, {
        children: rows.map(function (row) {
          prepareRow(row);
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", _objectSpread(_objectSpread({
            className: tableStyles.tbodyRowClass
          }, row.getRowProps()), {}, {
            onClick: handleRowClick ? function () {
              return handleRowClick(row);
            } : function () {},
            children: row.cells.map(function (cell) {
              return /*#__PURE__*/(0, _jsxRuntime.jsx)("td", _objectSpread(_objectSpread({
                className: "".concat(tableStyles.tbodyDataClass, " ").concat(cell.column.cellClass || "")
              }, cell.getCellProps()), {}, {
                children: cell.render("Cell")
              }), cell.column.Header);
            })
          }), row.id);
        })
      }))]
    })), rows.length <= 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "flex items-center justify-center py-20 px-2  border-b border-gray-300 text-center text-gray-400",
      children: isLoading ? "Loading data.." : "No data available"
    })]
  }));
};

exports.ConfigListTable = ConfigListTable;