'use client';

import { PropsWithChildren, useEffect } from 'react';
import { useTokenStore } from '@/store/token.state';
import { ShopItemApi } from '@/api/shop-item.api';
import { useShopItemsStore } from '@/store/shopItems.state';

export const ShopItemsProviver = ({ children }: PropsWithChildren) => {
    const { setShopItems } = useShopItemsStore();
    const { token } = useTokenStore();

    useEffect(() => {
        if (!token) {
            return;
        }
        const api = ShopItemApi.getInstance(token);
        api.getAllShopItems().then(shopItems => setShopItems(shopItems));
    }, [token]);

    return <>{children}</>;
};
