// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// operand-func.go
package expr

import (
	"errors"

	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/scan"
)

// -------- function call term
func newFuncCallTerm(tk *scan.Token, args []*scan.Term) *scan.Term {
	var pos scan.TermPosition = scan.PosLeaf
	if len(args) > 0 {
		pos = scan.PosMultifix
	}
	return &scan.Term{
		Tk:       *tk,
		Parent:   nil,
		Children: args,
		Position: pos,
		Priority: scan.PriValue,
		EvalFunc: evalFuncCall,
	}
}

// -------- eval func call
// func _evalFuncCall(ctx ExprContext, opTerm *term) (v any, err error) {
// 	name, _ := opTerm.Tk.Value.(string)
// 	params := make([]any, len(opTerm.Children), len(opTerm.Children)+5)
// 	for i, tree := range opTerm.Children {
// 		var param any
// 		if param, err = tree.Compute(ctx); err != nil {
// 			break
// 		}
// 		params[i] = param
// 	}

// 	if err == nil {
// 		v, err = CallFunction(ctx, name, params)
// 	}
// 	return
// }

func evalFuncCall(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	name, _ := opTerm.Tk.Value.(string)
	v, err = kern.CallFunctionByTerm(ctx, name, opTerm)
	return
}

// -------- function definition term
func newFuncDefTerm(tk *scan.Token, args []*scan.Term) *scan.Term {
	return &scan.Term{
		Tk:       *tk, // value is the expression body
		Parent:   nil,
		Children: args, // function params
		Position: scan.PosLeaf,
		Priority: scan.PriValue,
		EvalFunc: evalFuncDef,
	}
}

// -------- eval func def
func evalFuncDef(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	bodySpec := opTerm.Value()
	if ast, ok := bodySpec.(*scan.Ast); ok {
		paramList := make([]kern.ExprFuncParam, 0, len(opTerm.Children))
		for _, param := range opTerm.Children {
			var defValue any
			flags := kern.FuncParamFlags(0)
			if len(param.Children) > 0 {
				flags |= kern.PfDefault
				if defValue, err = param.Children[0].Compute(ctx); err != nil {
					return
				}
			}
			info := kern.NewFuncParamFlagDef(param.Source(), flags, defValue)
			paramList = append(paramList, info)
		}
		v = newExprFunctor(ast, paramList, ctx)
	} else {
		err = errors.New("invalid function definition: the body specification must be an expression")
	}
	return
}
