"use client";

import { Field, type FieldProps } from "formik";
import FilterCheckBox from "@/components/ui/FilterCheckBox";

/**
 * One boolean filter tick-box, wired to Formik.
 *
 * Six copies of this three-line render prop sat in `OpportunityFilterFlags`,
 * each annotated `({ field }: any)` — the only reason the `any` was there is
 * that `Field`'s render prop is generic and nobody had named the argument type.
 * `FieldProps<boolean>` is that name; it also makes `field.value` a boolean
 * rather than `any` at the point where it is negated.
 */
export default function FilterFlagField({
  name,
  label,
  onToggle,
}: {
  name: string;
  label: string;
  onToggle: (next: boolean) => void;
}) {
  return (
    <Field name={name}>
      {({ field }: FieldProps<boolean>) => (
        <FilterCheckBox
          id={name}
          label={label}
          checked={field.value}
          onChange={() => onToggle(!field.value)}
        />
      )}
    </Field>
  );
}
