import { useState } from "react";

const useSearch = (data, query) => {
  const [searchQuery, setSearchQuery] = useState(query);

  const filteredData = () => {
    if (!searchQuery) {
      return data;
    }

    return data.filter(
      (item) =>
        item.firstName.toLowerCase().includes(searchQuery.toLowerCase()) ||
        item.lastName.toLowerCase().includes(searchQuery.toLowerCase()) ||
        item.phoneNumber.toLowerCase().includes(searchQuery.toLowerCase())
    );
  };

  return {
    filteredData: filteredData(),
    setSearchQuery,
  };
};

export default useSearch;
