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 Birthplace() {
    const dispatch = useDispatch();
    const { translate } = useTranslations(ELocalesPlacement.CompatibilityV4);
    const { birthplace } = useSelector(selectors.selectCompatibilityV4Answers);
    const [isValidBirthplace, setIsValidBirthplace] = useState(false);
    const navigate = useNavigate();

    useEffect(() => {
        if (birthplace) {
            setIsValidBirthplace(true);
        }
    }, [birthplace])

    const handleValidBirthplace = (birthplace: string) => {
        if (birthplace) {
            dispatch(
                actions.compatibilityV4Answers.update({
                    birthplace,
                })
            );
            dispatch(actions.questionnaire.update({ birthPlace: birthplace }));
        }
        setIsValidBirthplace(true);
    };

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

    return (
        <div className={styles.container}>
            <Title variant="h2" className={styles.title}>
                {translate("/birthplace.title")}
            </Title>
            <p className={styles.text}>
                {translate("/birthplace.text")}
            </p>
            <BirthplaceInput
                value={birthplace}
                placeholder={translate("/birthplace.placeholder")}
                inputClassName={styles.input}
                placeholderClassName={styles.placeholder}
                onValid={handleValidBirthplace}
                onInvalid={() => setIsValidBirthplace(true)}
            />
            <Button
                className={styles.button}
                disabled={!isValidBirthplace}
                onClick={handleClick}
            >
                {translate("next")}
            </Button>
        </div>
    )
}

export default Birthplace