"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useFilterOptions;
exports.prioritizeStartsWithValue = exports.defaultFilter = void 0;
var _react = require("react");
var _get = _interopRequireDefault(require("lodash-es/get"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// eslint-disable-next-line you-dont-need-lodash-underscore/get

const defaultFilter = (it, searchValue, searchFields) => !searchValue ||
// match all if no search value provided
searchFields.some(field => String((0, _get.default)(it, field)).toLowerCase().includes(searchValue.trim().toLowerCase()));

/**
 * Values that start with a search pattern, should come first in the list of filtered typeahead option
 */
exports.defaultFilter = defaultFilter;
const prioritizeStartsWithValue = (optionA, optionB, searchFields, searchPattern) => {
  if (searchPattern.length < 3 ||
  // wait for the length to be meaningful
  searchFields.length !== 1 // multiple search fields might make the logic unnecessarily complex
  ) {
    return 0;
  }
  const optionAValue = String((0, _get.default)(optionA, searchFields[0]));
  const optionBValue = String((0, _get.default)(optionB, searchFields[0]));
  if (optionBValue.startsWith(searchPattern)) {
    return 1;
  }
  if (optionAValue.startsWith(searchPattern)) {
    return -1;
  }
  return 0;
};

/**
 * Filters a list of options with searchValue
 *
 * @param {array} options - An array of objects which should be filtered.
 * @param {string} searchValue - String value to match against, used as a search query.
 * @param {array} searchFields - An array of keys which should be checked on each options object.
 * @param {func} filterFunction - Custom predicate function for filtering
 * @returns {array} - Options array filtered by the search query.
 */
exports.prioritizeStartsWithValue = prioritizeStartsWithValue;
function useFilterOptions(options, searchValue, searchFields, filterFunction = defaultFilter) {
  const trimmedLowercaseValue = searchValue.trim().toLowerCase();
  const filterPredicate = (0, _react.useCallback)(it => filterFunction(it, trimmedLowercaseValue, searchFields), [filterFunction, trimmedLowercaseValue, searchFields]);
  return (0, _react.useMemo)(() => options.filter(filterPredicate).sort((a, b) => prioritizeStartsWithValue(a, b, searchFields, trimmedLowercaseValue)), [options, filterPredicate]);
}