import { ApiError } from '@/lib/types' import { useState, useCallback } from 'react' import { root } from '@repo/core' import { TemplateSocialController } from "@repo/sdk" import { useError } from "../data/use-error" export const useTemplateInteraction = () => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const likeTemplate = useCallback(async (templateId: string) => { setLoading(true) setError(null) const templateSocial = root.get(TemplateSocialController) const { data, error } = await useError(async () => await templateSocial.like({ templateId })) if (error) { setError(error) setLoading(false) return { success: false, error } } setLoading(false) return { success: data?.success || true, error: null } }, []) const unlikeTemplate = useCallback(async (templateId: string) => { setLoading(true) setError(null) const templateSocial = root.get(TemplateSocialController) const { data, error } = await useError(async () => await templateSocial.unlike({ templateId })) if (error) { setError(error) setLoading(false) return { success: false, error } } setLoading(false) return { success: data?.success || true, error: null } }, []) const checkLiked = useCallback(async (templateId: string) => { const templateSocial = root.get(TemplateSocialController) const { data, error } = await useError(async () => await templateSocial.checkLiked({ templateId })) if (error) { return { liked: false, error } } return { liked: data?.liked || false, error: null } }, []) const favoriteTemplate = useCallback(async (templateId: string) => { setLoading(true) setError(null) const templateSocial = root.get(TemplateSocialController) const { data, error } = await useError(async () => await templateSocial.favorite({ templateId })) if (error) { setError(error) setLoading(false) return { success: false, error } } setLoading(false) return { success: data?.success || true, error: null } }, []) const unfavoriteTemplate = useCallback(async (templateId: string) => { setLoading(true) setError(null) const templateSocial = root.get(TemplateSocialController) const { data, error } = await useError(async () => await templateSocial.unfavorite({ templateId })) if (error) { setError(error) setLoading(false) return { success: false, error } } setLoading(false) return { success: data?.success || true, error: null } }, []) const checkFavorited = useCallback(async (templateId: string) => { const templateSocial = root.get(TemplateSocialController) const { data, error } = await useError(async () => await templateSocial.checkFavorited({ templateId })) if (error) { return { favorited: false, error } } return { favorited: data?.favorited || false, error: null } }, []) return { loading, error, likeTemplate, unlikeTemplate, checkLiked, favoriteTemplate, unfavoriteTemplate, checkFavorited, } }