    {-# LANGUAGE OverloadedStrings #-}

    module Network.Protocol.MusicBrainz.Utils (
        nameCreditsToArtistName
      , nameCreditsToArtistSortName
    ) where

    import Control.Applicative ((<|>), liftA2)
    import Data.Maybe (catMaybes)
    import Data.Text (Text)
    import qualified Data.Text as T

    import Network.Protocol.MusicBrainz.Types

    nameCreditsToArtistName :: [ArtistCredit] -> Text
    nameCreditsToArtistName :: [ArtistCredit] -> Text
    nameCreditsToArtistName = [Text] -> Text
    T.concat ([Text] -> Text)
    -> ([ArtistCredit] -> [Text]) -> [ArtistCredit] -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (ArtistCredit -> Text) -> [ArtistCredit] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map ((ArtistCredit -> Maybe Text) -> ArtistCredit -> Text
    nameSpan (Artist -> Maybe Text
    _artistName (Artist -> Maybe Text)
    -> (ArtistCredit -> Artist) -> ArtistCredit -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ArtistCredit -> Artist
    _artistCreditArtist))

    nameCreditsToArtistSortName :: [ArtistCredit] -> Text
    nameCreditsToArtistSortName :: [ArtistCredit] -> Text
    nameCreditsToArtistSortName = [Text] -> Text
    T.concat ([Text] -> Text)
    -> ([ArtistCredit] -> [Text]) -> [ArtistCredit] -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (ArtistCredit -> Text) -> [ArtistCredit] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map ((ArtistCredit -> Maybe Text) -> ArtistCredit -> Text
    nameSpan ((Maybe Text -> Maybe Text -> Maybe Text)
    -> (Artist -> Maybe Text)
    -> (Artist -> Maybe Text)
    -> Artist
    -> Maybe Text
    forall a b c.
    (a -> b -> c) -> (Artist -> a) -> (Artist -> b) -> Artist -> c
    forall (f :: * -> *) a b c.
    Applicative f =>
    (a -> b -> c) -> f a -> f b -> f c
    liftA2 Maybe Text -> Maybe Text -> Maybe Text
    forall a. Maybe a -> Maybe a -> Maybe a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    (<|>) Artist -> Maybe Text
    _artistSortName Artist -> Maybe Text
    _artistName (Artist -> Maybe Text)
    -> (ArtistCredit -> Artist) -> ArtistCredit -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ArtistCredit -> Artist
    _artistCreditArtist))

    nameSpan :: (ArtistCredit -> Maybe Text) -> ArtistCredit -> Text
    nameSpan :: (ArtistCredit -> Maybe Text) -> ArtistCredit -> Text
    nameSpan ArtistCredit -> Maybe Text
    f = [Text] -> Text
    T.concat ([Text] -> Text)
    -> (ArtistCredit -> [Text]) -> ArtistCredit -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Maybe Text] -> [Text]
    forall a. [Maybe a] -> [a]
    catMaybes ([Maybe Text] -> [Text])
    -> (ArtistCredit -> [Maybe Text]) -> ArtistCredit -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Maybe Text -> [Maybe Text] -> [Maybe Text])
    -> (ArtistCredit -> Maybe Text)
    -> (ArtistCredit -> [Maybe Text])
    -> ArtistCredit
    -> [Maybe Text]
    forall a b c.
    (a -> b -> c)
    -> (ArtistCredit -> a) -> (ArtistCredit -> b) -> ArtistCredit -> c
    forall (f :: * -> *) a b c.
    Applicative f =>
    (a -> b -> c) -> f a -> f b -> f c
    liftA2 (:) ArtistCredit -> Maybe Text
    f (Maybe Text -> [Maybe Text]
    forall a. a -> [a]
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Maybe Text -> [Maybe Text])
    -> (ArtistCredit -> Maybe Text) -> ArtistCredit -> [Maybe Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ArtistCredit -> Maybe Text
    _artistCreditJoinPhrase)