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

// operator-default.go
package expr

//-------- default term

func newDefaultTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priDefault,
		evalFunc: evalDefault,
	}
}

func evalDefault(ctx ExprContext, opTerm *term) (v any, err error) {
	var rightValue any

	if err = opTerm.checkOperands(); err != nil {
		return
	}

	leftTerm := opTerm.children[0]
	if leftTerm.tk.Sym != SymVariable {
		// err = leftTerm.Errorf("left operand of %q must be a variable", self.tk.source)
		err = ErrLeftOperandMustBeVariable(leftTerm, opTerm)
		return
	}

	if leftValue, exists := ctx.GetVar(leftTerm.source()); exists {
		v = leftValue
	} else if rightValue, err = opTerm.children[1].compute(ctx); err == nil {
		v = rightValue
	}
	return
}

//-------- alternate term

func newAlternateTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priDefault,
		evalFunc: evalAlternate,
	}
}

func evalAlternate(ctx ExprContext, opTerm *term) (v any, err error) {
	var rightValue any

	if err = opTerm.checkOperands(); err != nil {
		return
	}

	leftTerm := opTerm.children[0]
	if leftTerm.tk.Sym != SymVariable {
		// err = leftTerm.Errorf("left operand of %q must be a variable", self.tk.source)
		err = ErrLeftOperandMustBeVariable(leftTerm, opTerm)
		return
	}

	if leftValue, exists := ctx.GetVar(leftTerm.source()); exists && leftValue != nil {
		if rightValue, err = opTerm.children[1].compute(ctx); err == nil {
			v = rightValue
		}
	} else {
		v = leftValue
	}
	return
}

//-------- default assign term

func newDefaultAssignTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priDefault,
		evalFunc: evalAssignDefault,
	}
}

func evalAssignDefault(ctx ExprContext, opTerm *term) (v any, err error) {
	var rightValue any

	if err = opTerm.checkOperands(); err != nil {
		return
	}

	leftTerm := opTerm.children[0]
	if leftTerm.tk.Sym != SymVariable {
		// err = leftTerm.Errorf("left operand of %q must be a variable", self.tk.source)
		err = ErrLeftOperandMustBeVariable(leftTerm, opTerm)
		return
	}

	if leftValue, exists := ctx.GetVar(leftTerm.source()); exists {
		v = leftValue
	} else if rightValue, err = opTerm.children[1].compute(ctx); err == nil {
		if functor, ok := rightValue.(Functor); ok {
			//ctx.RegisterFunc(leftTerm.source(), functor, 0, -1)
			ctx.RegisterFunc(leftTerm.source(), functor, TypeAny, []ExprFuncParam{
				NewFuncParamFlag(ParamValue, PfDefault|PfRepeat),
			})
		} else {
			v = rightValue
			ctx.UnsafeSetVar(leftTerm.source(), rightValue)
		}
	}
	return
}

// init
func init() {
	registerTermConstructor(SymDoubleQuestion, newDefaultTerm)
	registerTermConstructor(SymQuestionEqual, newDefaultAssignTerm)
	registerTermConstructor(SymQuestionExclam, newAlternateTerm)
}
