'use client';
import StoreProvider from './store';
<% if (functionality.includes('antd')) { -%>
import { ConfigProvider } from 'antd';
import enUS from 'antd/lib/locale/en_US';
<% } -%>
<% if (lib.includes('aelf-web-login')) { -%>
import WebLoginProvider from './webLoginProvider';
<% } -%>

import { useEffect, useState } from 'react';
import { store } from 'redux/store';
import Loading from 'components/Loading';
import { setEthData } from 'redux/reducer/data';

import { fetchEtherscan } from 'api/request';

function Provider({ children }: { children: React.ReactNode }) {
	const [loading, setLoading] = useState(true);
	const storeConfig = async () => {
		const { result } = await fetchEtherscan();
		store.dispatch(setEthData(result));
		setLoading(false);
	};
	useEffect(() => {
		storeConfig();
	}, []);
	return (
		<>
			<StoreProvider>
<% if (functionality.includes('antd')) { -%>
				<ConfigProvider
					locale={enUS}
					autoInsertSpaceInButton={false}
				>
<% } -%>
					{loading ? (
						<Loading></Loading>
					) : (
<% if (lib.includes('aelf-web-login')) { -%>
						<WebLoginProvider>{children}</WebLoginProvider>
<% } else { -%>
					<>{children}</>
<% } -%>
					)}

<% if (functionality.includes('antd')) { -%>
				</ConfigProvider>
<% } -%>
			</StoreProvider>
		</>
	);
}

export default Provider;
