import { useEffect, useState } from "react";
import { IOption } from "../../common/domain/entities/option";
import { useDataContext } from "../../common/contexts/data_context";


export const useChangeInterests = () => {
  const { profileRepository } = useDataContext();
  const [interests, setInterests] = useState<IOption[]>([]);

  const toogleInterest = (id: number) => {
    const newInterests = interests.map((interest) => {
      if (interest.id === id) {
        return { ...interest, isSelected: !interest.isSelected };
      }
      return interest;
    });
    setInterests(newInterests);
  };

  const onSubmit = async () => {
    const selectedInterests = interests
      .filter((interest) => interest.isSelected);
    await profileRepository!.saveInterests(selectedInterests);
  };

  const setAllInterests = (interests: IOption[]) => {
    setInterests(interests);
  };

  return { toogleInterest, interests, onSubmit, setAllInterests };
};
