package collections

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestMergeMaps(t *testing.T) {
	cases := []struct {
		desc   string
		maps   []map[string]string
		expect map[string]string
	}{
		{
			desc:   "empty",
			maps:   []map[string]string{},
			expect: map[string]string{},
		},
		{
			desc: "one empty map",
			maps: []map[string]string{
				{},
			},
			expect: map[string]string{},
		},
		{
			desc: "two empty map",
			maps: []map[string]string{
				{},
				{},
			},
			expect: map[string]string{},
		},
		{
			desc: "two nil map",
			maps: []map[string]string{
				nil,
				nil,
			},
			expect: map[string]string{},
		},
		{
			desc: "one map",
			maps: []map[string]string{
				{"a": "a"},
			},
			expect: map[string]string{
				"a": "a",
			},
		},
		{
			desc: "two map",
			maps: []map[string]string{
				{"a": "a"},
				{"b": "b"},
			},
			expect: map[string]string{
				"a": "a",
				"b": "b",
			},
		},
		{
			desc: "two map with conflict",
			maps: []map[string]string{
				{"a": "a", "b": "b"},
				{"b": "bb"},
			},
			expect: map[string]string{
				"a": "a",
				"b": "bb",
			},
		},
	}

	for _, c := range cases {
		t.Run(c.desc, func(tt *testing.T) {
			assert.Equal(tt, c.expect, MergeMaps(c.maps...))
		})
	}
}

func TestDeepCopyMap(t *testing.T) {
	cases := []struct {
		desc        string
		originalMap map[string]string
		expectedMap map[string]string
	}{
		{
			desc:        "string keys, string values",
			originalMap: map[string]string{"one": "first", "two": "second"},
			expectedMap: map[string]string{"one": "first", "two": "second"},
		},
		{
			desc:        "empty map",
			originalMap: map[string]string{},
			expectedMap: map[string]string{},
		},
	}

	for _, c := range cases {
		t.Run(c.desc, func(tt *testing.T) {
			copiedMap := DeepCopyMap(c.originalMap)
			assert.Equal(tt, copiedMap, c.expectedMap)

			// Modify the original map
			c.originalMap["xxx"] = "yyy"
			assert.Equal(tt, copiedMap, c.expectedMap)
		})
	}
}
