Here is one solution that satisfies the input you provided. I am not quite sure I understood your requirements correctly so if you were looking for something else, let us know:
module Main where
import Daml.Script
import DA.Assert
import DA.Next.Map (Map)
import qualified DA.Next.Map as Map
data Version = Version with
value : Text
deriving (Eq, Show)
processMap : Map Text [Version] -> Map Text [Version]
processMap mapList =
Map.fromList
(map (\(mapKey, versionList) -> (mapKey, processList versionList))
(Map.toList mapList))
processList : [Version] -> [Version]
processList versionList =
concatMap f versionList
where
f version
| version.value == "1000" = [version, Version "2000"]
| otherwise = [version]
test = script do
-- Original example
processMap (Map.fromList [("amount", [Version "1000"])])
=== Map.fromList [("amount", [Version "1000", Version "2000"])]
-- 2000 is inserted directly after 1000 not at the very end
processMap (Map.fromList [("amount", [Version "1000", Version "3000"])])
=== Map.fromList [("amount", [Version "1000", Version "2000", Version "3000"])]
-- This applies the same to all keys not just amount
processMap (Map.fromList [("notamount", [Version "1000", Version "3000"])])
=== Map.fromList [("notamount", [Version "1000", Version "2000", Version "3000"])]