import { useCallback } from "react";
import { useSearchFields } from "./useSearchFields.js";
import { wrapFilterInHasOnes } from "@contember/binding";
const useCreateOptionsFilter = (desugaredOptionPath, searchByFields, lazy) => {
  const desugaredSearchFields = useSearchFields(searchByFields);
  const lazyFilter = lazy && typeof lazy === "object" && lazy.createFilter;
  return useCallback((input) => {
    if (lazyFilter) {
      return lazyFilter(input);
    }
    if (!input) {
      return void 0;
    }
    if (desugaredSearchFields.length) {
      const or = [];
      for (const field of desugaredSearchFields) {
        or.push(wrapFilterInHasOnes(field.hasOneRelationPath, {
          [field.field]: { containsCI: input }
        }));
      }
      return { or };
    }
    if ("field" in desugaredOptionPath) {
      return wrapFilterInHasOnes(desugaredOptionPath.hasOneRelationPath, {
        [desugaredOptionPath.field]: { containsCI: input }
      });
    }
    return void 0;
  }, [desugaredOptionPath, desugaredSearchFields, lazyFilter]);
};
export { useCreateOptionsFilter };
//# sourceMappingURL=useCreateOptionsFilter.js.map
