package ru.dbotthepony.mc.otm.data

import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import kotlin.reflect.KProperty1


fun <V, T1> simpleCodec(factory: (T1) -> V, field1: KProperty1<V, T1>, codec1: Codec<T1>): Codec<V> {
	return RecordCodecBuilder.create {
		it.group(
			codec1.fieldOf(field1.name).forGetter(field1::get)
		).apply(it, factory)
	}
}

fun <V, T1, T2> simpleCodec(factory: (T1, T2) -> V, field1: KProperty1<V, T1>, codec1: Codec<T1>, field2: KProperty1<V, T2>, codec2: Codec<T2>): Codec<V> {
	return RecordCodecBuilder.create {
		it.group(
			codec1.fieldOf(field1.name).forGetter(field1::get),
			codec2.fieldOf(field2.name).forGetter(field2::get),
		).apply(it, factory)
	}
}

fun <V, T1, T2, T3> simpleCodec(factory: (T1, T2, T3) -> V, field1: KProperty1<V, T1>, codec1: Codec<T1>, field2: KProperty1<V, T2>, codec2: Codec<T2>, field3: KProperty1<V, T3>, codec3: Codec<T3>): Codec<V> {
	return RecordCodecBuilder.create {
		it.group(
			codec1.fieldOf(field1.name).forGetter(field1::get),
			codec2.fieldOf(field2.name).forGetter(field2::get),
			codec3.fieldOf(field3.name).forGetter(field3::get),
		).apply(it, factory)
	}
}
