package response

import (
	errs "gateway/internal/library/errors"
	"gateway/internal/library/errors/code"
	"gateway/internal/library/validate"
)

// RequestErrScope is used when mapping httpx.Parse / validation errors to business codes.
// Set once in main (e.g. response.RequestErrScope = code.Facade).
var RequestErrScope = code.Facade

// WrapRequestError maps binding / validation failures to InputInvalidFormat (HTTP 400).
// Business *errs.Error values are returned unchanged.
func WrapRequestError(err error) error {
	if err == nil {
		return nil
	}
	if errs.FromError(err) != nil {
		return err
	}
	if ve, ok := validate.AsErrors(err); ok {
		return ve.ToBusinessError(RequestErrScope)
	}

	return errs.For(RequestErrScope).InputInvalidFormat(err.Error())
}
