var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/hooks/useSearch.ts
var useSearch_exports = {};
__export(useSearch_exports, {
  useSearch: () => useSearch
});
module.exports = __toCommonJS(useSearch_exports);
var import_react = require("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] = (0, import_react.useState)(initialSearch ?? "");
  const [result, setResult] = (0, import_react.useState)(list);
  const searchTags = (0, import_react.useMemo)(() => additionalSearchTags ?? [], [additionalSearchTags]);
  const updateSearch = (0, import_react.useCallback)((newSearch) => {
    const usedSearch = newSearch ?? search;
    if (newSearch) {
      setSearch(search);
    }
    setResult(MultiSubjectSearchWithMapping([usedSearch, ...searchTags], list, searchMapping));
  }, [searchTags, list, search, searchMapping]);
  (0, import_react.useEffect)(() => {
    if (isSearchInstant) {
      setResult(MultiSubjectSearchWithMapping([search, ...searchTags], list, searchMapping));
    }
  }, [searchTags, isSearchInstant, list, search, searchMapping, additionalSearchTags]);
  const filteredResult = (0, import_react.useMemo)(() => {
    if (!filter) {
      return result;
    }
    return result.filter(filter);
  }, [result, filter]);
  const sortedAndFilteredResult = (0, import_react.useMemo)(() => {
    if (!sortingFunction) {
      return filteredResult;
    }
    return filteredResult.sort(sortingFunction);
  }, [filteredResult, sortingFunction]);
  const usedResult = (0, import_react.useMemo)(() => {
    if (!disabled) {
      return sortedAndFilteredResult;
    }
    return list;
  }, [disabled, list, sortedAndFilteredResult]);
  return {
    result: usedResult,
    hasResult: usedResult.length > 0,
    allItems: list,
    updateSearch,
    search,
    setSearch
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useSearch
});
//# sourceMappingURL=useSearch.cjs.map