{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE ViewPatterns        #-}

module Dovetail.Core.Data.Number.Format where

import Data.Foldable (fold)
import Data.Text (Text)
import Data.Text qualified as Text
import Dovetail
import Dovetail.Evaluate (builtIn)
import Numeric qualified

env :: forall ctx. Env ctx
env :: Env ctx
env = do
  let _ModuleName :: ModuleName
_ModuleName = Text -> ModuleName
ModuleName Text
"Data.Number.Format"

  [Env ctx] -> Env ctx
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold
    [ -- toPrecisionNative :: Int -> Number -> String
      ModuleName
-> Text -> (Integer -> Double -> Eval ctx Text) -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Integer -> Double -> Eval ctx Text)
        ModuleName
_ModuleName Text
"toPrecisionNative"
        \Integer
_ Double
_ ->
          EvaluationErrorType ctx -> Eval ctx Text
forall x (m :: * -> *) a.
(MonadError (EvaluationError x) m,
 MonadReader (EvaluationContext x) m) =>
EvaluationErrorType x -> m a
throwErrorWithContext (Text -> EvaluationErrorType ctx
forall ctx. Text -> EvaluationErrorType ctx
OtherError Text
"toPrecisionNative is not implemented")
      -- toFixedNative :: Int -> Number -> String
    , ModuleName
-> Text -> (Integer -> Double -> Eval ctx Text) -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Integer -> Double -> Eval ctx Text)
        ModuleName
_ModuleName Text
"toFixedNative"
        \Integer
p Double
x ->
          Text -> Eval ctx Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Eval ctx Text)
-> (String -> Text) -> String -> Eval ctx Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack (String -> Eval ctx Text) -> String -> Eval ctx Text
forall a b. (a -> b) -> a -> b
$ Maybe Int -> Double -> ShowS
forall a. RealFloat a => Maybe Int -> a -> ShowS
Numeric.showFFloat (Int -> Maybe Int
forall a. a -> Maybe a
Just (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
p)) Double
x String
""
      -- toExponentialNative :: Int -> Number -> String
    , ModuleName
-> Text -> (Integer -> Double -> Eval ctx Text) -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Integer -> Double -> Eval ctx Text)
        ModuleName
_ModuleName Text
"toExponentialNative"
        \Integer
p Double
x ->
          Text -> Eval ctx Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Eval ctx Text)
-> (String -> Text) -> String -> Eval ctx Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack (String -> Eval ctx Text) -> String -> Eval ctx Text
forall a b. (a -> b) -> a -> b
$ Maybe Int -> Double -> ShowS
forall a. RealFloat a => Maybe Int -> a -> ShowS
Numeric.showEFloat (Int -> Maybe Int
forall a. a -> Maybe a
Just (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
p)) Double
x String
""
      -- toString :: Number -> String
    , ModuleName -> Text -> (Double -> Eval ctx Text) -> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Double -> Eval ctx Text)
        ModuleName
_ModuleName Text
"toString"
        \Double
x ->
          Text -> Eval ctx Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Eval ctx Text)
-> (String -> Text) -> String -> Eval ctx Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack (String -> Eval ctx Text) -> String -> Eval ctx Text
forall a b. (a -> b) -> a -> b
$ Double -> ShowS
forall a. RealFloat a => a -> ShowS
Numeric.showFloat Double
x String
""
    ]