package formula

import "strings"

// sessionFormulaGlobalRoots is the canonical set of session-bound formula
// merge-field global roots — Salesforce v63's $User / $Profile / $UserRole /
// $Permission, which the engine normalizes to __User / __Profile /
// __UserRole / __Permission during expression rewrite.
//
// This is the SSoT for the question "is this identifier a session-bound
// formula global?" Validation, flow, approval, and action engines MUST read
// it via IsSessionFormulaGlobalRoot rather than restating the names through
// per-engine maps or switches. CLAUDE.md "Coding Red Lines" treats
// SObject-name branches inside generic engines as a violation; these
// identifiers are formula-language tokens (like `record` / `prior`), not
// SObject names, and their authoritative home is the formula package.
//
// Construction surfaces that populate the corresponding context keys:
//
//   - __User, __Profile, __UserRole — formula.BuildFormulaUserContext
//   - __Permission                  — validation_service.withFormulaPermissionContext (non-super-user)
//                                     formula.InjectSuperUserPermissions (super-user, all consumers)
//
// If a new session-bound global is introduced, add it here; the
// IsSessionFormulaGlobalRoot/BuildFormulaUserContext consistency test will
// otherwise fail and force the update.
var sessionFormulaGlobalRoots = map[string]struct{}{
	"User":       {},
	"Profile":    {},
	"UserRole":   {},
	"Permission": {},
}

// IsSessionFormulaGlobalRoot reports whether name is the root identifier of
// a session-bound formula merge-field global. The helper accepts the bare
// name ("Profile"), the engine-normalized form ("__Profile"), and the
// Salesforce wire form ("$Profile"), in any combination of leading
// whitespace. Comparison is case-sensitive — Salesforce v63 globals are
// PascalCase-only.
func IsSessionFormulaGlobalRoot(name string) bool {
	trimmed := strings.TrimSpace(name)
	trimmed = strings.TrimPrefix(trimmed, "$")
	trimmed = strings.TrimPrefix(trimmed, "__")
	if trimmed == "" {
		return false
	}
	_, ok := sessionFormulaGlobalRoots[trimmed]
	return ok
}
