"use client"; import { useState } from "react"; import { TextField } from "@/components/ui/Field"; import { begrenzeSvnr, formatSvnr, requiresAustrianSvnr, svnrErrorMessage, svnrHaeltAuf, validateSvnr } from "@/lib/svnr"; type SvNummerFieldProps = { value: string; onChange: (value: string) => void; /** Country of the employee's work location — validation applies to Austria only. */ locationCountry: string | null | undefined; /** ISO yyyy-mm-dd; enables the cross-check against the TTMMJJ tail. */ birthDate?: string | null; dense?: boolean; /** * „Nicht verfügbar" — angeboten bei der Einstellung, wo die Nummer oft noch * fehlt: die Anmeldung läuft, die Karte kommt später. * * Der Haken steht im Entwurf und nicht in der Datenbank. Er sagt „jetzt * nicht", und das sagt das leere Feld bereits; im Entwurf beantwortet er * nach einer Unterbrechung die Frage, ob die Nummer vergessen wurde oder * noch nicht vorliegt. */ nichtVerfuegbar?: boolean; onNichtVerfuegbar?: (wert: boolean) => void; }; /** * Shared by the hire wizard and the "Daten ändern" panel so both apply the * same rule. Errors are shown only once the field has been left, so the * message does not flash while the ten digits are still being typed. * * Ein abweichendes Geburtsdatum ist kein Fehler, sondern ein Hinweis unter * dem Feld (siehe svnrHaeltAuf in lib/svnr.ts). */ export function SvNummerField({ value, onChange, locationCountry, birthDate, dense, nichtVerfuegbar, onNichtVerfuegbar, }: SvNummerFieldProps) { const [touched, setTouched] = useState(false); const applies = requiresAustrianSvnr(locationCountry); const befund = applies && value.trim() !== "" ? validateSvnr(value, birthDate) : null; const fehler = touched && befund && svnrHaeltAuf(befund) ? svnrErrorMessage(befund) : null; const hinweis = befund === "birthDateMismatch" ? "Die Nummer enthält ein anderes Geburtsdatum als der Stammsatz. Speichern ist möglich — bitte nur prüfen, ob es ein Zahlendreher ist." : applies ? "10 Ziffern: laufende Nummer, Prüfziffer, Geburtsdatum (TTMMJJ)." : undefined; return (