-- |Diff Combinator, Internal
module Polysemy.Time.Diff where

import Torsor (Torsor, difference)

import Polysemy.Time.Class.Instant (Instant, dateTime)
import Polysemy.Time.Data.TimeUnit (TimeUnit, convert)
import Polysemy.Time.Effect.Time (Time)
import qualified Polysemy.Time.Effect.Time as Time

-- |Subtract two arbitrary values that can be converted to an 'Instant'.
diff ::
   dt u i1 i2 diff .
  TimeUnit diff =>
  TimeUnit u =>
  Torsor dt diff =>
  Instant i1 dt =>
  Instant i2 dt =>
  i1 ->
  i2 ->
  u
diff :: forall dt u i1 i2 diff.
(TimeUnit diff, TimeUnit u, Torsor dt diff, Instant i1 dt,
 Instant i2 dt) =>
i1 -> i2 -> u
diff i1
i1 i2
i2 =
  forall a b. (TimeUnit a, TimeUnit b) => a -> b
convert (forall p v. Torsor p v => p -> p -> v
difference (forall i dt. Instant i dt => i -> dt
dateTime i1
i1) (forall i dt. Instant i dt => i -> dt
dateTime i2
i2))

-- |Calculate the duration between the given 'Instant' and the current time.
since ::
   u t d dt diff r .
  TimeUnit diff =>
  TimeUnit u =>
  Instant t dt =>
  Torsor dt diff =>
  Member (Time t d) r =>
  t ->
  Sem r u
since :: forall u t d dt diff (r :: EffectRow).
(TimeUnit diff, TimeUnit u, Instant t dt, Torsor dt diff,
 Member (Time t d) r) =>
t -> Sem r u
since t
time = do
  t
now <- forall t d (r :: EffectRow). Member (Time t d) r => Sem r t
Time.now @t @d
  pure (forall dt u i1 i2 diff.
(TimeUnit diff, TimeUnit u, Torsor dt diff, Instant i1 dt,
 Instant i2 dt) =>
i1 -> i2 -> u
diff @dt t
now t
time)