module FFProbe.Data.Tags.Internal (parseTags) where

import Data.Aeson
import Data.Aeson.Key (toString)
import Data.Aeson.KeyMap (toList)
import Data.Aeson.Types (Parser)
import Data.Bifunctor (Bifunctor (bimap))
import Data.Text (unpack)
import FFProbe.Data.Tags

parseTags :: Value -> Parser TagList
parseTags :: Value -> Parser TagList
parseTags = String -> (Object -> Parser TagList) -> Value -> Parser TagList
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Tags" ((Object -> Parser TagList) -> Value -> Parser TagList)
-> (Object -> Parser TagList) -> Value -> Parser TagList
forall a b. (a -> b) -> a -> b
$ \Object
kvmap -> do
    TagList -> Parser TagList
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TagList -> Parser TagList) -> TagList -> Parser TagList
forall a b. (a -> b) -> a -> b
$ ((Key, Value) -> (String, TagValue)) -> [(Key, Value)] -> TagList
forall a b. (a -> b) -> [a] -> [b]
map ((Key -> String)
-> (Value -> TagValue) -> (Key, Value) -> (String, TagValue)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap Key -> String
toString Value -> TagValue
parseTag) (Object -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
toList Object
kvmap)
    where
        parseTag :: Value -> TagValue
parseTag (String Text
v) = String -> TagValue
StringTag (String -> TagValue) -> String -> TagValue
forall a b. (a -> b) -> a -> b
$ Text -> String
unpack Text
v
        parseTag Value
x = String -> TagValue
Other (String -> TagValue) -> String -> TagValue
forall a b. (a -> b) -> a -> b
$ Value -> String
forall a. Show a => a -> String
show Value
x