    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE DeriveGeneric #-}
    module Codec.Xlsx.Types.Internal.CfPair where

    import GHC.Generics (Generic)
    import Text.XML.Cursor

    import Codec.Xlsx.Parser.Internal
    import Codec.Xlsx.Types.Common
    import Codec.Xlsx.Types.ConditionalFormatting
    import Codec.Xlsx.Writer.Internal

    -- | Internal helper type for parsing "conditionalFormatting recods
    -- TODO: pivot, extList
    -- Implementing those will need this implementation to be changed
    --
    -- See 18.3.1.18 "conditionalFormatting (Conditional Formatting)" (p. 1610)
    newtype CfPair = CfPair
        { CfPair -> (SqRef, ConditionalFormatting)
    unCfPair :: (SqRef, ConditionalFormatting)
        } deriving (CfPair -> CfPair -> Bool
    (CfPair -> CfPair -> Bool)
    -> (CfPair -> CfPair -> Bool) -> Eq CfPair
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: CfPair -> CfPair -> Bool
    == :: CfPair -> CfPair -> Bool
    $c/= :: CfPair -> CfPair -> Bool
    /= :: CfPair -> CfPair -> Bool
    Eq, Int -> CfPair -> ShowS
    [CfPair] -> ShowS
    CfPair -> String
    (Int -> CfPair -> ShowS)
    -> (CfPair -> String) -> ([CfPair] -> ShowS) -> Show CfPair
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> CfPair -> ShowS
    showsPrec :: Int -> CfPair -> ShowS
    $cshow :: CfPair -> String
    show :: CfPair -> String
    $cshowList :: [CfPair] -> ShowS
    showList :: [CfPair] -> ShowS
    Show, (forall x. CfPair -> Rep CfPair x)
    -> (forall x. Rep CfPair x -> CfPair) -> Generic CfPair
    forall x. Rep CfPair x -> CfPair
    forall x. CfPair -> Rep CfPair x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. CfPair -> Rep CfPair x
    from :: forall x. CfPair -> Rep CfPair x
    $cto :: forall x. Rep CfPair x -> CfPair
    to :: forall x. Rep CfPair x -> CfPair
    Generic)

    instance FromCursor CfPair where
        fromCursor :: Cursor -> [CfPair]
    fromCursor Cursor
    cur = do
            sqref <- Name -> Cursor -> [SqRef]
    forall a. FromAttrVal a => Name -> Cursor -> [a]
    fromAttribute Name
    "sqref" Cursor
    cur
            let cfRules = Cursor
    cur Cursor
    -> (Cursor -> ConditionalFormatting) -> ConditionalFormatting
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Name -> Axis
    element (Text -> Name
    n_ Text
    "cfRule") Axis
    -> (Cursor -> ConditionalFormatting)
    -> Cursor
    -> ConditionalFormatting
    forall (m :: * -> *) a b c.
    Monad m =>
    (a -> m b) -> (b -> m c) -> a -> m c
    >=> Cursor -> ConditionalFormatting
    forall a. FromCursor a => Cursor -> [a]
    fromCursor
            return $ CfPair (sqref, cfRules)

    instance FromXenoNode CfPair where
      fromXenoNode :: Node -> Either Text CfPair
    fromXenoNode Node
    root = do
        sqref <- Node -> AttrParser SqRef -> Either Text SqRef
    forall a. Node -> AttrParser a -> Either Text a
    parseAttributes Node
    root (AttrParser SqRef -> Either Text SqRef)
    -> AttrParser SqRef -> Either Text SqRef
    forall a b. (a -> b) -> a -> b
    $ ByteString -> AttrParser SqRef
    forall a. FromAttrBs a => ByteString -> AttrParser a
    fromAttr ByteString
    "sqref"
        cfRules <- collectChildren root $ fromChildList "cfRule"
        return $ CfPair (sqref, cfRules)

    instance ToElement CfPair where
        toElement :: Name -> CfPair -> Element
    toElement Name
    nm (CfPair (SqRef
    sqRef, ConditionalFormatting
    cfRules)) =
            Name -> [(Name, Text)] -> [Element] -> Element
    elementList Name
    nm [ Name
    "sqref" Name -> Text -> (Name, Text)
    forall a. ToAttrVal a => Name -> a -> (Name, Text)
    .= SqRef -> Text
    forall a. ToAttrVal a => a -> Text
    toAttrVal SqRef
    sqRef ]
                        ((CfRule -> Element) -> ConditionalFormatting -> [Element]
    forall a b. (a -> b) -> [a] -> [b]
    map (Name -> CfRule -> Element
    forall a. ToElement a => Name -> a -> Element
    toElement Name
    "cfRule") ConditionalFormatting
    cfRules)