module Database.Drasil.Dump where

import Language.Drasil (UID, HasUID(..))
import Database.Drasil.ChunkDB (refTable, labelledcontentTable, sectionTable, 
  conceptinsTable, theoryModelTable, gendefTable, insmodelTable, dataDefnTable,
  unitTable, UMap, ChunkDB(termTable, symbolTable))

import Data.Map.Strict (Map, insert)
import qualified Data.Map.Strict as SM

import Control.Lens ((^.))

type ChunkType = String
type DumpedChunkDB = Map ChunkType [UID]

umapDump :: HasUID a => UMap a -> [UID]
umapDump :: forall a. HasUID a => UMap a -> [UID]
umapDump = forall a b. (a -> b) -> [a] -> [b]
map ((forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [a]
SM.elems

dumpChunkDB :: ChunkDB -> DumpedChunkDB
dumpChunkDB :: ChunkDB -> Map ChunkType [UID]
dumpChunkDB ChunkDB
cdb = 
      forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"symbols" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB -> SymbolMap
symbolTable ChunkDB
cdb)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"terms" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB -> TermMap
termTable ChunkDB
cdb)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"concepts" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap ConceptInstance)
conceptinsTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"units" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap UnitDefn)
unitTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"dataDefinitions" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap DataDefinition)
dataDefnTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"instanceModels" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap InstanceModel)
insmodelTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"generalDefinitions" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap GenDefn)
gendefTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"theoryModels" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap TheoryModel)
theoryModelTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"conceptInstances" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap ConceptInstance)
conceptinsTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"sections" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap Section)
sectionTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"labelledContent" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap LabelledContent)
labelledcontentTable)
    forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"references" (forall a. HasUID a => UMap a -> [UID]
umapDump forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb forall s a. s -> Getting a s a -> a
^. Lens' ChunkDB (UMap Reference)
refTable)
      forall a. Monoid a => a
mempty