module Unison.Codebase.Editor.HandleInput.MoveAll (handleMoveAll) where

import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Codebase.Editor.HandleInput.MoveBranch (moveBranchFunc)
import Unison.Codebase.Editor.HandleInput.MoveTerm (moveTermSteps)
import Unison.Codebase.Editor.HandleInput.MoveType (moveTypeSteps)
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Codebase.Path qualified as Path
import Unison.HashQualifiedPrime qualified as HQ'
import Unison.Prelude

handleMoveAll :: Bool -> Path.Path' -> Path.Path' -> Text -> Cli ()
handleMoveAll :: Bool -> Path' -> Path' -> Text -> Cli ()
handleMoveAll Bool
hasConfirmed Path'
src' Path'
dest' Text
description = do
  Maybe (Absolute, Branch IO -> Branch IO)
moveBranchFunc <- Bool
-> Path' -> Path' -> Cli (Maybe (Absolute, Branch IO -> Branch IO))
moveBranchFunc Bool
hasConfirmed Path'
src' Path'
dest'
  [(Absolute, Branch0 IO -> Branch0 IO)]
moveTermTypeSteps <- case (,) ((Path', NameSegment)
 -> (Path', NameSegment)
 -> ((Path', NameSegment), (Path', NameSegment)))
-> Maybe (Path', NameSegment)
-> Maybe
     ((Path', NameSegment)
      -> ((Path', NameSegment), (Path', NameSegment)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Path' -> Maybe (Path', NameSegment)
Path.toSplit' Path'
src' Maybe
  ((Path', NameSegment)
   -> ((Path', NameSegment), (Path', NameSegment)))
-> Maybe (Path', NameSegment)
-> Maybe ((Path', NameSegment), (Path', NameSegment))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Path' -> Maybe (Path', NameSegment)
Path.toSplit' Path'
dest' of
    Maybe ((Path', NameSegment), (Path', NameSegment))
Nothing -> [(Absolute, Branch0 IO -> Branch0 IO)]
-> Cli [(Absolute, Branch0 IO -> Branch0 IO)]
forall a. a -> Cli a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
    Just ((NameSegment -> HashQualified NameSegment)
-> (Path', NameSegment) -> (Path', HashQualified NameSegment)
forall a b. (a -> b) -> (Path', a) -> (Path', b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NameSegment -> HashQualified NameSegment
forall n. n -> HashQualified n
HQ'.NameOnly -> (Path', HashQualified NameSegment)
src, (Path', NameSegment)
dest) -> do
      [(Absolute, Branch0 IO -> Branch0 IO)]
termSteps <- (Path', HashQualified NameSegment)
-> (Path', NameSegment)
-> Cli [(Absolute, Branch0 IO -> Branch0 IO)]
forall (m :: * -> *).
(Path', HashQualified NameSegment)
-> (Path', NameSegment) -> Cli [(Absolute, Branch0 m -> Branch0 m)]
moveTermSteps (Path', HashQualified NameSegment)
src (Path', NameSegment)
dest
      [(Absolute, Branch0 IO -> Branch0 IO)]
typeSteps <- (Path', HashQualified NameSegment)
-> (Path', NameSegment)
-> Cli [(Absolute, Branch0 IO -> Branch0 IO)]
forall (m :: * -> *).
(Path', HashQualified NameSegment)
-> (Path', NameSegment) -> Cli [(Absolute, Branch0 m -> Branch0 m)]
moveTypeSteps (Path', HashQualified NameSegment)
src (Path', NameSegment)
dest
      pure ([(Absolute, Branch0 IO -> Branch0 IO)]
termSteps [(Absolute, Branch0 IO -> Branch0 IO)]
-> [(Absolute, Branch0 IO -> Branch0 IO)]
-> [(Absolute, Branch0 IO -> Branch0 IO)]
forall a. [a] -> [a] -> [a]
++ [(Absolute, Branch0 IO -> Branch0 IO)]
typeSteps)
  case (Maybe (Absolute, Branch IO -> Branch IO)
moveBranchFunc, [(Absolute, Branch0 IO -> Branch0 IO)]
moveTermTypeSteps) of
    (Maybe (Absolute, Branch IO -> Branch IO)
Nothing, []) -> Output -> Cli ()
Cli.respond (Path' -> Output
Output.MoveNothingFound Path'
src')
    (Maybe (Absolute, Branch IO -> Branch IO)
mupdates, [(Absolute, Branch0 IO -> Branch0 IO)]
steps) -> do
      ProjectPath
pp <- Cli ProjectPath
Cli.getCurrentProjectPath
      Text
-> ProjectBranch
-> [(Absolute, Branch IO -> Branch IO)]
-> [(Absolute, Branch0 IO -> Branch0 IO)]
-> Cli ()
forall (f :: * -> *) (g :: * -> *).
(Foldable f, Foldable g, Functor g) =>
Text
-> ProjectBranch
-> f (Absolute, Branch IO -> Branch IO)
-> g (Absolute, Branch0 IO -> Branch0 IO)
-> Cli ()
Cli.updateAndStepAt Text
description (ProjectPath
pp ProjectPath
-> Getting ProjectBranch ProjectPath ProjectBranch -> ProjectBranch
forall s a. s -> Getting a s a -> a
^. Getting ProjectBranch ProjectPath ProjectBranch
#branch) (Maybe (Absolute, Branch IO -> Branch IO)
-> [(Absolute, Branch IO -> Branch IO)]
forall a. Maybe a -> [a]
maybeToList Maybe (Absolute, Branch IO -> Branch IO)
mupdates) [(Absolute, Branch0 IO -> Branch0 IO)]
steps
      Output -> Cli ()
Cli.respond Output
Output.Success