"use client";

import { useTranslation } from "react-i18next";
import DatePicker, { DateObject } from "react-multi-date-picker";
import { cn } from "@/lib/helpers";
import { useBirthDateField } from "./useBirthDateField";
import AssetIcon from "@/components/ui/AssetIcon";

interface DatePickerProps {
  name: string;
  label?: string;
  minDate?: Date;
  maxDate?: Date;
  rmdpClassname?: string;
  disabled?: boolean;
  showDueDate?: boolean;
}

/* A plain component: no caller passes a ref, and the old `forwardRef` render function
   declared one parameter, which React warns about on every render in development. */
function BirthDateField({
    name,
    label,
    minDate,
    maxDate,
    rmdpClassname = "",
    disabled = false,
    showDueDate = false,
    ...props
  }: DatePickerProps) {
    const { t } = useTranslation();
    const {
      field,
      pickerValue,
      meta,
      isFocused,
      displayValue,
      datePickerRef,
      isRtl,
      handleInputChange,
      handleFocus,
      handleBlur,
      handleIconClick,
      handleChange,
    } = useBirthDateField(name);

    return (
      <div
        className={cn("relative w-full mb-4", rmdpClassname)}
        dir={isRtl ? "rtl" : "ltr"}
      >
        {label && (
          <label
            htmlFor={name}
            className={`absolute transition-all duration-200 text-primary-5 z-10
      ${isRtl ? "right-[9px]" : "left-[15px]"}
      ${
        isFocused || displayValue
          ? "text-[10px] top-[5px] bg-white px-1 text-primary-5/70"
          : "lg:text-base md:text-sm text-sm top-3"
      }
    `}
            onClick={handleIconClick}
          >
            {label}
          </label>
        )}

        <div
          className={`relative flex items-center ${
            isRtl ? "flex-row-reverse" : ""
          }`}
        >
          <DatePicker
            {...field}
            {...props}
            value={pickerValue}
            format="DD-MM-YYYY"
            onChange={(date: Date | DateObject | DateObject[] | null) =>
              handleChange(date, true)
            }
            onOpen={handleFocus}
            onClose={handleBlur}
            minDate={minDate}
            maxDate={maxDate}
            portal
            zIndex={9999}
            calendarPosition="bottom-center"
            disabled={disabled}
            containerClassName="w-full !h-full"
            inputClass={cn(
              "w-full p-3 pl-[19px] border rounded-2xl h-[48px] bg-[#29246D]/[0.03] text-primary-5 focus:outline-none",
              isRtl ? "text-right pr-3" : "text-left pl-[19px] ",
              meta.touched && meta.error
                ? "border-red-500"
                : "border-[#29246D1A]/10",
              isFocused || field.value ? "pt-6" : "pt-3",
              rmdpClassname,
            )}
            ref={datePickerRef}
            onOpenPickNewDate={false}
            inputMode="numeric"
            render={() => (
              <input
                /* On the input, not on `<DatePicker>` — see the note in
                   `DateField`: a `render` prop replaces the picker's own
                   input, so the id passed to the picker left the floating
                   `<label htmlFor={name}>` associated with nothing. */
                id={name}
                type="text"
                value={displayValue}
                onChange={handleInputChange}
                onFocus={handleFocus}
                onBlur={handleBlur}
                onClick={() => {
                  handleFocus();
                }}
                disabled={disabled}
                className={cn(
                  "w-full p-3 pl-[19px] border rounded-2xl h-[48px] bg-[#29246D]/[0.03] text-primary-5 focus:outline-none",
                  isRtl ? "text-right pr-3" : "text-left pl-[19px] ",
                  meta.touched && meta.error
                    ? "border-red-500"
                    : "border-[#29246D1A]/10",
                  isFocused || displayValue ? "pt-6" : "pt-3",
                  rmdpClassname,
                )}
                placeholder={isFocused || displayValue ? "" : undefined}
              />
            )}
          />

          <button
            type="button"
            aria-label={t("COMMON.OPEN_CALENDAR")}
            disabled={disabled}
            onClick={handleIconClick}
            className="absolute text-primary-5 z-10"
            style={{
              left: isRtl ? "10px" : "auto",
              right: isRtl ? "auto" : "10px",
              top: "50%",
              transform: "translateY(-50%)",
            }}
          >
            {showDueDate ? (
              <AssetIcon src="/assets/homepage/dateicon.svg" width={18} height={22} />
            ) : (
              <AssetIcon src="/assets/homepage/dateicon.svg" width={18} height={22} />
            )}
          </button>
        </div>

        {meta.touched && meta.error && (
          <div className="text-red-500 text-sm mt-1">{meta.error}</div>
        )}
      </div>
    );
}

export default BirthDateField;
