2P34LOESTKTXTUTLVASZEKZ3VEFVHBJDZJ3LLAOGKH3GECC76LCQC
move old new = R $ \card ->
let removeOld x = x & categories %~ Map.delete (toText old)
oldCopies = card ^? categories . ix (toText old) . copies
move old new numCopies = R $ \card ->
let oldCopies = card ^? categories . ix (toText old) . copies
copiesToMove = oldCopies <&> max numCopies
in oldCopies <&> removeOld . (\x -> card & categories .~ updateCopies x)
updateOld =
Map.update
( \y ->
let (l, r) = (toNumber @Int $ y ^. copies, toNumber @Int numCopies)
in if (y ^. copies) <= numCopies
then Nothing
else Just $ y & copies .~ (PositiveNumber . unsafeRefine . fromIntegral $ (r - l))
)
(toText old)
(card ^. categories)
in copiesToMove <&> (\x -> x & categories .~ updateOld) . (\x -> card & categories .~ updateCopies x)