import { debounce } from "lodash";
import type { SearchResultsStore } from "searchResults/domain/searchStore";
import { SearchResults } from "searchResults/domain/searchResultsEntity";

type GetSearchResultsStore = Pick<
  SearchResultsStore,
  "searchResults" | "search" | "searchRequest"
>;
const debouncedTask = debounce((task) => Promise.resolve(task()), 200);

const searchUseCase = (
  store: GetSearchResultsStore
): Promise<SearchResults> | undefined => {
  return debouncedTask(() => store.search(store.searchRequest));
};

export { searchUseCase };
