"use client";

import { v4 as uuidv4 } from 'uuid';
import { Provider } from 'react-redux';
import React, { FC, useEffect } from 'react';
import { PersistGate } from 'redux-persist/integration/react';
import { persistor, store } from './store';
import { getData, storeData } from '@/server_api/storage';


interface ReduxProviderProps {
    children: React.ReactNode;
    subdomain: string | null;
}

const ReduxProvider: FC<ReduxProviderProps> = ({ children, subdomain }) => {

    useEffect(() => {
        loadUuID();
    }, [subdomain, children]);

    const loadUuID = async () => {
        const getuuid = await getData('dynatech_uuid');
        if (!getuuid) {
            const uuid = uuidv4();
            storeData('dynatech_uuid', uuid);
        }
    };

    return (
        <Provider store={store}>
            <PersistGate loading={null} persistor={persistor}>
                {children}
            </PersistGate>
        </Provider>
    );
}

export default ReduxProvider;