package ru.dbotthepony.kstarbound.defs.util

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap

/**
 * Возвращает глубокую изменяемую копию [input] примитивов/List'ов/Map'ов
 */
fun flattenList(input: List<Any>): ArrayList<Any> {
	val list = ArrayList<Any>(input.size)

	for (v in input) {
		when (v) {
			is Map<*, *> -> list.add(flattenMap(v as Map<String, Any>))
			is List<*> -> list.add(flattenList(v as List<Any>))
			else -> list.add(v)
		}
	}

	return list
}

fun flattenMap(input: Map<String, Any>): Object2ObjectArrayMap<String, Any> {
	val map = Object2ObjectArrayMap<String, Any>()

	for ((k, v) in input) {
		when (v) {
			is Map<*, *> -> map[k] = flattenMap(v as Map<String, Any>)
			is List<*> -> map[k] = flattenList(v as List<Any>)
			else -> map[k] = v
		}
	}

	return map
}
