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

// operator-builtin.go
package expr

//-------- builtin term

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

func evalBuiltin(ctx ExprContext, self *term) (v any, err error) {
	var childValue any
	var it Iterator

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

	count := 0
	if isList(childValue) {
		list, _ := childValue.(*ListType)
		it := NewFlatArrayIterator(*list)
		for moduleSpec, err1 := it.Next(); err1 == nil; moduleSpec, err1 = it.Next() {
			if module, ok := moduleSpec.(string); ok {
				if ImportInContext(ctx, module) {
					count++
				} else {
					err = self.Errorf("unknown module %q", module)
					break
				}
			} else {
				err = self.Errorf("expected string at item nr %d, got %T", it.Index()+1, moduleSpec)
				break
			}
		}
	} else if isString(childValue) {
		module, _ := childValue.(string)
		count, err = ImportInContextByGlobPattern(ctx, module)
	} else {
		err = self.errIncompatibleType(childValue)
	}
	if err == nil {
		v = count
	}
	return
}

// init
func init() {
	registerTermConstructor(SymKwBuiltin, newBuiltinTerm)
}