"use strict";

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

var _react = _interopRequireDefault(require("react"));

require("./DataTable.css");

var _ = require("../");

var _Pagination = _interopRequireDefault(require("../../Pagination"));

var R = _interopRequireWildcard(require("ramda"));

var _sortingHelper = require("./sortingHelper");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _LoadingAnimation = _interopRequireDefault(require("../../LoadingAnimation"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var generateHeader = function generateHeader(_ref) {
  var sortable = _ref.sortable,
      sortingOption = _ref.sortingOption,
      onSort = _ref.onSort,
      data = _ref.data;
  return function (_ref2, index) {
    var currentLabel = _ref2.label,
        width = _ref2.width;

    var isCurrentColumnSorted = function isCurrentColumnSorted() {
      return sortingOption.label === currentLabel;
    };

    var headerClickHandler = function headerClickHandler() {
      var order = sortingOption.order;
      var currentOrder = isCurrentColumnSorted() ? order : null;
      var nextOrder = (0, _sortingHelper.getNextOrder)(currentOrder);
      var nextData = (0, _sortingHelper.getNextData)(index, currentOrder, data);
      onSort({
        sortingOption: {
          label: currentLabel,
          order: nextOrder
        },
        data: nextData
      });
    };

    return _react.default.createElement(_.Th, {
      id: "label",
      style: width && {
        width: width
      },
      className: "\n        ".concat(sortable && 'vs-data-table-header-sortable', "\n        ").concat(sortable && isCurrentColumnSorted() && 'vs-data-table-header-sorted', "\n      "),
      key: index,
      onClick: sortable ? headerClickHandler : undefined
    }, _react.default.createElement("div", null, currentLabel, _react.default.createElement("span", null, sortable && (0, _sortingHelper.getSortingIcon)(sortingOption, currentLabel))));
  };
};

var generateRow = function generateRow(_ref3) {
  var _onClick = _ref3.onClick,
      columns = _ref3.columns;
  return function (rowItems, index) {
    return _react.default.createElement(_.Tr, {
      key: index
    }, rowItems.map(function (item, columnIndex) {
      var getColumn = R.compose(R.defaultTo({}), R.prop(columnIndex));

      var _getColumn = getColumn(columns),
          label = _getColumn.label,
          clickable = _getColumn.clickable,
          _getColumn$renderCell = _getColumn.renderCell,
          renderCell = _getColumn$renderCell === void 0 ? R.identity : _getColumn$renderCell;

      return _react.default.createElement(_.Td, {
        key: columnIndex
      }, clickable && _react.default.createElement("a", {
        className: "vs-data-table-clickable-cell",
        onClick: function onClick() {
          _onClick({
            value: item,
            label: label
          });
        }
      }, renderCell(item)), !clickable && renderCell(item));
    }));
  };
};

var getDataWithPagination = function getDataWithPagination(rowsPerPage, page) {
  return R.compose(R.take(rowsPerPage), R.drop(rowsPerPage * (page - 1)));
};

var NoDataLabel = function NoDataLabel(_ref4) {
  var label = _ref4.label;
  return _react.default.createElement("div", {
    className: "vs-data-table-no-data-label"
  }, label);
};

var DataTable = function DataTable(_ref5) {
  var _ref5$caption = _ref5.caption,
      caption = _ref5$caption === void 0 ? '' : _ref5$caption,
      _ref5$description = _ref5.description,
      description = _ref5$description === void 0 ? '' : _ref5$description,
      _ref5$columns = _ref5.columns,
      columns = _ref5$columns === void 0 ? [] : _ref5$columns,
      _ref5$data = _ref5.data,
      data = _ref5$data === void 0 ? [] : _ref5$data,
      _ref5$isLoading = _ref5.isLoading,
      isLoading = _ref5$isLoading === void 0 ? false : _ref5$isLoading,
      loadingMessage = _ref5.loadingMessage,
      _ref5$rowsPerPage = _ref5.rowsPerPage,
      rowsPerPage = _ref5$rowsPerPage === void 0 ? 25 : _ref5$rowsPerPage,
      _ref5$page = _ref5.page,
      page = _ref5$page === void 0 ? 1 : _ref5$page,
      onPageChange = _ref5.onPageChange,
      _ref5$pagination = _ref5.pagination,
      pagination = _ref5$pagination === void 0 ? false : _ref5$pagination,
      _ref5$sortingOption = _ref5.sortingOption,
      sortingOption = _ref5$sortingOption === void 0 ? {} : _ref5$sortingOption,
      _ref5$sortable = _ref5.sortable,
      sortable = _ref5$sortable === void 0 ? false : _ref5$sortable,
      onClick = _ref5.onClick,
      onSort = _ref5.onSort,
      renderToolbar = _ref5.renderToolbar,
      _ref5$noDataLabel = _ref5.noDataLabel,
      noDataLabel = _ref5$noDataLabel === void 0 ? "No data available." : _ref5$noDataLabel,
      rowsPerPageTemplate = _ref5.rowsPerPageTemplate,
      totalRecordsTemplate = _ref5.totalRecordsTemplate;
  var normalizedData = pagination ? getDataWithPagination(rowsPerPage, page)(data) : data;
  return _react.default.createElement(_.TableContainer, {
    className: "vs-data-table-container"
  }, _react.default.createElement(_.TableTitle, null, _react.default.createElement("div", {
    className: "vs-data-table-top-level-header"
  }, _react.default.createElement("div", null, caption, _react.default.createElement("p", null, description)), _react.default.createElement("div", null, renderToolbar && renderToolbar({
    columns: columns,
    data: data
  })))), isLoading ? _react.default.createElement("div", {
    className: "vs-data-table-loading-animation-wrapper"
  }, _react.default.createElement(_LoadingAnimation.default, {
    loadingMessage: loadingMessage
  })) : _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_.Table, null, _react.default.createElement(_.THead, null, _react.default.createElement(_.Tr, null, columns.map(generateHeader({
    sortable: sortable,
    sortingOption: sortingOption,
    onSort: onSort,
    data: data
  })))), _react.default.createElement(_.TBody, null, normalizedData.map(generateRow({
    onClick: onClick,
    columns: columns
  })))), normalizedData.length === 0 && _react.default.createElement(NoDataLabel, {
    label: noDataLabel
  }), pagination && _react.default.createElement(_Pagination.default, {
    className: "vs-table-pagination",
    numberOfRows: data.length,
    rowsPerPage: rowsPerPage,
    page: page,
    onChange: onPageChange,
    rowsPerPageTemplate: rowsPerPageTemplate,
    totalRecordsTemplate: totalRecordsTemplate
  })));
};

exports.DataTable = DataTable;
DataTable.propTypes = {
  caption: _propTypes.default.string,
  description: _propTypes.default.string,
  columns: _propTypes.default.array,
  data: _propTypes.default.array,
  isLoading: _propTypes.default.bool,
  loadingMessage: _propTypes.default.string,
  rowsPerPage: _propTypes.default.number,
  page: _propTypes.default.number,
  onPageChange: _propTypes.default.func,
  pagination: _propTypes.default.bool,
  sortingOption: _propTypes.default.shape({
    label: _propTypes.default.string,
    order: _propTypes.default.string
  }),
  sortable: _propTypes.default.bool,
  onSort: _propTypes.default.func,
  onClick: _propTypes.default.func,
  renderToolbar: _propTypes.default.func,
  noDataLabel: _propTypes.default.string,
  rowsPerPageTemplate: _propTypes.default.string,
  totalRecordsTemplate: _propTypes.default.string
};