'use client';

import { PropsWithChildren, useEffect } from 'react';
import { useTokenStore } from '@/store/token.state';
import { ConfigApi } from '@/api/config.api';
import { useConfigStore } from '@/store/config.state';

export const ConfigProvider = ({ children }: PropsWithChildren) => {
    const { setConfig } = useConfigStore();
    const { token } = useTokenStore();

    useEffect(() => {
        if (!token) {
            return;
        }
        const api = ConfigApi.getInstance(token);
        api.getConfig().then(config => setConfig(config.data));
    }, [token]);

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