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

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

import Data.Mergeable
  ( Indexed (..),
  )
import Data.Morpheus.Types.Internal.AST.Base
  ( Ref (..),
  )
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 = forall a b. (a, b) -> a
fst

instance (Eq name, Hashable name) => KeyOf name (Ref name) where
  keyOf :: Ref name -> name
keyOf = forall name. Ref name -> name
refName

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

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