import { parseMeasures } from "@/app/lib/parseMeasures";
import type { ClassifiedDeal } from "./types";

export function filterMeasureRows(
  deals: ClassifiedDeal[],
  instructedByDeal: Record<string, string[]>,
  search: string,
): ClassifiedDeal[] {
  const q = search.toLowerCase().trim();
  if (!q) return deals;

  return deals.filter((deal) => {
    if (deal.dealname?.toLowerCase().includes(q)) return true;
    if (deal.landlordPropertyId?.toLowerCase().includes(q)) return true;

    const proposed = parseMeasures(deal.proposedMeasures);
    if (proposed.some((m) => m.toLowerCase().includes(q))) return true;

    const instructed = instructedByDeal[deal.dealId] ?? [];
    if (instructed.some((m) => m.toLowerCase().includes(q))) return true;

    const techApproved = parseMeasures(deal.technicalApprovedMeasuresForInstall);
    if (techApproved.some((m) => m.toLowerCase().includes(q))) return true;

    return false;
  });
}
