'use client'

import { useProductStore } from "@/app/product/store/Store";
import { usePathname } from "next/navigation";
import { useEffect, useRef } from "react";

const ManageData = () => {

    const { setVariantId } = useProductStore()
    const pathname = usePathname()
    const prevPathnameRef = useRef(pathname)

    useEffect(() => {
        const prevPath = prevPathnameRef.current
        const isLeavingProduct = prevPath?.startsWith('/product/') && !pathname.startsWith('/product/')

        if (isLeavingProduct) {
            setVariantId('')
        }

        prevPathnameRef.current = pathname
    }, [pathname, setVariantId]);

    return null
}

export default ManageData