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

// operand-selector-case.go
package expr

import (
	"fmt"
	"strings"
)

// -------- selector case term

type selectorCase struct {
	filterList *term
	caseExpr   Expr
}

func (sc *selectorCase) String() string {
	var sb strings.Builder
	if sc.filterList != nil {
		sc.filterList.toString(&sb)
		sb.WriteByte(' ')
	}
	sb.WriteByte('{')
	sb.WriteString(sc.caseExpr.String())
	sb.WriteByte('}')
	return sb.String()
}

func newSelectorCaseTerm(row, col int, filterList *term, caseExpr Expr) *term {
	tk := NewValueToken(row, col, SymSelectorCase, "", &selectorCase{filterList: filterList, caseExpr: caseExpr})
	return &term{
		tk:       *tk,
		parent:   nil,
		children: nil,
		position: posLeaf,
		priority: priValue,
		evalFunc: evalSelectorCase,
	}
}

// -------- eval selector case
func evalSelectorCase(ctx ExprContext, opTerm *term) (v any, err error) {
	var ok bool
	if v, ok = opTerm.value().(*selectorCase); !ok {
		err = fmt.Errorf("selector-case expected, got %T", opTerm.value())
	}
	return
}
