{-|
Module      : Hoyo.CLI.Complete
Copyright   : (c) Frederick Pringle, 2023
License     : BSD-3-Clause
Maintainer  : freddyjepringle@gmail.com

Bash competion functions used by the CLI.
-}

module Hoyo.CLI.Complete (
  -- * Completing command-line arguments
    bookmarkCompleter
  , configKeyCompleter
  , configValueCompleter
  ) where

import           Control.Monad.IO.Class

import           Data.Maybe
import qualified Data.Text              as T

import           Hoyo

import           Options.Applicative

-- | Fetch existing data file and use it to complete bookmark names.
bookmarkCompleter :: Completer
bookmarkCompleter :: Completer
bookmarkCompleter = IO [String] -> Completer
listIOCompleter (IO [String] -> Completer) -> IO [String] -> Completer
forall a b. (a -> b) -> a -> b
$ do
  String
sFp <- IO String
defaultConfigPath
  String
bFp <- IO String
defaultBookmarksPath
  Either HoyoException Bookmarks
res <- GlobalOptions
-> String
-> String
-> HoyoMonad Bookmarks
-> IO (Either HoyoException Bookmarks)
forall a.
GlobalOptions
-> String -> String -> HoyoMonad a -> IO (Either HoyoException a)
withFiles GlobalOptions
defaultGlobalOptions String
bFp String
sFp HoyoMonad Bookmarks
getBookmarks
  case Either HoyoException Bookmarks
res of
    Left HoyoException
err              -> do IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ HoyoException -> IO ()
forall a. Show a => a -> IO ()
print HoyoException
err
                                [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
    Right (Bookmarks [Bookmark]
bms) -> do
      let indices :: [String]
indices = (Bookmark -> String) -> [Bookmark] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> String
forall a. Show a => a -> String
show (Int -> String) -> (Bookmark -> Int) -> Bookmark -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bookmark -> Int
_bookmarkIndex) [Bookmark]
bms
      let nicknames :: [String]
nicknames = (Bookmark -> Maybe String) -> [Bookmark] -> [String]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ((Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> String
T.unpack (Maybe Text -> Maybe String)
-> (Bookmark -> Maybe Text) -> Bookmark -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bookmark -> Maybe Text
_bookmarkName) [Bookmark]
bms
      [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String]
nicknames [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> [String]
indices)

-- | Complete configuration keys.
configKeyCompleter :: Completer
configKeyCompleter :: Completer
configKeyCompleter = [String] -> Completer
listCompleter [
  String
"fail_on_error"
  , String
"display_creation_time"
  , String
"enable_clearing"
  , String
"enable_reset"
  , String
"backup_before_clear"
  , String
"default_command"
  ]

-- | Complete configuration values.
--
-- TODO: could be more sophisticated, considering the current key
configValueCompleter :: Completer
configValueCompleter :: Completer
configValueCompleter = [String] -> Completer
listCompleter [
  String
"true"
  , String
"True"
  , String
"false"
  , String
"False"
  ]