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

// operand-dict.go
package expr

import (
	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/scan"
)

// -------- dict term
func newDictTerm(args map[any]*scan.Term) *scan.Term {
	return &scan.Term{
		Tk:       *scan.NewValueToken(0, 0, scan.SymDict, "{}", args),
		Parent:   nil,
		Children: nil,
		Position: scan.PosLeaf,
		Priority: scan.PriValue,
		EvalFunc: evalDict,
	}
}

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