import { useEffect, useState } from "react"
import { saveCollection, removeCollection } from "@/services/home"

export default function (initailState, targetType, targetId) {
  const [isSaved, setIsSaved] = useState(initailState)
  useEffect(() => {
    setIsSaved(initailState)
  }, [initailState])
  const toggleSave = () => {
    if (isSaved) {
      removeCollection(targetType, targetId).then(() => {
        setIsSaved(false)
      })
    } else {
      saveCollection(targetType, targetId).then(() => {
        setIsSaved(true)
      })
    }
  }
  return [isSaved, toggleSave]
}
