"use client"; import { Layers } from "lucide-react"; import type { InspectionFormValues } from "@/app/(app)/inspections/new/inspection-form-values"; import { FEED_CONDITION_LABELS, ratingToCondition } from "@/lib/colonies/condition"; import { COLONY_CONDITION_LABELS } from "@/lib/colonies/labels"; import type { Colony, ColonyCondition } from "@/lib/colonies/types"; import { CheckboxField } from "@/components/form/checkbox-field"; import { COLONY_TAKEOVER_FIELDS, type ColonyTakeoverField } from "@/lib/inspections/types"; export interface ColonyTakeoverSectionProps { /** What the colony card holds now; null when it is not known (nothing loaded yet). */ colony: Pick | null; /** What this inspection recorded. */ values: Pick; selected: ColonyTakeoverField[]; onChange: (fields: ColonyTakeoverField[]) => void; /** Without colonies:update the section is not offered at all. */ canUpdateColony: boolean; } interface TakeoverRow { field: ColonyTakeoverField; label: string; } const NOT_RECORDED = "noch nicht erfasst"; const UNKNOWN = "bisher unbekannt"; function conditionText( condition: ColonyCondition | null | undefined, labels: Record, ): string { if (condition === undefined) return UNKNOWN; return condition === null ? NOT_RECORDED : labels[condition]; } function rowText(name: string, before: string, after: string): string { return before === after ? `${name}: ${after} (unverändert)` : `${name}: ${before} → ${after}`; } /** One row per colony-card value this inspection has an answer for. */ function takeoverRows({ colony, values }: Pick): TakeoverRow[] { const rows: TakeoverRow[] = []; for (const field of COLONY_TAKEOVER_FIELDS) { if (field === "occupiedFrames" && values.colonyStrength.trim() !== "") { const before = colony === null ? UNKNOWN : colony.occupiedFrames === null ? NOT_RECORDED : String(colony.occupiedFrames); rows.push({ field, label: rowText("Besetzte Waben", before, String(Number(values.colonyStrength))), }); } const brood = ratingToCondition(values.broodPattern); if (field === "broodStatus" && brood !== null) { rows.push({ field, label: rowText( "Brut", conditionText(colony?.broodStatus, COLONY_CONDITION_LABELS), COLONY_CONDITION_LABELS[brood], ), }); } if (field === "feedStatus" && values.feedStatus !== null) { rows.push({ field, label: rowText( "Futter", conditionText(colony?.feedStatus, FEED_CONDITION_LABELS), FEED_CONDITION_LABELS[values.feedStatus], ), }); } } return rows; } function countText(count: number): string { if (count === 0) return "Es wird nichts auf die Volkskarte übernommen."; if (count === 1) return "1 Wert wird auf die Volkskarte übernommen."; return `${count} Werte werden auf die Volkskarte übernommen.`; } /** * „Volkskarte“ above the save button: which of today's answers also go onto * the colony card, each as „bisher → neu“ with a box. Ticked are exactly the * fields filled in this inspection; nothing is taken over that was not asked. */ export function ColonyTakeoverSection({ colony, values, selected, onChange, canUpdateColony, }: ColonyTakeoverSectionProps) { if (!canUpdateColony) { return null; } const rows = takeoverRows({ colony, values }); function toggle(field: ColonyTakeoverField, checked: boolean) { onChange( COLONY_TAKEOVER_FIELDS.filter((candidate) => candidate === field ? checked : selected.includes(candidate), ), ); } const takenCount = rows.filter((row) => selected.includes(row.field)).length; return (
{rows.length > 0 ? (
Volkskarte

Diese Angaben auch auf der Volkskarte eintragen:

{rows.map((row) => ( toggle(row.field, checked)} /> ))}
) : null} {/* Mounted even while empty, or its first message is not announced. */}

{rows.length > 0 ? countText(takenCount) : ""}

); }