{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Ext.KeyOf
  ( KeyOf (..),
    toPair,
  )
where

import Data.Morpheus.Ext.Map
  ( Indexed (..),
  )
import Data.Morpheus.Types.Internal.AST.Base
  ( FieldName (..),
    Ref (..),
    TypeName (..),
    TypeNameRef (..),
  )
import Relude

class (Eq k, Hashable k) => KeyOf k a | a -> k where
  keyOf :: a -> k

instance (Eq k, Hashable k) => KeyOf k (k, a) where
  keyOf :: (k, a) -> k
keyOf = (k, a) -> k
forall a b. (a, b) -> a
fst

instance KeyOf FieldName Ref where
  keyOf :: Ref -> FieldName
keyOf = Ref -> FieldName
refName

instance KeyOf TypeName TypeNameRef where
  keyOf :: TypeNameRef -> TypeName
keyOf = TypeNameRef -> TypeName
typeNameRef

instance (Eq k, Hashable k) => KeyOf k (Indexed k a) where
  keyOf :: Indexed k a -> k
keyOf = Indexed k a -> k
forall k a. Indexed k a -> k
indexedKey

toPair :: KeyOf k a => a -> (k, a)
toPair :: a -> (k, a)
toPair a
x = (a -> k
forall k a. KeyOf k a => a -> k
keyOf a
x, a
x)