import { fetchWishList } from "@/apis/user";
import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { useAbortController } from "./useAbortController";

export const useWishList = () => {
    const [wishList, setWishList] = useState<any[]>([]);
    const { getSignal } = useAbortController();
    const [searchParams]= useSearchParams();
    
    const locationCode = searchParams.get("locationCode") || '';
    const token = searchParams.get("token") || '';

    const getWishList = async () => {
        const res = await fetchWishList({params:{locationCode:locationCode},options:{signal:getSignal(),headers: { Authorization: `Bearer ${token}` },}});
        const _wishList = res.result as any[];
        setWishList(_wishList);
    };

    useEffect(() => {
        if(locationCode){
            getWishList();
        }
    }, [locationCode]);

    return {
        wishList,
    };
};
