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

// operator-include.go
package expr

//-------- include term

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

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

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

	count := 0
	if IsList(childValue) {
		list, _ := childValue.([]any)
		for i, filePathSpec := range list {
			if filePath, ok := filePathSpec.(string); ok {
				if v, err = EvalFile(ctx, filePath); err == nil {
					count++
				} else {
					err = self.Errorf("can't load file %q", filePath)
					break
				}
			} else {
				err = self.Errorf("expected string at item nr %d, got %T", i+1, filePathSpec)
				break
			}
		}
	} else if IsString(childValue) {
		filePath, _ := childValue.(string)
		if v, err = EvalFile(ctx, filePath); err == nil {
			count++
		}
	} else {
		err = self.errIncompatibleType(childValue)
	}
	if err == nil {
		v = count
	}
	return
}

// init
func init() {
	registerTermConstructor(SymKwInclude, newIncludeTerm)
}
