/**
 * 庫存篩選 Hook
 * 處理庫存項目的搜尋和類型篩選邏輯
 */

import { useMemo } from "react";
import { WarehouseInventory } from "../types/warehouse";
import { AVAILABLE_PRODUCTS } from "../constants/products";

export const useInventoryFilter = (
  items: WarehouseInventory[],
  searchTerm: string,
  typeFilter: string
) => {
  return useMemo(() => {
    return items.filter((item) => {
      const matchesSearch =
        item.productName.toLowerCase().includes(searchTerm.toLowerCase()) ||
        item.batchNumber.toLowerCase().includes(searchTerm.toLowerCase());
      
      // 根據類型篩選
      let matchesType = true;
      if (typeFilter !== "all") {
        const product = AVAILABLE_PRODUCTS.find((p) => p.id === item.productId);
        matchesType = product?.type === typeFilter;
      }
      
      return matchesSearch && matchesType;
    });
  }, [items, searchTerm, typeFilter]);
};