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

// t_iterator_test.go
package expr

import (
	"testing"

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

func TestIteratorParser(t *testing.T) {
	section := "Iterator"
	inputs := []inputType{
		/*   1 */ {`include "test-resources/iterator.expr"; it=$(ds,3); *it`, int64(0), nil},
		/*   2 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++`, int64(1), nil},
		/*   3 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; #it`, int64(3), nil},
		/*   4 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; it.reset; *it`, int64(0), nil},
		/*   5 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); add(it)`, int64(6), nil},
		/*   6 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); mul(it)`, int64(0), nil},
		/*   7 */ {`builtin "math.arith"; include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); mul(it)`, int64(12000), nil},
		/*   8 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it++; it.index`, int64(0), nil},
		/*   9 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it.clean`, nil, nil},
		/*  10 */ {`it=$(1,2,3); it++`, int64(1), nil},
		/*  11 */ {`it=$(1,2,3); it++; it.reset; it++`, int64(1), nil},
		/*  12 */ {`it=$([1,2,3,4],1); it++`, int64(2), nil},
		/*  13 */ {`it=$([1,2,3,4],1,3); it++; it++;`, int64(3), nil},
		/*  14 */ {`it=$([1,2,3,4],1,3,2); it++; it++;`, int64(4), nil},
		/*  15 */ {`it=$([1,2,3,4],1,2,2); it++; it++;`, nil, `EOF`},
		/*  16 */ {`include "test-resources/filter.expr"; it=$(ds,10); it++`, int64(2), nil},
		/*  17 */ {`it=$({"next":func(){5}}); it++`, int64(5), nil},
		/*  18 */ {`it=$({"next":func(){5}}); it.clean`, nil, nil},
		/*  19 */ {`it=$({1:"one",2:"two",3:"three"}); it++`, int64(1), nil},
		/*  20 */ {`it=$({1:"one",2:"two",3:"three"}, "default", "value"); it++`, "one", nil},
		/*  21 */ {`it=$({1:"one",2:"two",3:"three"}, "desc", "key"); it++`, int64(3), nil},
		/*  22 */ {`it=$({1:"one",2:"two",3:"three"}, "asc", "item"); it++`, kern.NewList([]any{int64(1), "one"}), nil},
		/*  23 */ {`builtin "os.file"; fileReadIterator("test-file.txt") map ${__}`, kern.NewList([]any{int64(0), int64(1)}), nil},
		/*  24 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 2))`, int64(0), nil},
		/*  25 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 3))`, int64(2), nil},
		/*  26 */ {`#($(10) map ${_})`, int64(10), nil},
		/*  27 */ {`#($(10,0) map ${_})`, int64(10), nil},
		/*  28 */ {`$(10) digest ${_}`, int64(9), nil},
		/*  29 */ {`$(10,0) digest ${_}`, int64(1), nil},
		/*  30 */ {`$(10,0,-2) digest ${_}`, int64(2), nil},
		/*  31 */ {`[3,4,5] map ${_#}`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
	}

	// runTestSuiteSpec(t, section, inputs, 10)
	runTestSuite(t, section, inputs)
}
