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

// operator-unset.go
package expr

import (
	"strings"

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

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

func newUnsetTerm(tk *scan.Token) (inst *scan.Term) {
	return &scan.Term{
		Tk:       *tk,
		Children: make([]*scan.Term, 0, 1),
		Position: scan.PosPrefix,
		Priority: scan.PriSign,
		EvalFunc: evalUnset,
	}
}

func deleteContextItem(ctx kern.ExprContext, opTerm *scan.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.ErrIncompatiblePrefixPostfixType(item)
	}
	return
}

func evalUnset(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	var childValue any
	var deleted bool

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

	count := 0
	if array.IsArray(childValue) {
		list, _ := childValue.(*array.ArrayType)
		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() {
	scan.RegisterTermConstructor(sym.SymKwUnset, newUnsetTerm)
}
