// 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/list"
)

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

func newLinkedListTerm(row, col int, args []*scan.Term) *scan.Term {
	return &scan.Term{
		Tk:       *scan.NewValueToken(row, col, sym.SymLinkedList, "[<>]", args),
		Parent:   nil,
		Children: nil,
		Position: scan.PosLeaf,
		Priority: scan.PriValue,
		EvalFunc: evalLinkedList,
	}
}

// -------- list func
func evalLinkedList(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	listTerm, _ := opTerm.Value().([]*scan.Term)
	items := list.NewLinkedList()
	for _, tree := range listTerm {
		var param any
		if param, err = tree.Compute(ctx); err != nil {
			break
		}
		items.PushBack(param)
	}
	if err == nil {
		v = items
	}
	return
}
