import { useEffect } from '@wordpress/element';
import { useSettings } from '../../../contexts/SettingsContext';
import { getConfig } from '../../../Helpers';
import { SettingsStoreProvider } from './SettingsStore';
import AnalyticsShell from './AnalyticsShell';

/**
 * Entry of the Sales Analytics admin page (admin.php?page=ssgsw-analytics).
 * Licensed sites get one shared settings store; Free sites get the locked sample preview.
 */
export default function AnalyticsApp() {
	const { isPro, isSetupComplete } = useSettings();

	useEffect( () => {
		// The menu item only exists once setup is complete; guard direct visits anyway.
		if ( ! isSetupComplete ) {
			window.location.replace( getConfig().admin_page_url || 'admin.php?page=ssgsw-admin' );
		}
	}, [ isSetupComplete ] );

	if ( ! isSetupComplete ) {
		return null;
	}

	return (
		<div className="ssgsw-an-app">
			{ isPro ? (
				<SettingsStoreProvider>
					<AnalyticsShell licensed />
				</SettingsStoreProvider>
			) : (
				<AnalyticsShell licensed={ false } />
			) }
		</div>
	);
}
