import type { Metadata } from 'next';
import StorePage from '@/components/store/StorePage';
import StoreClosedPage from '@/components/store/StoreClosedPage';
import { loadStoreConfig } from '@/lib/storeConfig';
import { loadStoreCatalog } from '@/lib/storeCatalog';

type Props = { searchParams: Promise<{ vista_previa?: string }> };

// La vista previa del borrador no se indexa.
// eslint-disable-next-line react-refresh/only-export-components
export async function generateMetadata({ searchParams }: Props): Promise<Metadata> {
	const { vista_previa } = await searchParams;
	return vista_previa ? { robots: { index: false, follow: false, nocache: true } } : {};
}

export default async function MainPage({ searchParams }: Props) {
	const { vista_previa } = await searchParams;
	const [config, catalog] = await Promise.all([
		loadStoreConfig(vista_previa ? { previewToken: vista_previa } : undefined),
		loadStoreCatalog()
	]);
	if (config.status === 'closed') return <StoreClosedPage config={config} />;
	const esBorrador = Boolean(vista_previa) && config.status === 'draft';
	return (
		<StorePage
			config={config}
			catalog={catalog}
			vistaPrevia={esBorrador}
		/>
	);
}
