import NameIcon from "@icons/ContactFormIcon/NameIcon.tsx";
import EmailIcon from "@icons/ContactFormIcon/EmailIcon.tsx";
import TextIcon from "@icons/ContactFormIcon/TextIcon.tsx";
import AddressIcon from "@icons/ContactFormIcon/AddressIcon.tsx";
import { Dispatch, SetStateAction } from "react";
import { CustomInput } from "@/components/ViewPublicationPage/ContactForm/CustomInput/CustomInput.tsx";
import PhoneIcon from "@icons/ContactFormIcon/PhoneIcon.tsx";
import PhoneInput from "react-phone-number-input";
import { useQuizStore } from "@/stores/useQuizStore";
import { useTranslation } from "react-i18next";

type InputsProps = {
  name: string;
  setName: Dispatch<SetStateAction<string>>;
  email: string;
  setEmail: (email: string) => void;
  phone: string;
  setPhone: (phone: string) => void;
  text: string;
  setText: Dispatch<SetStateAction<string>>;
  adress: string;
  setAdress: Dispatch<SetStateAction<string>>;
  emailError?: string;
  phoneError?: string;
  onEmailBlur?: () => void;
  onPhoneBlur?: () => void;
  crutch: {
    disableEmail: boolean;
  };
};

const pathOnly = window.location.pathname;
const iscrutch =
  pathOnly === "/cc006b40-ccbd-4600-a1d3-f902f85aa0a0" || pathOnly === "/28525cd7-9ddf-4c4a-a55b-e3d2f7d47583";

export const Inputs = ({
  name,
  setName,
  email,
  setEmail,
  phone,
  setPhone,
  text,
  setText,
  adress,
  setAdress,
  emailError,
  phoneError,
  onEmailBlur,
  onPhoneBlur,
  crutch,
}: InputsProps) => {
  const { settings } = useQuizStore();
  const { t } = useTranslation();
  const FC = settings.cfg.formContact.fields;

  if (!FC) return null;
  const Name = (
    <CustomInput
      onChange={({ target }) => setName(target.value)}
      id={name}
      title={iscrutch ? "Введите имя и фамилию" : FC["name"].innerText || `${t("Enter")} ${t("Name").toLowerCase()}`}
      desc={FC["name"].text || t("Name")}
      Icon={NameIcon}
    />
  );
  const Email = (
    <CustomInput
      onChange={({ target }) => {
        setEmail(target.value.replaceAll(/\s/g, ""));
      }}
      onBlur={onEmailBlur}
      id={email}
      title={FC["email"].innerText || `${t("Enter")} Email`}
      desc={FC["email"].text || "Email"}
      Icon={EmailIcon}
      type="email"
      error={emailError}
    />
  );
  const Phone = (
    <CustomInput
      onChange={({ target }) => setText(target.value)}
      onChangePhone={(phone: string) => {
        setPhone(phone);
      }}
      onBlur={onPhoneBlur}
      value={phone}
      id={phone}
      title={FC["phone"].innerText || `${t("Enter")} ${t("Phone number").toLowerCase()}`}
      desc={FC["phone"].text || t("Phone number")}
      Icon={PhoneIcon}
      isPhone={true}
      error={phoneError}
    />
  );
  const Text = (
    <CustomInput
      onChange={({ target }) => setText(target.value)}
      id={text}
      title={FC["text"].text || `${t("Enter")} ${t("Last name").toLowerCase()}`}
      desc={FC["text"].innerText || t("Last name")}
      Icon={TextIcon}
    />
  );
  const Adress = (
    <CustomInput
      onChange={({ target }) => setAdress(target.value)}
      id={adress}
      title={FC["address"].innerText || `${t("Enter")} ${t("Address").toLowerCase()}`}
      desc={FC["address"].text || t("Address")}
      Icon={AddressIcon}
    />
  );

  if (Object.values(FC).some((data) => data.used)) {
    return (
      <>
        {FC["name"].used ? Name : <></>}
        {FC["email"].used && !crutch.disableEmail ? Email : <></>}
        {FC["phone"].used ? Phone : <></>}
        {FC["text"].used ? Text : <></>}
        {FC["address"].used ? Adress : <></>}
      </>
    );
  } else {
    return (
      <>
        {Name}
        {Email}
        {Phone}
      </>
    );
  }
};
