const json =
    `[
                {
                    "id": 2000,
                    "name": "Autobahn"
                },
                {
                    "id": 2001,
                    "name": "Bus"
                },
                {
                    "id": 2002,
                    "name": "Autobus"
                },
                {
                    "id": 2003,
                    "name": "Fahrrad"
                },
                {
                    "id": 2004,
                    "name": "Tram"
                },
                {
                    "id": 2005,
                    "name": "Flugzeug"
                }
            ]`;



// check if JSON is valid
JSON.parse(json)

const requestDelay = 10

export default [
    {
        url: '/issue-251.json',
        method: 'get',
        rawResponse: async (req, res) => {
            res.setHeader('Content-Type', 'application/json')
            res.statusCode = 200
            
            const url= new URL(req.url, `http://${req.headers.host}`)
            const q = Object.fromEntries(url.searchParams)
            
            if (q && q.q) {
                const query = q.q.toLowerCase()
                const filtered = JSON.parse(json).filter(item => item.name.toLowerCase().includes(query))
                setTimeout(function() {
                    res.end(JSON.stringify(filtered))
                }, requestDelay);
                return
            }
            
            setTimeout(function() {
                res.end(json)
            }, requestDelay);

        },
    }

];