"use client";

import { useLanguageStore } from "@/store/languageStore";
import { useTranslation } from "react-i18next";
import ModalInput from "@/components/ui/ModalInput";
import SelectInput from "@/components/ui/SelectInput";
import {
  nationalityNeedsPassport,
  nationalityResidencyOptions,
} from "@/data/Constants";
import {
  FIELD_LABEL_CLASS,
} from "./registerVolunteerParts";
import type { FormikProps } from "formik";
import type { RegisterVolunteerValues } from "./registerVolunteerValues";

/** A field group lifted out of the form of the same family. */
export default function RegisterVolunteerIdentityFields({
  values,
  setFieldValue,
}: {
  values: RegisterVolunteerValues;
  setFieldValue: FormikProps<RegisterVolunteerValues>["setFieldValue"];
}) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((s) => s.language);

  return (
  <div className="grid grid-cols-2 mobilescreen:gap-[0px] mobilescreen:grid-cols-1 gap-[100px]">
    <div>
      {nationalityNeedsPassport(values.nationality) ? (
        <>
          <p className={FIELD_LABEL_CLASS}>
            {t("COMMON.PASSPORT_NUMBER")}
          </p>
          <ModalInput
            name="passport_number"
            type="text"
            placeholder={t("COMMON.PASSPORT_NUMBER")}
            maxLength={20}
          />
        </>
      ) : (
        <>
          <p className={FIELD_LABEL_CLASS}>{t("COMMON.CIVIL_ID")}</p>
          <ModalInput
            name="civil_id"
            type="text"
            placeholder={t("COMMON.CIVIL_ID")}
            maxLength={12}
            digitsOnly
          />
        </>
      )}
    </div>
    <div>
      <p className={FIELD_LABEL_CLASS}>
        {t("COMMON.NATIONALITY")}
      </p>
      <SelectInput
        name="nationality"
        label={t("COMMON.NATIONALITY")}
        options={nationalityResidencyOptions.map((item) => ({
          label:
            selectedLanguage === "ar" ? item.name_ar : item.name_en,
          value: item.value,
        }))}
        onChange={(selectedOption) => {
          const value = selectedOption?.value || "";
          setFieldValue("nationality", value);
          if (nationalityNeedsPassport(value)) {
            setFieldValue("civil_id", "");
          } else {
            setFieldValue("passport_number", "");
          }
        }}
      />
    </div>
  </div>
  );
}
