'use client';

import { PropsWithChildren, useEffect } from 'react';
import { ConfigBotApi } from '@/api/config-bot.api';
import { useTokenStore } from '@/store/token.state';

const configBotApi = ConfigBotApi.getInstance();

export const TokenProvider = ({ children }: PropsWithChildren) => {
    const { setToken } = useTokenStore();

    useEffect(() => {
        configBotApi.getConfigBot().then(cfg => {
            console.log('[TokenProvider] got config:', cfg);
            setToken(cfg.token);
        }).catch(err => {
            console.error('[TokenProvider] error getting config:', err);
        });
    }, []);

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