import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { withListProps, ASC, passedWithListPropsTypes } from './HOCs/withListProps';
export var passedListManagerPropsTypes = Object.assign({}, passedWithListPropsTypes, {
  filteredRows: PropTypes.array.isRequired,
  visibleRows: PropTypes.array.isRequired
}); // TODO add catch errors (fieldsRules do not have real ids)

var ListManagerInner =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(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 (_.isFunction(customFilterByText)) {
      return customFilterByText;
    }

    return this.defaultFilterByText;
  };

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

    if (_.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 (_.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 (!_.isString(field)) {
      return rows;
    }

    var sort = this.getSort(fieldsRules[field]);
    return rows.sort(function (preRow, nextRow) {
      return sort(preRow[field], nextRow[field]) * (direction === 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 = _objectWithoutPropertiesLoose(_this$props2, ["children", "fieldsRules", "rows"]);

    return Object.assign({
      filteredRows: filteredRows,
      visibleRows: visibleRows
    }, props);
  };

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

  return ListManagerInner;
}(React.PureComponent);

_defineProperty(ListManagerInner, "propTypes", Object.assign({}, passedWithListPropsTypes, {
  rows: PropTypes.array.isRequired,
  children: PropTypes.element.isRequired,
  fieldsRules: PropTypes.shape({
    isFiltering: PropTypes.bool,
    filterByText: PropTypes.func,
    filterByParams: PropTypes.func,
    sort: PropTypes.func
  })
}));

_defineProperty(ListManagerInner, "defaultProps", {
  fieldsRules: {}
});

export var ListManager = withListProps(ListManagerInner);