module Matterhorn.KeybindingConsistency
  ( ensureKeybindingConsistency
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick.Keybindings

import qualified Data.Text as T

import           Matterhorn.Types

-- | Given a configuration, we want to check it for internal consistency
-- (i.e. that a given keybinding isn't associated with multiple events
-- which both need to get generated in the same UI mode).
ensureKeybindingConsistency :: KeyConfig KeyEvent
                            -> [(T.Text, [MHKeyEventHandler])]
                            -> Either String ()
ensureKeybindingConsistency :: KeyConfig KeyEvent
-> [(Text, [MHKeyEventHandler])] -> Either String ()
ensureKeybindingConsistency KeyConfig KeyEvent
kc [(Text, [MHKeyEventHandler])]
modeMaps = do
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Text, [MHKeyEventHandler])]
modeMaps forall a b. (a -> b) -> a -> b
$ \(Text
mode, [MHKeyEventHandler]
handlers) -> do
        case forall k (m :: * -> *).
Ord k =>
KeyConfig k
-> [KeyEventHandler k m]
-> Either [(Binding, [KeyHandler k m])] (KeyDispatcher k m)
keyDispatcher KeyConfig KeyEvent
kc [MHKeyEventHandler]
handlers of
            Left [(Binding, [KeyHandler KeyEvent MH])]
conflicts ->
                forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack forall a b. (a -> b) -> a -> b
$
                    Text
"Key binding conflict in '" forall a. Semigroup a => a -> a -> a
<> Text
mode forall a. Semigroup a => a -> a -> a
<> Text
"' mode:\n" forall a. Semigroup a => a -> a -> a
<>
                    forall k (m :: * -> *).
Ord k =>
KeyConfig k -> [(Binding, [KeyHandler k m])] -> Text
bindingConflictMessage KeyConfig KeyEvent
kc [(Binding, [KeyHandler KeyEvent MH])]
conflicts
            Right {} -> forall (m :: * -> *) a. Monad m => a -> m a
return ()