{-# LANGUAGE ViewPatterns #-}

-- | Internal module for 'Percentage'.
--
-- @since 0.1
module Pythia.Data.Percentage.Internal
  ( Percentage (.., MkPercentage),
    unPercentage,
  )
where

import Language.Haskell.TH.Syntax (Lift)
import Numeric.Data.Interval (Interval (MkInterval), IntervalBound (Closed))
import Pythia.Prelude

-- | Represents a percentage.
--
-- @since 0.1
type Percentage :: Type
newtype Percentage = InternalPercentage (Interval (Closed 0) (Closed 100) Word8)
  deriving stock
    ( -- | @since 0.1
      Percentage -> Percentage -> Bool
(Percentage -> Percentage -> Bool)
-> (Percentage -> Percentage -> Bool) -> Eq Percentage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Percentage -> Percentage -> Bool
== :: Percentage -> Percentage -> Bool
$c/= :: Percentage -> Percentage -> Bool
/= :: Percentage -> Percentage -> Bool
Eq,
      -- | @since 0.1
      (forall x. Percentage -> Rep Percentage x)
-> (forall x. Rep Percentage x -> Percentage) -> Generic Percentage
forall x. Rep Percentage x -> Percentage
forall x. Percentage -> Rep Percentage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Percentage -> Rep Percentage x
from :: forall x. Percentage -> Rep Percentage x
$cto :: forall x. Rep Percentage x -> Percentage
to :: forall x. Rep Percentage x -> Percentage
Generic,
      -- | @since 0.1
      (forall (m :: Type -> Type). Quote m => Percentage -> m Exp)
-> (forall (m :: Type -> Type).
    Quote m =>
    Percentage -> Code m Percentage)
-> Lift Percentage
forall t.
(forall (m :: Type -> Type). Quote m => t -> m Exp)
-> (forall (m :: Type -> Type). Quote m => t -> Code m t) -> Lift t
forall (m :: Type -> Type). Quote m => Percentage -> m Exp
forall (m :: Type -> Type).
Quote m =>
Percentage -> Code m Percentage
$clift :: forall (m :: Type -> Type). Quote m => Percentage -> m Exp
lift :: forall (m :: Type -> Type). Quote m => Percentage -> m Exp
$cliftTyped :: forall (m :: Type -> Type).
Quote m =>
Percentage -> Code m Percentage
liftTyped :: forall (m :: Type -> Type).
Quote m =>
Percentage -> Code m Percentage
Lift,
      -- | @since 0.1
      Eq Percentage
Eq Percentage =>
(Percentage -> Percentage -> Ordering)
-> (Percentage -> Percentage -> Bool)
-> (Percentage -> Percentage -> Bool)
-> (Percentage -> Percentage -> Bool)
-> (Percentage -> Percentage -> Bool)
-> (Percentage -> Percentage -> Percentage)
-> (Percentage -> Percentage -> Percentage)
-> Ord Percentage
Percentage -> Percentage -> Bool
Percentage -> Percentage -> Ordering
Percentage -> Percentage -> Percentage
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Percentage -> Percentage -> Ordering
compare :: Percentage -> Percentage -> Ordering
$c< :: Percentage -> Percentage -> Bool
< :: Percentage -> Percentage -> Bool
$c<= :: Percentage -> Percentage -> Bool
<= :: Percentage -> Percentage -> Bool
$c> :: Percentage -> Percentage -> Bool
> :: Percentage -> Percentage -> Bool
$c>= :: Percentage -> Percentage -> Bool
>= :: Percentage -> Percentage -> Bool
$cmax :: Percentage -> Percentage -> Percentage
max :: Percentage -> Percentage -> Percentage
$cmin :: Percentage -> Percentage -> Percentage
min :: Percentage -> Percentage -> Percentage
Ord,
      -- | @since 0.1
      Int -> Percentage -> ShowS
[Percentage] -> ShowS
Percentage -> String
(Int -> Percentage -> ShowS)
-> (Percentage -> String)
-> ([Percentage] -> ShowS)
-> Show Percentage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Percentage -> ShowS
showsPrec :: Int -> Percentage -> ShowS
$cshow :: Percentage -> String
show :: Percentage -> String
$cshowList :: [Percentage] -> ShowS
showList :: [Percentage] -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.1
      Percentage -> ()
(Percentage -> ()) -> NFData Percentage
forall a. (a -> ()) -> NFData a
$crnf :: Percentage -> ()
rnf :: Percentage -> ()
NFData
    )

-- | Pattern synonym for Percentage.
--
-- @since 0.1
pattern MkPercentage :: Word8 -> Percentage
pattern $mMkPercentage :: forall {r}. Percentage -> (Word8 -> r) -> ((# #) -> r) -> r
MkPercentage x <- (unPercentage -> x)

{-# COMPLETE MkPercentage #-}

-- | @since 0.1
instance Display Percentage where
  displayBuilder :: Percentage -> Builder
displayBuilder (InternalPercentage (MkInterval Word8
x)) =
    String -> Builder
forall a. Display a => a -> Builder
displayBuilder (Word8 -> String
forall a. Show a => a -> String
show Word8
x) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"%"

-- | Retrieve the raw percentage.
--
-- @since 0.1
unPercentage :: Percentage -> Word8
unPercentage :: Percentage -> Word8
unPercentage (InternalPercentage (MkInterval Word8
x)) = Word8
x