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

// t_operator_test.go
package expr

import (
	"testing"

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

func TestOperator(t *testing.T) {
	section := "Operator"
	inputs := []inputType{
		/*   1 */ {`a=1; unset "a"; a`, nil, `undefined variable or function "a"`},
		/*   2 */ {`a=1; unset ["a", "b"]`, int64(1), nil},
		/*   3 */ {`f=func(){3}; unset "f()"`, int64(1), nil},
		/*   4 */ {`a=1; a<<=1+0`, int64(2), nil},
		/*   5 */ {`a=2; a>>=1+0`, int64(1), nil},
		/*   6 */ {`1<<1`, int64(2), nil},
		/*   7 */ {`1>>1`, int64(0), nil},
		/*   8 */ {`1|2`, int64(3), nil},
		/*   9 */ {`a=1; a|=2`, int64(3), nil},
		/*  10 */ {`3&1`, int64(1), nil},
		/*  11 */ {`a=3; a&=1`, int64(1), nil},
		/*  12 */ {`~1`, int64(-2), nil},
		/*  13 */ {`0x10`, int64(16), nil},
		/*  14 */ {`0x1X`, nil, `[1:5] two adjacent operators: "1" and "X"`},
		/*  15 */ {`0o10`, int64(8), nil},
		/*  16 */ {`0b10`, int64(2), nil},
		/*  17 */ {`~true`, nil, `[1:2] prefix/postfix operator "~" does not support operand 'true' [bool]`},
		/*  18 */ {`1^2`, int64(3), nil},
		/*  19 */ {`3^2`, int64(1), nil},
		/*  20 */ {`a=1; a^=2`, int64(3), nil},
		/*  21 */ {`a=1; ++a`, int64(2), nil},
		/*  22 */ {`a=1; --a`, int64(0), nil},
	}

	// t.Setenv("EXPR_PATH", ".")

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

func TestOperatorMap(t *testing.T) {
	section := "Operator-Map"
	inputs := []inputType{
		/*   1 */ {`a=1; --a`, int64(0), nil},
		/*   2 */ {`[1,2,3] map var("_")`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
		/*   3 */ {`[1,2,3] map $_`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
	}

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

func TestOperatorFilter(t *testing.T) {
	section := "Operator-Filter"
	inputs := []inputType{
		/*   1 */ {`[1,2,3,4] filter ($_ % 2 == 0)`, kern.NewList([]any{int64(2), int64(4)}), nil},
	}

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

func TestOperatorDigest(t *testing.T) {
	section := "Operator-Digest"
	inputs := []inputType{
		/*   1 */ {`max=0; [2,3,1] digest max=(($_ > max) ? {$_} :: {max})`, int64(3), nil},
	}

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

func TestOperatorJoin(t *testing.T) {
	section := "Operator-Join"
	inputs := []inputType{
		/*   1 */ {`["a","b"] join ["x"]`, kern.NewList([]any{"a", "b", "x"}), nil},
		/*   2 */ {`["a","b"] join ["x"-true]`, nil, `[1:21] left operand 'x' [string] and right operand 'true' [bool] are not compatible with operator "-"`},
	}

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

func TestOperatorGroupBy(t *testing.T) {
	section := "Operator-GroupBy"
	inputs := []inputType{
		/*   1 */ {`L=[{"num": 1, "alpha": "one"}, {"num": 2, "alpha": "two"}, {"num": 3, "alpha": "three"}]; L groupby "num"`,
			kern.NewDict(map[any]any{
				"1": kern.NewListA(kern.NewDict(map[any]any{"num": int64(1), "alpha": "one"})),
				"2": kern.NewListA(kern.NewDict(map[any]any{"num": int64(2), "alpha": "two"})),
				"3": kern.NewListA(kern.NewDict(map[any]any{"num": int64(3), "alpha": "three"})),
			}),
			nil},
		/*   2 */ {`cars = [{"model": "compas", "vendor": "jeep"}, {"model": "limited", "vendor": "jeep"}, {"model": "600", "vendor":"fiat"}]; cars groupby "vendor"`,
			kern.NewDict(map[any]any{
				"jeep": kern.NewListA(
					kern.NewDict(map[any]any{"model": "compas", "vendor": "jeep"}),
					kern.NewDict(map[any]any{"model": "limited", "vendor": "jeep"})),
				"fiat": kern.NewListA(kern.NewDict(map[any]any{"model": "600", "vendor": "fiat"})),
			}),
			nil},
		/*  3 */ {`[3,4,5] groupby $__`,
			kern.NewDict(map[any]any{
				"0": kern.NewListA(int64(3)),
				"1": kern.NewListA(int64(4)),
				"2": kern.NewListA(int64(5)),
			}),
			nil},
	}

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