import { useEffect } from "react"
import { getConsumables, parseConsumables } from "../utils/lib"
import useStore from "../utils/store"

export const ConsumablesManager = () => {
  const auth = useStore((state) => state.auth)
  const setAuth = useStore((state) => state.setAuth)
  const setConsumables = useStore((state) => state.setConsumables)

  useEffect(() => {
    if (auth.bcToken) {
      fetchConsumables()
    }
  }, [auth])

  const fetchConsumables = async () => {
    try {
      const consumables = await getConsumables(auth.bcToken)
      console.log("CONSUMABLES", parseConsumables(consumables.voids))
      setConsumables(parseConsumables(consumables.voids))
    } catch (error) {
      setAuth({
        ...auth,
        bcToken: "",
        rigoToken: "",
      })
    }
  }

  return <></>
}
