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

// operand-func.go
package expr

import (
	"errors"
)

// -------- function call term
func newFuncCallTerm(tk *Token, args []*term) *term {
	return &term{
		tk:       *tk,
		parent:   nil,
		children: args,
		position: posLeaf,
		priority: priValue,
		evalFunc: evalFuncCall,
	}
}

// -------- eval func call
func evalFuncCall(ctx ExprContext, self *term) (v any, err error) {
	name, _ := self.tk.Value.(string)
	params := make([]any, len(self.children), len(self.children)+5)
	for i, tree := range self.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
}

// -------- function definition term
func newFuncDefTerm(tk *Token, args []*term) *term {
	return &term{
		tk:       *tk, // value is the expression body
		parent:   nil,
		children: args, // function params
		position: posLeaf,
		priority: priValue,
		evalFunc: evalFuncDef,
	}
}

// -------- eval func def
func evalFuncDef(ctx ExprContext, self *term) (v any, err error) {
	bodySpec := self.value()
	if expr, ok := bodySpec.(*ast); ok {
		paramList := make([]ExprFuncParam, 0, len(self.children))
		for _, param := range self.children {
			var defValue any
			flags := paramFlags(0)
			if len(param.children) > 0 {
				flags |= PfDefault
				if defValue, err = param.children[0].compute(ctx); err != nil {
					return
				}
			}
			info := NewFuncParamFlagDef(param.source(), flags, defValue)
			paramList = append(paramList, info)
		}
		v = newExprFunctor(expr, paramList, ctx)
	} else {
		err = errors.New("invalid function definition: the body specification must be an expression")
	}
	return
}