import {Plugin} from "vite";

export interface WailsPluginOptions {
    excludeUrls?: string[]
}

const wailsPlugin:(options?:WailsPluginOptions)=>Plugin = (options)=>({
    name: "wailsPlugin",
    configureServer(server) {
        server.middlewares.use((req, res, next) => {
            if(!options){
                next()
                return
            }

            if(!options.excludeUrls){
                next()
                return
            }

            let excludeUrls = options.excludeUrls || []
            for (let i = 0; i < excludeUrls.length; i++) {
                let excludeUrl = excludeUrls[i]
                let reg = new RegExp(excludeUrl)
                if(reg.test(req.originalUrl)){
                    res.writeHead(404)
                    res.end()
                    return
                }
            }
            next()
        })
    },
})

export default wailsPlugin;