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

// operator-dot.go
package expr

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

// -------- dot term
func newDotTerm(tk *scan.Token) (inst *scan.Term) {
	return &scan.Term{
		Tk:       *tk,
		Children: make([]*scan.Term, 0, 2),
		Position: scan.PosInfix,
		Priority: scan.PriDot,
		EvalFunc: evalDot,
	}
}

func evalDot(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	var leftValue, rightValue any

	if err = opTerm.CheckOperands(); err != nil {
		return
	}
	if leftValue, err = opTerm.Children[0].Compute(ctx); err != nil {
		return
	}

	indexTerm := opTerm.Children[1]

	switch unboxedValue := leftValue.(type) {
	case kern.ExtIterator:
		if indexTerm.Tk.Sym == scan.SymVariable /*|| indexTerm.Tk.Sym == scan.SymString */ {
			opName := indexTerm.Source()
			if unboxedValue.HasOperation(opName) {
				v, err = unboxedValue.CallOperation(opName, map[string]any{})
			} else {
				err = indexTerm.Errorf("this iterator do not support the %q command", opName)
				v = false
			}
		} else {
			err = indexTerm.Tk.ErrorExpectedGot("identifier")
		}
	case *kern.DictType:
		var ok bool
		s := opTerm.Children[1].Tk.Sym
		if s == scan.SymVariable || s == scan.SymString {
			src := opTerm.Children[1].Source()
			if len(src) > 1 && src[0] == '"' && src[len(src)-1] == '"' {
				src = src[1 : len(src)-1]
			}
			if v, ok = unboxedValue.GetItem(src); !ok {
				err = opTerm.Errorf("key %q not found", src)
			}
		} else if rightValue, err = opTerm.Children[1].Compute(ctx); err == nil {
			if v, ok = unboxedValue.GetItem(rightValue); !ok {
				err = opTerm.Errorf("key %q not found", rightValue)
			}
		}
	default:
		if rightValue, err = opTerm.Children[1].Compute(ctx); err == nil {
			err = opTerm.Errorf("incompatible types: %s and %s", kern.TypeName(leftValue), kern.TypeName(rightValue))
		}
	}
	return
}

// init
func init() {
	scan.RegisterTermConstructor(scan.SymDot, newDotTerm)
}
