import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import NewsPage from '../../components/NewsPage';
import { isValidLanguage } from '../../lib/i18n/config';

const NEWS_DIR = path.join(process.cwd(), 'content/news');

export default function News({ searchParams }: { searchParams?: { lang?: string } }) {
    const files = fs.readdirSync(NEWS_DIR).filter((f) => f.endsWith('.md'));
    const newsList = files.map((file) => {
        const id = file.replace(/\\.md$/, '');
        const fileContent = fs.readFileSync(path.join(NEWS_DIR, file), 'utf-8');
        const { data } = matter(fileContent);
        return { id, ...data };
    });
    const lang = searchParams?.lang;
    const initialLanguage = lang && isValidLanguage(lang) ? lang : undefined;
    return <NewsPage initialLanguage={initialLanguage} newsList={newsList} data-oid="84zot4q" />;
}
