import { defineConfig } from '@adonisjs/cors'

/**
 * Configuration options to tweak the CORS policy. The following
 * options are documented on the official documentation website.
 *
 * https://docs.adonisjs.com/guides/security/cors
 */
const corsConfig = defineConfig({
  origin: (requestOrigin) => {
    /**
     * Allow localhost on any port during development.
     */
    if (
      requestOrigin.includes('localhost') ||
      requestOrigin.includes('0.0.0.0') ||
      requestOrigin.includes('127.0.0.1') ||
      requestOrigin.includes('::1')) {
      return true
    }
    return false
  },

  methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
  headers: true,
  credentials: true,
  maxAge: 90,
})

export default corsConfig
