"use client"; import { useActionState, useEffect, useRef, useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldLegend, FieldSet, } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { InfoIcon } from "lucide-react"; import { submitCitizenshipApplication } from "@/lib/citizenship/actions"; import { initialFormState } from "@/lib/form-action"; import { EYE_COLORS } from "@/lib/citizenship/types"; import { Spinner } from "@/components/ui/spinner"; import { ImageUploader } from "@/components/image-uploader"; import { removeBackground } from "@imgly/background-removal"; type Props = { firstName: string; secondName: string; sex: string; birthDate: Date; regions: { id: string; name: string }[]; }; function formatDate(d: Date | null): string { if (!d) return ""; return d.toISOString().split("T")[0]; } type AppType = "PREVIOUS_CITIZENSHIP" | "NATURALIZATION"; export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDate, regions }: Props) { const [appType, setAppType] = useState(""); const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState); const [isEuResident, setIsEuResident] = useState(null); const [profileProcessing, setProfileProcessing] = useState(false); const profileInputRef = useRef(null); const signatureInputRef = useRef(null); useEffect(() => { if (state.error) { toast.error(state.error); } }, [state.error]); const errors = state.fieldErrors ?? {}; async function handleProfileSelect(file: File) { setProfileProcessing(true); try { const resultBlob = await removeBackground(file); const processedFile = new File([resultBlob], file.name, { type: "image/png" }); const dt = new DataTransfer(); dt.items.add(processedFile); if (profileInputRef.current) profileInputRef.current.files = dt.files; } catch { const dt = new DataTransfer(); dt.items.add(file); if (profileInputRef.current) profileInputRef.current.files = dt.files; } finally { setProfileProcessing(false); } } function handleSignatureSelect(file: File) { const dt = new DataTransfer(); dt.items.add(file); if (signatureInputRef.current) signatureInputRef.current.files = dt.files; } function handleProfileRemove() { if (profileInputRef.current) profileInputRef.current.value = ""; } function handleSignatureRemove() { if (signatureInputRef.current) signatureInputRef.current.value = ""; } return (
Personal information Important notice Personal data is sourced from your account profile. Please ensure that all information is accurate. If corrections are required, update your details in Kotosoft ID and synchronise your account. Surname Given names
Sex Date of birth
Eligibility basis Select the basis on which you are applying for citizenship. setAppType(v as AppType)} aria-invalid={!!errors.applicationType?.length} required > Previous citizenship holder I held citizenship of the Duspean Republic or the Country of Reality on 31 March 2026. Naturalisation I am a foreign national seeking naturalisation. {errors.applicationType && {errors.applicationType[0]}}
{appType === "PREVIOUS_CITIZENSHIP" && (
Previous citizenship details Country {errors.previousCountry && {errors.previousCountry[0]}} Region of issuance The region or authority that issued your previous citizenship. {errors.previousRegion && {errors.previousRegion[0]}}
)} {appType === "NATURALIZATION" && (
Naturalisation statement Why do you wish to become a citizen of the European Federation?