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

// operator-range.go
package expr

import "fmt"

// -------- range term
type intPair struct {
	a, b int
}

func (p *intPair) TypeName() string {
	return TypePair
}

func (p *intPair) ToString(opt FmtOpt) string {
	return fmt.Sprintf("(%d, %d)", p.a, p.b)
}

func isIntPair(v any) bool {
	_, ok := v.(*intPair)
	return ok
}

func newRangeTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priRange,
		evalFunc: evalRange,
	}
}

func evalRange(ctx ExprContext, self *term) (v any, err error) {
	var leftValue, rightValue any

	// if err = self.checkOperands(); err != nil {
	// 	return
	// }
	if len(self.children) == 0 {
		leftValue = int64(0)
		rightValue = int64(-1)
	} else if len(self.children) == 1 {
		if leftValue, err = self.children[0].compute(ctx); err != nil {
			return
		}
		rightValue = int64(ConstLastIndex)
	} else if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
		return
	}
	if !(IsInteger(leftValue) && IsInteger(rightValue)) {
		err = self.errIncompatibleTypes(leftValue, rightValue)
		return
	}

	startIndex, _ := leftValue.(int64)
	endIndex, _ := rightValue.(int64)

	v = &intPair{int(startIndex), int(endIndex)}
	return
}

// init
func init() {
	registerTermConstructor(SymColon, newRangeTerm)
}
