package GoUtil

import (
	dailyTaskCfg "server/conf/daily_task"
	"server/game/mod/item"
)

func MapIntToSlice(m map[int]struct{}) []int32 {
	var s []int32
	for k := range m {
		s = append(s, int32(k))
	}
	return s
}
func MapIntToIntSlice(m map[int]struct{}) []int {
	var s []int
	for k := range m {
		s = append(s, k)
	}
	return s
}

func MapIntToInt32(m map[int]int) map[int32]int32 {
	r := make(map[int32]int32)
	for k, v := range m {
		r[int32(k)] = int32(v)
	}
	return r
}

func MapInt32ToInt(m map[int32]int32) map[int]int {
	r := make(map[int]int)
	for k, v := range m {
		r[int(k)] = int(v)
	}
	return r
}

func GetMapKey(v any) []int {
	if v == nil {
		return []int{}
	}
	switch m := v.(type) {
	case map[int]int:
		var keys []int
		for k := range m {
			keys = append(keys, k)
		}
		return keys
	case map[int32]int32:
		var keys []int
		for k := range m {
			keys = append(keys, int(k))
		}
		return keys
	case map[int]struct{}:
		var keys []int
		for k := range m {
			keys = append(keys, int(k))
		}
		return keys
	case map[int]string:
		var keys []int
		for k := range m {
			keys = append(keys, int(k))
		}
		return keys
	case map[int][]*item.Item:
		var keys []int
		for k := range m {
			keys = append(keys, int(k))
		}
		return keys
	case map[int]dailyTaskCfg.Jackpot:
		var keys []int
		for k := range m {
			keys = append(keys, int(k))
		}
		return keys
	}
	return []int{}
}

func DiffMap(a, b map[int]int) map[int]int {
	diff := make(map[int]int)
	for k, v := range a {
		if v2, ok := b[k]; !ok || v != v2 {
			diff[k] = v
		}
	}
	for k, v := range b {
		if _, ok := a[k]; !ok {
			diff[k] = v
		}
	}
	return diff
}

func GetMapIntValueByKey(m interface{}, key string) int {
	mi, ok := m.(map[string]interface{})
	if !ok {
		return 0
	}
	if v, ok := mi[key]; ok {
		return Int(v)
	}
	return 0
}
