import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
import { productCategories } from './data';

export async function GET(request: Request) {
  let data = productCategories;

  const cookieStore = await cookies();
  const lang = (await cookieStore).get('lang')?.value || 'fa';

  if (lang === 'fa') {
    data = productCategories.map(
      ({
        id,
        title,
        fa_title,
        icon,
        typesCount,
        priceTypeTitle,
        fa_priceTypeTitle,
        bestForTitle,
        fa_bestForTitle,
        description,
        fa_description,
      }) => {
        if (lang === 'fa') {
          return {
            id,
            icon,
            typesCount,
            title: fa_title || title,
            priceTypeTitle: fa_priceTypeTitle || priceTypeTitle,
            bestForTitle: fa_bestForTitle || bestForTitle,
            description: fa_description || description,
          };
        }
        return {
          id,
          icon,
          typesCount,
          title,
          priceTypeTitle,
          bestForTitle,
          description,
        };
      },
    );
  }

  return NextResponse.json({ data: productCategories });
}
