import Title from "@/components/Title";
import styles from "./styles.module.scss";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";
import Button from "../../components/Button";
import routes from "@/routes";
import { useNavigate } from "react-router-dom";
import { useSelector } from "react-redux";
import { selectors, actions } from "@/store";
import { useDispatch } from "react-redux";
import { useEffect, useState } from "react";
import BirthplaceInput from "@/components/pages/ABDesign/v1/pages/EmailEnterPage/BirthplaceInput";

function FormerPartnerName() {
    const dispatch = useDispatch();
    const { translate } = useTranslations(ELocalesPlacement.CompatibilityV4);
    const { formerPartnerName } = useSelector(selectors.selectCompatibilityV4Answers);
    const [isValidFormerPartnerName, setIsValidFormerPartnerName] = useState(false);
    const navigate = useNavigate();

    useEffect(() => {
        if (formerPartnerName) {
            setIsValidFormerPartnerName(true);
        }
    }, [formerPartnerName])

    const handleValidFormerPartnerName = (formerPartnerName: string) => {
        if (formerPartnerName) {
            dispatch(
                actions.compatibilityV4Answers.update({
                    formerPartnerName,
                })
            );
        }
        setIsValidFormerPartnerName(true);
    };

    const handleClick = () => {
        navigate(routes.client.compatibilityV4FormerPartnerBirthdate());
    }

    return (
        <div className={styles.container}>
            <Title variant="h2" className={styles.title}>
                {translate("/former-partner-name.title")}
            </Title>
            <p className={styles.text}>
                {translate("/former-partner-name.text")}
            </p>
            <BirthplaceInput
                value={formerPartnerName}
                placeholder={translate("/former-partner-name.placeholder")}
                inputClassName={styles.input}
                placeholderClassName={styles.placeholder}
                onValid={handleValidFormerPartnerName}
                onInvalid={() => setIsValidFormerPartnerName(false)}
            />
            <Button
                className={styles.button}
                disabled={!isValidFormerPartnerName}
                onClick={handleClick}
            >
                {translate("next")}
            </Button>
        </div>
    )
}

export default FormerPartnerName