import { useEffect, useState } from "react"
import { saveLike, removeLike } from "@/services/home"

export default function (initailState, targetType, targetId) {
  const [isLike, setIsLike] = useState(initailState)
  useEffect(() => {
    setIsLike(initailState)
  }, [initailState])
  const toggleLike = () => {
    if (isLike) {
      removeLike(targetType, targetId).then(() => {
        setIsLike(false)
      })
    } else {
      saveLike(targetType, targetId).then(() => {
        setIsLike(true)
      })
    }
  }
  return [isLike, toggleLike]
}
