package simulation

import (
	"fmt"

	"git.cw.tr/mukan-network/mukan-sdk/types/kv"

	clientsim "git.cw.tr/mukan-network/mukan-ibc/modules/core/02-client/simulation"
	connectionsim "git.cw.tr/mukan-network/mukan-ibc/modules/core/03-connection/simulation"
	channelsim "git.cw.tr/mukan-network/mukan-ibc/modules/core/04-channel/simulation"
	ibcexported "git.cw.tr/mukan-network/mukan-ibc/modules/core/exported"
	"git.cw.tr/mukan-network/mukan-ibc/modules/core/keeper"
)

// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding ibc type.
func NewDecodeStore(k keeper.Keeper) func(kvA, kvB kv.Pair) string {
	return func(kvA, kvB kv.Pair) string {
		if res, found := clientsim.NewDecodeStore(k.Codec(), kvA, kvB); found {
			return res
		}

		if res, found := connectionsim.NewDecodeStore(k.Codec(), kvA, kvB); found {
			return res
		}

		if res, found := channelsim.NewDecodeStore(k.Codec(), kvA, kvB); found {
			return res
		}

		panic(fmt.Errorf("invalid %s key prefix: %s", ibcexported.ModuleName, string(kvA.Key)))
	}
}
