module Cursor.Tree.Collapse
  ( treeCursorOpenCurrentForest,
    treeCursorCloseCurrentForest,
    treeCursorToggleCurrentForest,
    treeCursorOpenCurrentForestRecursively,
    treeCursorToggleCurrentForestRecursively,
  )
where

import Cursor.Tree.Types
import qualified Data.List.NonEmpty as NE

treeCursorOpenCurrentForest :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorOpenCurrentForest :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorOpenCurrentForest TreeCursor a b
tc =
  case TreeCursor a b -> CForest b
forall a b. TreeCursor a b -> CForest b
treeBelow TreeCursor a b
tc of
    CForest b
EmptyCForest -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing
    ClosedForest NonEmpty (Tree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (CTree b) -> CForest b
forall a. NonEmpty (CTree a) -> CForest a
OpenForest (NonEmpty (CTree b) -> CForest b)
-> NonEmpty (CTree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (Tree b -> CTree b) -> NonEmpty (Tree b) -> NonEmpty (CTree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map Tree b -> CTree b
forall a. Tree a -> CTree a
makeCTree NonEmpty (Tree b)
ts}
    OpenForest NonEmpty (CTree b)
_ -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing

treeCursorCloseCurrentForest :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorCloseCurrentForest :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorCloseCurrentForest TreeCursor a b
tc =
  case TreeCursor a b -> CForest b
forall a b. TreeCursor a b -> CForest b
treeBelow TreeCursor a b
tc of
    CForest b
EmptyCForest -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing
    ClosedForest NonEmpty (Tree b)
_ -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing
    OpenForest NonEmpty (CTree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (Tree b) -> CForest b
forall a. NonEmpty (Tree a) -> CForest a
ClosedForest (NonEmpty (Tree b) -> CForest b) -> NonEmpty (Tree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (CTree b -> Tree b) -> NonEmpty (CTree b) -> NonEmpty (Tree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map CTree b -> Tree b
forall a. CTree a -> Tree a
rebuildCTree NonEmpty (CTree b)
ts}

treeCursorToggleCurrentForest :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorToggleCurrentForest :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorToggleCurrentForest TreeCursor a b
tc =
  case TreeCursor a b -> CForest b
forall a b. TreeCursor a b -> CForest b
treeBelow TreeCursor a b
tc of
    CForest b
EmptyCForest -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing
    ClosedForest NonEmpty (Tree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (CTree b) -> CForest b
forall a. NonEmpty (CTree a) -> CForest a
OpenForest (NonEmpty (CTree b) -> CForest b)
-> NonEmpty (CTree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (Tree b -> CTree b) -> NonEmpty (Tree b) -> NonEmpty (CTree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map Tree b -> CTree b
forall a. Tree a -> CTree a
makeCTree NonEmpty (Tree b)
ts}
    OpenForest NonEmpty (CTree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (Tree b) -> CForest b
forall a. NonEmpty (Tree a) -> CForest a
ClosedForest (NonEmpty (Tree b) -> CForest b) -> NonEmpty (Tree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (CTree b -> Tree b) -> NonEmpty (CTree b) -> NonEmpty (Tree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map CTree b -> Tree b
forall a. CTree a -> Tree a
rebuildCTree NonEmpty (CTree b)
ts}

treeCursorOpenCurrentForestRecursively :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorOpenCurrentForestRecursively :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorOpenCurrentForestRecursively TreeCursor a b
tc =
  case TreeCursor a b -> CForest b
forall a b. TreeCursor a b -> CForest b
treeBelow TreeCursor a b
tc of
    CForest b
EmptyCForest -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing
    ClosedForest NonEmpty (Tree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (CTree b) -> CForest b
forall a. NonEmpty (CTree a) -> CForest a
OpenForest (NonEmpty (CTree b) -> CForest b)
-> NonEmpty (CTree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (Tree b -> CTree b) -> NonEmpty (Tree b) -> NonEmpty (CTree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map (Bool -> Tree b -> CTree b
forall a. Bool -> Tree a -> CTree a
cTree Bool
True) NonEmpty (Tree b)
ts}
    OpenForest NonEmpty (CTree b)
_ -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing

treeCursorToggleCurrentForestRecursively :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorToggleCurrentForestRecursively :: TreeCursor a b -> Maybe (TreeCursor a b)
treeCursorToggleCurrentForestRecursively TreeCursor a b
tc =
  case TreeCursor a b -> CForest b
forall a b. TreeCursor a b -> CForest b
treeBelow TreeCursor a b
tc of
    CForest b
EmptyCForest -> Maybe (TreeCursor a b)
forall a. Maybe a
Nothing
    ClosedForest NonEmpty (Tree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (CTree b) -> CForest b
forall a. NonEmpty (CTree a) -> CForest a
OpenForest (NonEmpty (CTree b) -> CForest b)
-> NonEmpty (CTree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (Tree b -> CTree b) -> NonEmpty (Tree b) -> NonEmpty (CTree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map (Bool -> Tree b -> CTree b
forall a. Bool -> Tree a -> CTree a
cTree Bool
True) NonEmpty (Tree b)
ts}
    OpenForest NonEmpty (CTree b)
ts -> TreeCursor a b -> Maybe (TreeCursor a b)
forall a. a -> Maybe a
Just (TreeCursor a b -> Maybe (TreeCursor a b))
-> TreeCursor a b -> Maybe (TreeCursor a b)
forall a b. (a -> b) -> a -> b
$ TreeCursor a b
tc {treeBelow :: CForest b
treeBelow = NonEmpty (Tree b) -> CForest b
forall a. NonEmpty (Tree a) -> CForest a
ClosedForest (NonEmpty (Tree b) -> CForest b) -> NonEmpty (Tree b) -> CForest b
forall a b. (a -> b) -> a -> b
$ (CTree b -> Tree b) -> NonEmpty (CTree b) -> NonEmpty (Tree b)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map CTree b -> Tree b
forall a. CTree a -> Tree a
rebuildCTree NonEmpty (CTree b)
ts}