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

// operator-unset.go
package expr

import "strings"

//-------- unset term

func newUnsetTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 1),
		position: posPrefix,
		priority: priSign,
		evalFunc: evalUnset,
	}
}

func deleteContextItem(ctx ExprContext, opTerm *term, item any) (deleted bool, err error) {
	if name, ok := item.(string); ok {
		var size int
		if strings.HasSuffix(name, "()") {
			size = ctx.FuncCount()
			ctx.DeleteFunc(strings.TrimRight(name, "()"))
			deleted = ctx.FuncCount() < size
		} else {
			size = ctx.VarCount()
			ctx.DeleteVar(name)
			deleted = ctx.VarCount() < size
		}
	} else {
		err = opTerm.errIncompatibleType(item)
	}
	return
}

func evalUnset(ctx ExprContext, opTerm *term) (v any, err error) {
	var childValue any
	var deleted bool

	if childValue, err = opTerm.evalPrefix(ctx); err != nil {
		return
	}

	count := 0
	if IsList(childValue) {
		list, _ := childValue.(*ListType)
		for _, item := range *list {
			if deleted, err = deleteContextItem(ctx, opTerm, item); err != nil {
				break
			} else if deleted {
				count++
			}
		}
	} else if deleted, err = deleteContextItem(ctx, opTerm, childValue); err == nil && deleted {
		count++
	}
	if err == nil {
		v = int64(count)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymKwUnset, newUnsetTerm)
}
