"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.usePaginatedSearch = usePaginatedSearch;
exports.usePaginatedSearchState = usePaginatedSearchState;
var _react = require("react");
var _useFilterOptions = require("../use-filter-options");
var _usePagination = require("../use-pagination");
function usePaginatedSearchState(config) {
  const initConfig = config || {};
  const {
    searchQuery: initialQuery,
    ...initialState
  } = initConfig;
  const [searchQuery, setSearchQuery] = (0, _react.useState)(initialQuery || '');
  const pagination = (0, _usePagination.usePaginationState)(initialState);
  const setSearchQueryAndResetPage = (0, _react.useCallback)(query => {
    setSearchQuery(query);
    pagination.setPage(0);
  }, [pagination.setPage, setSearchQuery]);
  return (0, _react.useMemo)(() => ({
    ...pagination,
    searchQuery,
    setSearchQuery: setSearchQueryAndResetPage
  }), [pagination, searchQuery, setSearchQueryAndResetPage]);
}

/**
 * @typedef PaginatedSearch
 * @property {array} items
 * @property {array} unpaginatedFilteredItems - all items passed through search filter, unpaginated
 * @property {number} pageSize - items per page
 * @property {number} currentPage - current page number
 * @property {number} total - total items count
 * @property {string} searchValue
 * @property {function} setPage
 * @property {function} setPageSize
 * @property {function} setTotal
 * @property {func} setSearchValue
 * */

const defaultOptions = {
  searchFields: ['name']
};

/**
 * A hook that combines filtering and pagination for an array of items
 *
 * @param {array} [items=[]] Array of items that should be filtered
 * and paginated
 * @param {options} {
 *     searchFields = ['name'],
 *     filterFunction,
 *     paginationDefaults,
 *   } - options to configure search and provide
 * initial state for pagination, can be undefined
 * @returns {PaginatedSearch} - object containing filtered items,
 * pagination hook state and search query
 */

function usePaginatedSearch(items = [], {
  searchFields = defaultOptions.searchFields,
  filterFunction,
  defaultPageSize
} = defaultOptions) {
  const [searchValue, setSearchValue] = (0, _react.useState)('');
  const filteredItems = (0, _useFilterOptions.useFilterOptions)(items, searchValue, searchFields, filterFunction);
  const pagination = (0, _usePagination.usePagination)(filteredItems, defaultPageSize);
  const setSearchValueAndReset = (0, _react.useCallback)(newSearchValue => {
    setSearchValue(newSearchValue);
    pagination.setPage(0);
  }, [pagination.currentPage, pagination.pageSize, pagination.setPage, setSearchValue]);
  return (0, _react.useMemo)(() => ({
    ...pagination,
    unpaginatedFilteredItems: filteredItems,
    searchValue,
    setSearchValue: setSearchValueAndReset
  }), [pagination, searchValue, setSearchValueAndReset]);
}

/**
 * Example:
 ```
  const fullListOfItems = getALotOfItemsSomehow();
  const {
    items,
    searchValue,
    total,
    currentPage,
    pageSize,
    setSearchValue,
    setPage,
    setPageSize,
  } = usePaginatedSearch(fullListOfItems);

  return (
    <React.Fragment>
      <Search value={searchValue} onChange={setSearchValue} />
      <PaginationControls
        total={total}
        currentPage={currentPage}
        pageSize={pageSize}
        availableSizes={[20, 50, 100]}
        onPageChange={setPage}
        onPageSizeChange={setPageSize}
      />
      {items.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </React.Fragment>
  );
 ```
 */