import axios from 'axios';
import { useState } from './useState';
import useNotify from './useNotify';

export function useTokenConfig() {
    const state = useState();
    const { notify } = useNotify();

    async function fetch() {
        try {
            const { data } = await axios.get('AccessTokenConfigs/findOne', {
                filter: { fields: ['renewInterval', 'renewPeriod'] },
            });
            if (!data) return;
            state.setTokenConfig(data);
            sessionStorage.setItem('renewPeriod', data.renewPeriod);
            return data;
        } catch (error) {
            notify('errors.tokenConfig', 'negative');
            console.error('Error fetching token config:', error);
        }
    }

    return {
        fetch,
        state,
    };
}
