yDigitGroupStyle (Just (DigitGroups c (g:gs))) l s = addseps (g:|gs) (toInteger l) s
  where
    addseps (g:|gs) l s
        | l' > 0    = addseps gs' l' rest <> WideBuilder (TB.singleton c <> TB.fromText part) (fromIntegral g + 1)
        | otherwise = WideBuilder (TB.fromText s) (fromInteger l)
      where
        (rest, part) = T.splitAt (fromInteger l') s
        gs' = fromMaybe (g:|[]) $ nonEmpty gs
        l' = l - toInteger g

-- like journalCanonicaliseAmounts
-- | Canonicalise an amount's display style using the provided commodity style map.
can
applyDigitGroupStyle (Just (DigitGroups c (g:gs))) l s = addseps (g:|gs) (toInteger l) s
  where
    addseps (g:|gs) l s
        | l' > 0    = addseps gs' l' rest <> WideBuilder (TB.singleton c <> TB.fromText part) (fromIntegral g + 1)
        | otherwise = WideBuilder (TB.fromText s) (fromInteger l)
      where
        (rest, part) = T.splitAt (fromInteger l') s
        gs' = fromMaybe (g:|[]) $ nonEmpty gs
        l' = l - toInteger g

-- like journalCanonicaliseAmounts
-- | Canonicalise an amount's display style using the provided commodity style map.
applyDigitGroupStyle (Just (DigitGroups c (g:gs))) l s = addseps (g:|gs) (toInteger l) s
  where
    addseps (g:|gs) l s
        | l' > 0    = addseps gs' l' rest <> WideBuilder (TB.singleton c <> TB.fromText part) (fromIntegral g + 1)
        | otherwise = WideBuilder (TB.fromText s) (fromInteger l)
      where
        (rest, part) = T.splitAt (fromInteger l') s
        gs' = fromMaybe (g:|[]) $ nonEmpty gs
        l' = l - toInteger g

-- like journalCanonicaliseAmounts
-- | Canonicalise an amount's display style using the provided commodity style map.