package cors

import (
	"net/http"
	"strings"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func Cors() gin.HandlerFunc {
	config := cors.DefaultConfig()

	config.AddAllowMethods(http.MethodDelete, http.MethodOptions, http.MethodPatch)
	config.AddAllowHeaders("Authorization", "X-Require-Cookie", "X-Device")
	config.AllowCredentials = true
	config.AllowOriginFunc = func(origin string) bool {
		if gin.Mode() == gin.DebugMode {
			return true
		}

		if strings.Contains(origin, "links123.com") {
			return true
		}

		return false
	}

	return cors.New(config)
}
