"use strict";

exports.__esModule = true;
exports.useStaticListFilterCollection = useStaticListFilterCollection;
var _react = require("react");
var _useFilterCollection = require("./useFilterCollection");
var _wordStartsWithMatch = require("../utils/wordStartsWithMatch");
let queryNameCounter = 0;
function useStaticListFilterCollection(
/**
 * This is a reference to the filter that was defined in the main collection. For example: `collection.filters.<filterName>`
 */
filter,
/**
 * Static list of items to show in the filter component
 */
list) {
  // if a new list is passed, update as ref so `fetchData` can access the latest list (collection should still be re-fetched by consumer).
  const listRef = (0, _react.useRef)(list);
  listRef.current = list;
  const createFilterCollection = (0, _useFilterCollection.useCreateFilterCollection)();
  const [queryName] = (0, _react.useState)(() => `static-list-filter-collection-${filter.name || ''}-${queryNameCounter++}`);
  const [collection] = (0, _react.useState)(() => createFilterCollection(filter, {
    queryName,
    fetchData: async query => {
      const {
        search
      } = query;
      let filteredList = listRef.current;
      if (search) {
        const startsWith = (0, _wordStartsWithMatch.wordStartsWithMatch)(search);
        filteredList = listRef.current.filter(item => {
          return startsWith(filter.itemName(item));
        });
      }
      return {
        items: filteredList
      };
    },
    queryOptions: {
      refetchOnMount: false,
      retryOnMount: false
    }
  }));
  return collection;
}
//# sourceMappingURL=useStaticListFilterCollection.js.map