"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.ListManager = exports.passedListManagerPropsTypes = void 0;

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

var _lodash = _interopRequireDefault(require("lodash"));

var _withListProps = require("./HOCs/withListProps");

var passedListManagerPropsTypes = Object.assign({}, _withListProps.passedWithListPropsTypes, {
  filteredRows: _propTypes.default.array.isRequired,
  visibleRows: _propTypes.default.array.isRequired
}); // TODO add catch errors (fieldsRules do not have real ids)

exports.passedListManagerPropsTypes = passedListManagerPropsTypes;

var ListManagerInner =
/*#__PURE__*/
function (_React$PureComponent) {
  (0, _inheritsLoose2.default)(ListManagerInner, _React$PureComponent);

  function ListManagerInner() {
    return _React$PureComponent.apply(this, arguments) || this;
  }

  var _proto = ListManagerInner.prototype;

  _proto.defaultFilterByText = function defaultFilterByText(value, filterText) {
    value = String(value).toLowerCase();
    return value.includes(filterText);
  };

  _proto.getFilterByText = function getFilterByText(field) {
    var customFilterByText = field.filterByText;

    if (_lodash.default.isFunction(customFilterByText)) {
      return customFilterByText;
    }

    return this.defaultFilterByText;
  };

  _proto.defaultSort = function defaultSort(prevValue, nextValue) {
    if (_lodash.default.isString(prevValue)) {
      prevValue = prevValue.toLowerCase();
    }

    if (_lodash.default.isString(nextValue)) {
      nextValue = nextValue.toLowerCase();
    }

    if (prevValue === nextValue) {
      return 0;
    }

    return prevValue >= nextValue ? 1 : -1;
  };

  _proto.getSort = function getSort(field) {
    var customSort = field.sort;

    if (_lodash.default.isFunction(customSort)) {
      return customSort;
    }

    return this.defaultSort;
  };

  _proto.getRowsByFilterText = function getRowsByFilterText(rows, filterText, fieldsRules) {
    var _this = this;

    var fieldsRulesKeys = Object.keys(fieldsRules);

    if (fieldsRulesKeys.length === 0) {
      return rows;
    }

    filterText = filterText.toLowerCase();
    return rows.filter(function (row) {
      return fieldsRulesKeys.filter(function (fieldName) {
        return fieldsRules[fieldName].isFiltering === true;
      }).some(function (fieldName) {
        var filterByText = _this.getFilterByText(fieldsRules[fieldName]);

        return filterByText(row[fieldName], filterText);
      });
    });
  };

  _proto.getRowsByFieldParams = function getRowsByFieldParams(rows, fieldsParams, fieldsRules) {
    return rows.filter(function (row) {
      return Object.keys(fieldsParams).every(function (fieldName) {
        var filteredValue = row[fieldName];
        var fieldParams = fieldsParams[fieldName];
        var filterByParams = fieldsRules[fieldName].filterByParams;
        return filterByParams(filteredValue, fieldParams);
      });
    });
  };

  _proto.getRowsByPagination = function getRowsByPagination(rows, activePage, pageSize) {
    var beginIndex = activePage * pageSize;
    return rows.slice(beginIndex, beginIndex + pageSize);
  };

  _proto.getRowsBySorting = function getRowsBySorting(rows, sorting, fieldsRules) {
    var field = sorting.field,
        direction = sorting.direction;

    if (!_lodash.default.isString(field)) {
      return rows;
    }

    var sort = this.getSort(fieldsRules[field]);
    return rows.sort(function (preRow, nextRow) {
      return sort(preRow[field], nextRow[field]) * (direction === _withListProps.ASC ? 1 : -1);
    });
  };

  _proto.getProps = function getProps() {
    var _this$props = this.props,
        rows = _this$props.rows,
        activePage = _this$props.activePage,
        pageSize = _this$props.pageSize,
        filterText = _this$props.filterText,
        fieldsRules = _this$props.fieldsRules,
        fieldsParams = _this$props.fieldsParams,
        sorting = _this$props.sorting;
    var filteredRows = this.getRowsByFieldParams(rows, fieldsParams, fieldsRules);
    filteredRows = this.getRowsByFilterText(filteredRows, filterText, fieldsRules);
    var sortedFilteredRows = this.getRowsBySorting(filteredRows, sorting, fieldsRules);
    var visibleRows = this.getRowsByPagination(sortedFilteredRows, activePage, pageSize);
    var _this$props2 = this.props,
        children = _this$props2.children,
        _fieldsRules = _this$props2.fieldsRules,
        _rows = _this$props2.rows,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$props2, ["children", "fieldsRules", "rows"]);
    return Object.assign({
      filteredRows: filteredRows,
      visibleRows: visibleRows
    }, props);
  };

  _proto.render = function render() {
    return _react.default.cloneElement(this.props.children, Object.assign({}, this.getProps()));
  };

  return ListManagerInner;
}(_react.default.PureComponent);

(0, _defineProperty2.default)(ListManagerInner, "propTypes", Object.assign({}, _withListProps.passedWithListPropsTypes, {
  rows: _propTypes.default.array.isRequired,
  children: _propTypes.default.element.isRequired,
  fieldsRules: _propTypes.default.shape({
    isFiltering: _propTypes.default.bool,
    filterByText: _propTypes.default.func,
    filterByParams: _propTypes.default.func,
    sort: _propTypes.default.func
  })
}));
(0, _defineProperty2.default)(ListManagerInner, "defaultProps", {
  fieldsRules: {}
});
var ListManager = (0, _withListProps.withListProps)(ListManagerInner);
exports.ListManager = ListManager;