import Title from "@/components/Title";
import styles from "./styles.module.scss";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";
import { IAnswersSessionCompatibilityV2 } from "@/api/resources/Session";
import { useMemo } from "react";
import Answer from "../../components/Answer";
import { useSelector } from "react-redux";
import { actions, selectors } from "@/store";
import { useDispatch } from "react-redux";
import { useSession } from "@/hooks/session/useSession";
import { ESourceAuthorization } from "@/api/resources/User";
import routes from "@/routes";
import { useNavigate } from "react-router-dom";
import { sleep } from "@/services/date";
import { answerTimeOut } from "../../data";

function YourPriority() {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { updateSession } = useSession();
  const { translate } = useTranslations(ELocalesPlacement.CompatibilityV2);
  const { yourPriority } = useSelector(selectors.selectCompatibilityV2Answers);

  const answers: {
    id: IAnswersSessionCompatibilityV2["your_priority"];
    title: string;
  }[] = useMemo(
    () => [
      {
        id: "inner_harmony",
        title: translate("/your-priority.answer1"),
      },
      {
        id: "reliable_relationship",
        title: translate("/your-priority.answer2"),
      },
      {
        id: "new_impressions",
        title: translate("/your-priority.answer3"),
      },
    ],
    [translate]
  );

  const handleClick = async (
    id: IAnswersSessionCompatibilityV2["your_priority"]
  ) => {
    dispatch(actions.compatibilityV2Answers.update({ yourPriority: id }));
    updateSession(
      {
        answers: {
          your_priority: id,
        },
      },
      ESourceAuthorization["aura.compatibility.v2"]
    );
    if (id !== yourPriority) await sleep(answerTimeOut);
    navigate(routes.client.compatibilityV2PersonalizedRelationshipAnalysis());
  };

  return (
    <div className={styles.container}>
      <Title variant="h2" className={styles.title}>
        {translate("/your-priority.title")}
      </Title>
      {answers.map((answers, index) => (
        <Answer
          key={index}
          answer={answers}
          isSelected={yourPriority === answers.id}
          onClick={() => handleClick(answers.id)}
        />
      ))}
    </div>
  );
}

export default YourPriority;
