package connection

import (
	sdk "git.cw.tr/mukan-network/mukan-sdk/types"

	"git.cw.tr/mukan-network/mukan-ibc/modules/core/03-connection/keeper"
	"git.cw.tr/mukan-network/mukan-ibc/modules/core/03-connection/types"
)

// InitGenesis initializes the ibc connection submodule's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k *keeper.Keeper, gs types.GenesisState) {
	for _, connection := range gs.Connections {
		conn := types.NewConnectionEnd(connection.State, connection.ClientId, connection.Counterparty, connection.Versions, connection.DelayPeriod)
		k.SetConnection(ctx, connection.Id, conn)
	}
	for _, connPaths := range gs.ClientConnectionPaths {
		k.SetClientConnectionPaths(ctx, connPaths.ClientId, connPaths.Paths)
	}
	k.SetNextConnectionSequence(ctx, gs.NextConnectionSequence)
	k.SetParams(ctx, gs.Params)

	k.CreateSentinelLocalhostConnection(ctx)
}

// ExportGenesis returns the ibc connection submodule's exported genesis.
func ExportGenesis(ctx sdk.Context, k *keeper.Keeper) types.GenesisState {
	return types.GenesisState{
		Connections:            k.GetAllConnections(ctx),
		ClientConnectionPaths:  k.GetAllClientConnectionPaths(ctx),
		NextConnectionSequence: k.GetNextConnectionSequence(ctx),
		Params:                 k.GetParams(ctx),
	}
}
