import fsPromises from 'fs/promises'
import path from 'path'

export default async function MapApi(req: any, res: any) {
  const { map } = req.query
  try {
    // get list of all files(maps) in the readyornot folder - maybe there is a cleaner way to do this?
    // we filter out any subdirectories as well as any files not ending in png or jpg
    var fs = require('fs')
    var maps = fs.readdirSync(path.join(process.cwd(), "/public/images/" + map + "/"), { withFileTypes: true })
      .filter((file: any) => file.isFile())
      .filter((file: any) => file.name.endsWith(".jpg") || file.name.endsWith(".png"))
      .map((file: any) => '/images/' + map + '/' + file.name);


    // get Empire and Game name first to create an EmpireData object
    res.status(200).json(maps)
  }
  catch (error) {
    console.log(error)
    res.status(500).json({ error: 'Error reading data' })
  }
}