import {useState, useMemo} from "react";
import Input from "@/components/Low/Input";
import { search } from "@/utils/search";

export function useListSearch<T>(fields: string[][], rows: T[]) {
	const [text, setText] = useState("");

	const renderSearch = () => <Input label="Search" type="text" name="search" onChange={setText} placeholder="Enter search text" value={text} />;

	const updatedRows = useMemo(() => {
		return search(text, fields, rows);
	}, [fields, rows, text]);

	return {
		text,
		rows: updatedRows,
		renderSearch,
	};
}
