'use client';

import { suggestRelaxations, type FlightFilters } from '@/services/flights/filterSort';
import type { FlightData } from './types';
import './flight-filter-chips.css';

interface RelaxFilterChipsProps {
  /** Unfiltered offers — relaxations are measured against these, not the
   *  (empty) post-filter set, or every candidate would show 0 results. */
  offers: FlightData[];
  filters: FlightFilters;
  currency: string;
  onApply: (patch: Partial<FlightFilters>) => void;
}

/**
 * "Allow 1 stop · 34 results" / "Raise to $800 · 9 results" — one-tap
 * widenings for a filtered-to-nothing result set, each guaranteed to
 * actually return something (see suggestRelaxations). Shared by every
 * zero-results surface a filter (manual or Smart Filters) can produce,
 * rather than each one growing its own "try loosening a filter" copy.
 */
export default function RelaxFilterChips({ offers, filters, currency, onApply }: RelaxFilterChipsProps) {
  const relaxations = suggestRelaxations(offers, filters, currency);
  if (relaxations.length === 0) return null;

  return (
    <div className="d-flex flex-wrap justify-content-center gap-2 mt-3">
      {relaxations.map((r) => (
        <button
          key={r.key}
          type="button"
          className="flights-filter-chip flights-filter-chip--relax"
          onClick={() => onApply(r.patch)}
        >
          {r.label} · {r.resultCount} result{r.resultCount === 1 ? '' : 's'}
        </button>
      ))}
    </div>
  );
}
