package decoders_test

import (
	"reflect"
	"testing"

	"git.max-richter.dev/max/marka/parser/decoders"
	"git.max-richter.dev/max/marka/template"
)

func TestDecodeListObject(t *testing.T) {
	templateBlock := template.Block{
		Path:         "ingredients",
		Codec:        template.CodecList,
		ListTemplate: "- { amount } { type }",
	}
	input := "- 10g flour\n- 1/2cup water\n- 1tsp salt"

	parsed, err := decoders.List(input, templateBlock)
	if err != nil {
		t.Fatalf("Err: %s", err)
	}

	want := []any{
		map[string]any{
			"amount": "10g",
			"type":   "flour",
		},
		map[string]any{
			"amount": "1/2cup",
			"type":   "water",
		},
		map[string]any{
			"amount": "1tsp",
			"type":   "salt",
		},
	}

	if !reflect.DeepEqual(parsed, want) {
		t.Fatalf("unexpected result.\n got: %#v\nwant: %#v", parsed, want)
	}
}

func TestDecodeListString(t *testing.T) {
	templateBlock := template.Block{
		Path:         "ingredients",
		Codec:        template.CodecList,
		ListTemplate: "- { . }",
	}
	input := "- flour\n- water\n- salt"

	parsed, err := decoders.List(input, templateBlock)
	if err != nil {
		t.Fatalf("Err: %s", err)
	}

	want := []any{
		"flour",
		"water",
		"salt",
	}

	if !reflect.DeepEqual(parsed, want) {
		t.Fatalf("unexpected result.\n got: %#v\nwant: %#v", parsed, want)
	}
}

func TestDecodeNumberedListString(t *testing.T) {
	templateBlock := template.Block{
		Path:         "ingredients",
		Codec:        template.CodecList,
		ListTemplate: "{ @index } { . }",
	}
	input := "1. Wash and dry the lettuce.\n2. Halve the cherry tomatoes.\n3. Toss with olive oil and salt."

	parsed, err := decoders.List(input, templateBlock)
	if err != nil {
		t.Fatalf("Err: %s", err)
	}

	want := []any{
		"Wash and dry the lettuce.",
		"Halve the cherry tomatoes.",
		"Toss with olive oil and salt.",
	}

	if !reflect.DeepEqual(parsed, want) {
		t.Fatalf("unexpected result.\n got: %#v\nwant: %#v", parsed, want)
	}
}
