module Rob.Actions.Remove (main) where

import Rob.Logger (err)
import Rob.Config (get, errorNoTemplatesAvailable, deleteTemplate)
import Rob.Types (Config(..))
import Rob.Project (getTemplateName)
import Rob.UserMessages (
    choseATemplateToDelete,
    noTemplateSelected,
    emptyString
  )

import System.Exit (exitFailure, exitSuccess)
import FortyTwo (select)

main :: IO ()
main :: IO ()
main = do
  Config
config <- IO Config
get
  Config -> IO ()
nukeTemplate Config
config

-- Remove a project template from the available ones
nukeTemplate :: Config -> IO ()
nukeTemplate :: Config -> IO ()
nukeTemplate (Config []) = IO ()
errorNoTemplatesAvailable
nukeTemplate (Config [Template]
templates) = do
  String
templateName <- String -> [String] -> IO String
select String
choseATemplateToDelete ([String] -> IO String) -> [String] -> IO String
forall a b. (a -> b) -> a -> b
$ (Template -> String) -> [Template] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Template -> String
getTemplateName [Template]
templates
  String -> IO ()
putStrLn String
emptyString
  if (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) String
templateName then do
    Config
_ <- Config -> String -> IO Config
deleteTemplate ([Template] -> Config
Config [Template]
templates) String
templateName
    IO ()
forall a. IO a
exitSuccess
  else do
    String -> IO ()
err String
noTemplateSelected
    IO ()
forall a. IO a
exitFailure