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

// operator-cat.go
package expr

import (
	"fmt"
	"io"

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

//-------- cat term

func newCatTerm(tk *scan.Token) (inst *scan.Term) {
	return &scan.Term{
		Tk:       *tk,
		Children: make([]*scan.Term, 0, 2),
		Position: scan.PosInfix,
		Priority: scan.PriIterOp,
		EvalFunc: evalCat,
	}
}

func evalCat(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	var leftValue, rightValue any
	var itLeft, itRight kern.Iterator
	// var item any
	var ok bool

	if err = opTerm.CheckOperands(); err != nil {
		return
	}

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

	if rightValue, err = opTerm.Children[1].Compute(ctx); err != nil {
		return
	}

	if itLeft, ok = leftValue.(kern.Iterator); !ok {
		if itLeft, err = NewIterator(ctx, leftValue, nil); err != nil {
			return nil, fmt.Errorf("left operand of JOIN must be an iterable data-source; got %s", kern.TypeName(leftValue))
		}
	}

	if itRight, ok = rightValue.(kern.Iterator); !ok {
		if itRight, err = NewIterator(ctx, rightValue, nil); err != nil {
			return nil, fmt.Errorf("right operand of JOIN must be an iterable data-source; got %s", kern.TypeName(rightValue))
		}
	}

	// values := kern.NewListA()
	// for _, it := range []kern.Iterator{itLeft, itRight} {
	// 	for item, err = it.Next(); err == nil; item, err = it.Next() {
	// 		values.AppendItem(item)
	// 	}
	// }
	// if err == io.EOF {
	// 	err = nil
	// }
	// v = values
	v = newCatIterator(itLeft, itRight)
	return
}

const catIteratorType = "cat"

type catIterator struct {
	kern.IteratorBase
	itLeft    kern.Iterator
	itRight   kern.Iterator
	itCurrent kern.Iterator
}

func newCatIterator(itLeft, itRight kern.Iterator) (it *catIterator) {
	it = &catIterator{
		IteratorBase: kern.IteratorBase{},
		itLeft:       itLeft,
		itRight:      itRight,
		itCurrent:    itLeft,
	}
	it.Reset()
	return
}

func (it *catIterator) TypeName() string {
	return catIteratorType
}

func (it *catIterator) String() string {
	return fmt.Sprintf("$(%s %s %s)", it.itLeft, catIteratorType, it.itRight)
}

func (it *catIterator) Next() (item any, err error) {
	if it.itCurrent == nil {
		err = io.EOF
	} else {
		if item, err = it.itCurrent.Next(); err == nil {
			it.Increment()
		} else if err == io.EOF {
			if it.itCurrent == it.itLeft {
				it.itCurrent = it.itRight
			} else {
				return
			}
			if item, err = it.itCurrent.Next(); err == nil {
				it.Increment()
			} else {
				it.itCurrent = nil
			}
		}
	}
	it.SetCurrent(item)
	return
}

// func (it *catIterator) Reset() (err error) {
// 	err = it.itLeft.Reset()
// 	it.IteratorBase.Reset()
// 	return
// }

func (it *catIterator) CallOperation(name string, args map[string]any) (v any, err error) {
	switch name {
	case kern.NextName:
		v, err = it.Next()
	// case kern.ResetName:
	// 	err = it.Reset()
	// case kern.CleanName:
	// 	err = it.Clean()
	case kern.IndexName:
		v = it.Index()
	case kern.CurrentName:
		v, err = it.Current()
	case kern.CountName:
		v = it.Count()
	default:
		err = kern.ErrNoOperation(name)
	}
	return
}

// init
func init() {
	scan.RegisterTermConstructor(sym.SymKwCat, newCatTerm)
}
