module Dodge.Base.NewID where
import qualified IntMapHelp as IM
import LensHelp

-- | generalised way of putting a new item into a lensed intmap, returning the
-- new index as well
plNewID :: ALens' b (IM.IntMap a)
    -> a
    -> b
    -> (Int,b)
plNewID l x w = (i,w & l #%~ IM.insert i x)
  where
    i = IM.newKey $ w ^# l

-- | place an new object into an intmap and update its id
plNewUpID :: ALens' b (IM.IntMap a)
    -> ALens' a Int
    -> a
    -> b
    -> (Int,b)
plNewUpID l li x w = (i,w & l #%~ IM.insert i (x & li #~ i))
  where
    i = IM.newKey $ w ^# l

-- | place an new object into an intmap and update its id
plNew :: ALens' b (IM.IntMap a)
    -> ALens' a Int
    -> a
    -> b
    -> b
plNew l li x = snd . plNewUpID l li x

-- | place an new object into an intmap using the new id
plNewUsing :: ALens' b (IM.IntMap a)
    -> (Int -> a)
    -> b
    -> b
plNewUsing l f x = x & l #%~ IM.insert i (f i)
  where
    i = IM.newKey $ x ^# l
