import { Field, Select } from "./form";
import type { Metric } from "@/api/programme";
import { fmtInt } from "@/lib/format";

/** Chooses the variable an indicator is calculated from, and shows what it counts today. */
export function MetricSelect({
  label,
  value,
  onChange,
  metrics,
  error,
  required,
}: {
  label: string;
  value: string;
  onChange: (v: string) => void;
  metrics: Metric[];
  error?: string | undefined;
  required?: boolean;
}) {
  const m = metrics.find((x) => x.key === value);
  return (
    <Field
      label={label}
      required={!!required}
      error={error}
      help={m ? `${m.help} Now: ${fmtInt(m.total)}.` : undefined}
    >
      <Select
        value={value}
        onChange={onChange}
        options={metrics.map((x): [string, string] => [x.key, x.label])}
      />
    </Field>
  );
}
