// src/hooks/useSearch.ts
import { useCallback, useEffect, useMemo, useState } from "react";

// src/utils/simpleSearch.ts
var MultiSubjectSearchWithMapping = (search, objects, mapping) => {
  return objects.filter((object) => {
    const mappedSearchKeywords = mapping(object)?.map((value) => value.toLowerCase().trim());
    if (!mappedSearchKeywords) {
      return true;
    }
    return search.every((searchValue) => !!mappedSearchKeywords.find((value) => !!value && value.includes(searchValue.toLowerCase().trim())));
  });
};

// src/hooks/useSearch.ts
var useSearch = ({
  list,
  initialSearch,
  searchMapping,
  additionalSearchTags,
  isSearchInstant = true,
  sortingFunction,
  filter,
  disabled = false
}) => {
  const [search, setSearch] = useState(initialSearch ?? "");
  const [result, setResult] = useState(list);
  const searchTags = useMemo(() => additionalSearchTags ?? [], [additionalSearchTags]);
  const updateSearch = useCallback((newSearch) => {
    const usedSearch = newSearch ?? search;
    if (newSearch) {
      setSearch(search);
    }
    setResult(MultiSubjectSearchWithMapping([usedSearch, ...searchTags], list, searchMapping));
  }, [searchTags, list, search, searchMapping]);
  useEffect(() => {
    if (isSearchInstant) {
      setResult(MultiSubjectSearchWithMapping([search, ...searchTags], list, searchMapping));
    }
  }, [searchTags, isSearchInstant, list, search, searchMapping, additionalSearchTags]);
  const filteredResult = useMemo(() => {
    if (!filter) {
      return result;
    }
    return result.filter(filter);
  }, [result, filter]);
  const sortedAndFilteredResult = useMemo(() => {
    if (!sortingFunction) {
      return filteredResult;
    }
    return filteredResult.sort(sortingFunction);
  }, [filteredResult, sortingFunction]);
  const usedResult = useMemo(() => {
    if (!disabled) {
      return sortedAndFilteredResult;
    }
    return list;
  }, [disabled, list, sortedAndFilteredResult]);
  return {
    result: usedResult,
    hasResult: usedResult.length > 0,
    allItems: list,
    updateSearch,
    search,
    setSearch
  };
};
export {
  useSearch
};
//# sourceMappingURL=useSearch.js.map