"use strict";

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

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

var _ramda = require("ramda");

var _classnames = _interopRequireDefault(require("classnames"));

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

var _ErrorBoundary = require("../ErrorBoundary");

var _Spinner = _interopRequireDefault(require("../Spinner"));

var _BlankSlate = require("../BlankSlate");

require("./style.css");

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

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 _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 renderDefaultEmptyState = function renderDefaultEmptyState() {
  return _react.default.createElement(_BlankSlate.BlankSlate, null);
};

var ListView = function ListView(_ref) {
  var data = _ref.data,
      renderContent = _ref.renderContent,
      onClick = _ref.onClick,
      renderHeader = _ref.renderHeader,
      renderFooter = _ref.renderFooter,
      isLoading = _ref.isLoading,
      _ref$renderEmptyState = _ref.renderEmptyState,
      renderEmptyState = _ref$renderEmptyState === void 0 ? renderDefaultEmptyState : _ref$renderEmptyState,
      restProps = _objectWithoutProperties(_ref, ["data", "renderContent", "onClick", "renderHeader", "renderFooter", "isLoading", "renderEmptyState"]);

  var isDataEmpty = (0, _ramda.isEmpty)(data);
  var isClickable = !(0, _ramda.isNil)(onClick);

  var header = renderHeader && _react.default.createElement("div", {
    className: "vs-list-view-header"
  }, renderHeader());

  var spinner = _react.default.createElement("div", {
    className: "vs-list-view-spinner-wrapper"
  }, _react.default.createElement(_Spinner.default, {
    size: "extra-large"
  }));

  var listViewContent = _react.default.createElement("div", {
    className: "vs-list-view"
  }, (0, _ramda.defaultTo)([])(data).map(function (item, index) {
    return _react.default.createElement("div", {
      key: item.id || index,
      className: (0, _classnames.default)({
        'vs-list-item': true,
        'vs-list-item-clickable': isClickable
      }),
      onClick: isClickable ? function () {
        return onClick(item);
      } : undefined
    }, renderContent(item));
  }));

  var footer = renderFooter && !isDataEmpty && _react.default.createElement("div", {
    className: "vs-list-view-footer"
  }, renderFooter());

  return _react.default.createElement("div", _extends({}, restProps, {
    className: "vs-list-view-container"
  }), header, isLoading && spinner, isDataEmpty && renderEmptyState(), !isLoading && listViewContent, footer);
};

ListView.propTypes = {
  data: _propTypes.default.array,
  renderContent: _propTypes.default.func.isRequired,
  onClick: _propTypes.default.func,
  renderHeader: _propTypes.default.func,
  renderFooter: _propTypes.default.func,
  renderEmptyState: _propTypes.default.func,
  isLoading: _propTypes.default.bool.isRequired
};
ListView.defaultProps = {
  isLoading: false
};

var _default = (0, _ErrorBoundary.withErrorBoundary)(ListView);

exports.default = _default;