{-# LANGUAGE CPP #-}

module Autodocodec.Aeson.Compat where

#if MIN_VERSION_aeson(2,0,0)
import Data.Aeson.Key (Key)
import qualified Data.Aeson.Key as K
import qualified Data.Aeson.KeyMap as KM
#else
import qualified Data.HashMap.Strict as HM
#endif
import Data.Text (Text)

#if MIN_VERSION_aeson(2,0,0)
toKey :: Text -> Key
toKey :: Text -> Key
toKey = Text -> Key
K.fromText
#else
toKey :: Text -> Text
toKey = id
#endif

#if MIN_VERSION_aeson(2,0,0)
fromKey :: Key -> Text
fromKey :: Key -> Text
fromKey = Key -> Text
K.toText
#else
fromKey :: Text -> Text
fromKey = id
#endif

#if MIN_VERSION_aeson(2,0,0)
lookupKey :: Key -> KM.KeyMap v -> Maybe v
lookupKey :: forall v. Key -> KeyMap v -> Maybe v
lookupKey = forall v. Key -> KeyMap v -> Maybe v
KM.lookup
#else
lookupKey :: Text -> HM.HashMap Text v -> Maybe v
lookupKey = HM.lookup
#endif

#if MIN_VERSION_aeson(2,0,0)
insert :: Key -> v -> KM.KeyMap v -> KM.KeyMap v
insert :: forall v. Key -> v -> KeyMap v -> KeyMap v
insert = forall v. Key -> v -> KeyMap v -> KeyMap v
KM.insert
#else
insert :: Text -> v -> HM.HashMap Text v -> HM.HashMap Text v
insert = HM.insert
#endif

#if MIN_VERSION_aeson(2,0,0)
fromList :: [(Key, v)] -> KM.KeyMap v
fromList :: forall v. [(Key, v)] -> KeyMap v
fromList = forall v. [(Key, v)] -> KeyMap v
KM.fromList
#else
fromList :: [(Text, v)] -> HM.HashMap Text v
fromList = HM.fromList
#endif

#if MIN_VERSION_aeson(2,0,0)
toList ::  KM.KeyMap v -> [(Key, v)]
toList :: forall v. KeyMap v -> [(Key, v)]
toList = forall v. KeyMap v -> [(Key, v)]
KM.toList
#else
toList ::HM.HashMap Text v -> [(Text, v)]
toList = HM.toList
#endif

#if MIN_VERSION_aeson(2,0,0)
map ::  (v1 -> v2) -> KM.KeyMap v1 -> KM.KeyMap v2
map :: forall v1 v2. (v1 -> v2) -> KeyMap v1 -> KeyMap v2
map = forall v1 v2. (v1 -> v2) -> KeyMap v1 -> KeyMap v2
KM.map
#else
map ::  (v1 -> v2) -> HM.HashMap Text v1 -> HM.HashMap Text v2
map = HM.map
#endif