'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

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

var multipleColumns = function multipleColumns(_ref) {
  var data = _ref.data;
  var columns = _ref.columns;
  var query = _ref.query;
  var strategy = _ref.strategy;
  var transform = _ref.transform;

  if (!query) {
    return data;
  }

  return Object.keys(query).reduce(function (filteredData, searchColumn) {
    return singleColumn({
      data: filteredData,
      columns: columns,
      searchColumn: searchColumn,
      query: query[searchColumn],
      strategy: strategy,
      transform: transform
    });
  }, data);
};

var singleColumn = function singleColumn(_ref2) {
  var data = _ref2.data;
  var columns = _ref2.columns;
  var _ref2$searchColumn = _ref2.searchColumn;
  var searchColumn = _ref2$searchColumn === undefined ? 'all' : _ref2$searchColumn;
  var query = _ref2.query;
  var strategy = _ref2.strategy;
  var transform = _ref2.transform;

  if (!query) {
    return data;
  }

  var ret = columns;

  if (searchColumn !== 'all') {
    ret = columns.filter(function (col) {
      return col.cell && col.cell.property === searchColumn;
    });
  }

  return data.filter(function (row) {
    return ret.filter(function (column) {
      return _columnMatches({
        query: query, column: column, strategy: strategy, transform: transform, row: row
      });
    }).length > 0;
  });
};

var _columnMatches = function _columnMatches(_ref3) {
  var // eslint-disable-line no-underscore-dangle
  query = _ref3.query;
  var _ref3$column = _ref3.column;
  var column = _ref3$column === undefined ? { cell: {} } : _ref3$column;
  var row = _ref3.row;
  var _ref3$strategy = _ref3.strategy;
  var strategy = _ref3$strategy === undefined ? strategies.infix : _ref3$strategy;
  var _ref3$transform = _ref3.transform;
  var transform = _ref3$transform === undefined ? function (v) {
    return v.toLowerCase();
  } : _ref3$transform;

  var property = column.cell.property;
  var value = (0, _get2.default)(row, property);
  var formatter = column.cell.resolve || function (a) {
    return a;
  };
  var formattedValue = formatter(value);

  if (typeof formattedValue === 'undefined') {
    formattedValue = '';
  }

  formattedValue = formattedValue.toString ? formattedValue.toString() : '';

  return strategy(transform(query)).evaluate(transform(formattedValue));
};

var matches = function matches() {
  var _ref4 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

  var value = _ref4.value;
  var query = _ref4.query;
  var _ref4$strategy = _ref4.strategy;
  var strategy = _ref4$strategy === undefined ? strategies.infix : _ref4$strategy;
  var _ref4$transform = _ref4.transform;
  var transform = _ref4$transform === undefined ? function (v) {
    return v.toLowerCase();
  } : _ref4$transform;

  if (!query) {
    return {};
  }

  return strategy(transform(query)).matches(transform(value));
};

var infix = function infix(queryTerm) {
  return {
    evaluate: function evaluate(searchText) {
      return searchText.indexOf(queryTerm) !== -1;
    },
    matches: function matches(searchText) {
      var splitString = searchText.split(queryTerm);
      var result = [];
      var currentPosition = 0;

      for (var x = 0; x < splitString.length; x++) {
        result.push({
          startIndex: currentPosition + splitString[x].length,
          length: queryTerm.length
        });

        currentPosition += splitString[x].length + queryTerm.length;
      }

      result.pop();

      return result;
    }
  };
};

var prefix = function prefix(queryTerm) {
  return {
    evaluate: function evaluate(searchText) {
      return searchText.indexOf(queryTerm) === 0;
    },
    matches: function matches(searchText) {
      var prefixIndex = searchText.indexOf(queryTerm);

      if (prefixIndex === 0) {
        return [{
          startIndex: 0,
          length: queryTerm.length
        }];
      }

      return [];
    }
  };
};

var strategies = {
  infix: infix,
  prefix: prefix
};

exports.default = {
  multipleColumns: multipleColumns,
  singleColumn: singleColumn,
  _columnMatches: _columnMatches,
  matches: matches,
  strategies: strategies
};