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

// operand-list.go
package expr

import (
	"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"
)

// -------- list term
func newListTermA(args ...*scan.Term) *scan.Term {
	return newListTerm(0, 0, args)
}

func newListTerm(row, col int, args []*scan.Term) *scan.Term {
	return &scan.Term{
		Tk:       *scan.NewValueToken(row, col, sym.SymList, "[]", args),
		Parent:   nil,
		Children: nil,
		Position: scan.PosLeaf,
		Priority: scan.PriValue,
		EvalFunc: evalList,
	}
}

// -------- list func
func evalList(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	list, _ := opTerm.Value().([]*scan.Term)
	items := make(array.ArrayType, len(list))
	for i, tree := range list {
		var param any
		if param, err = tree.Compute(ctx); err != nil {
			break
		}
		items[i] = param
	}
	if err == nil {
		v = &items
	}
	return
}
