package got

import (
	"fmt"
	"strings"

	. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
	G "github.com/dave/jennifer/jen"
)

var (
	GenGetFilter = &Middleware{
		Id:   "filter",
		Type: "method",
		Fn: func(ctx *MiddlewareContext) error {
			var (
				method = ctx.Method
				entity = strings.ToLower(method.Entity)
			)
			ctx.Statement.Block(G.Id(`
			var (
				ok bool
				res *`).
				Qual(CODE_GEN_V2_COMMON, "ApiFilter").
				Id(fmt.Sprintf(`
			)
			if res, ok = filtersApiReference["%s"]; !ok {
				res = common.NewApiFilter("%s")
			}
			return res, nil
			`, entity, entity)))
			return nil
		},
	}
)
