"use client";

import dynamic from "next/dynamic";
import { useEffect } from "react";
import { useFormikContext } from "formik";
import { isUnderageToday } from "@/lib/age";

/** Pieces lifted out of `RegisterVolunteerModalForm`. */

export const CountryCodeSelect = dynamic(
  () => import("@/components/ui/CountryCodeSelect"),
  {
    ssr: false,
    loading: () => (
      <div className="h-[48px] w-[90px] rounded-2xl bg-gray-100 animate-pulse" />
    ),
  }
);

/** Re-exported so existing importers keep working off one definition. */
export type { ChoiceItem } from "@/lib/choices";

/**
 * A `useEffect` cannot live in Formik's render prop: that callback runs during
 * the parent's render and is not a component, so a hook called there breaks the
 * rules of hooks. Hence the watcher is its own child component, which is a
 * real component and can hold the effect.
 */
export function DobWatcher({
  setIsUnder18,
}: {
  setIsUnder18: (value: boolean) => void;
}) {
  const { values } = useFormikContext<{ dob: string }>();

  useEffect(() => {
    setIsUnder18(isUnderageToday(values.dob));
  }, [values.dob, setIsUnder18]);

  return null;
}

/** The shared label class for this form's fields. */
export const FIELD_LABEL_CLASS =
  "text-secondary-100 2xl:text-[25px] lg:text-lg md:text-lg xss:text-sm font-semibold 2xl:pb-5 laptopmain:pb-2 pb-2";

export default CountryCodeSelect;
