{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TemplateHaskell #-}

module Emanote.Model.StaticFile where

import Commonmark.Extensions.WikiLink qualified as WL
import Data.Aeson qualified as Aeson
import Data.IxSet.Typed (Indexable (..), IxSet, ixFun, ixList)
import Data.Time (UTCTime)
import Emanote.Route qualified as R
import Optics.TH (makeLenses)
import Relude

data StaticFile = StaticFile
  { StaticFile -> R @SourceExt 'AnyExt
_staticFileRoute :: R.R 'R.AnyExt
  , StaticFile -> FilePath
_staticFilePath :: FilePath
  , StaticFile -> UTCTime
_staticFileTime :: UTCTime
  -- ^ Indicates that this file was updated no latter than the given time.
  }
  deriving stock (StaticFile -> StaticFile -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StaticFile -> StaticFile -> Bool
$c/= :: StaticFile -> StaticFile -> Bool
== :: StaticFile -> StaticFile -> Bool
$c== :: StaticFile -> StaticFile -> Bool
Eq, Eq StaticFile
StaticFile -> StaticFile -> Bool
StaticFile -> StaticFile -> Ordering
StaticFile -> StaticFile -> StaticFile
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
min :: StaticFile -> StaticFile -> StaticFile
$cmin :: StaticFile -> StaticFile -> StaticFile
max :: StaticFile -> StaticFile -> StaticFile
$cmax :: StaticFile -> StaticFile -> StaticFile
>= :: StaticFile -> StaticFile -> Bool
$c>= :: StaticFile -> StaticFile -> Bool
> :: StaticFile -> StaticFile -> Bool
$c> :: StaticFile -> StaticFile -> Bool
<= :: StaticFile -> StaticFile -> Bool
$c<= :: StaticFile -> StaticFile -> Bool
< :: StaticFile -> StaticFile -> Bool
$c< :: StaticFile -> StaticFile -> Bool
compare :: StaticFile -> StaticFile -> Ordering
$ccompare :: StaticFile -> StaticFile -> Ordering
Ord, Int -> StaticFile -> ShowS
[StaticFile] -> ShowS
StaticFile -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [StaticFile] -> ShowS
$cshowList :: [StaticFile] -> ShowS
show :: StaticFile -> FilePath
$cshow :: StaticFile -> FilePath
showsPrec :: Int -> StaticFile -> ShowS
$cshowsPrec :: Int -> StaticFile -> ShowS
Show, forall x. Rep StaticFile x -> StaticFile
forall x. StaticFile -> Rep StaticFile x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep StaticFile x -> StaticFile
$cfrom :: forall x. StaticFile -> Rep StaticFile x
Generic)
  deriving anyclass ([StaticFile] -> Encoding
[StaticFile] -> Value
StaticFile -> Encoding
StaticFile -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [StaticFile] -> Encoding
$ctoEncodingList :: [StaticFile] -> Encoding
toJSONList :: [StaticFile] -> Value
$ctoJSONList :: [StaticFile] -> Value
toEncoding :: StaticFile -> Encoding
$ctoEncoding :: StaticFile -> Encoding
toJSON :: StaticFile -> Value
$ctoJSON :: StaticFile -> Value
Aeson.ToJSON)

type StaticFileIxs = '[R.R 'R.AnyExt, WL.WikiLink]

type IxStaticFile = IxSet StaticFileIxs StaticFile

instance Indexable StaticFileIxs StaticFile where
  indices :: IxList StaticFileIxs StaticFile
indices =
    forall (ixs :: [Type]) a r. MkIxList ixs ixs a r => r
ixList
      (forall ix a. Ord ix => (a -> [ix]) -> Ix ix a
ixFun forall a b. (a -> b) -> a -> b
$ forall x. One x => OneItem x -> x
one forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticFile -> R @SourceExt 'AnyExt
_staticFileRoute)
      (forall ix a. Ord ix => (a -> [ix]) -> Ix ix a
ixFun forall a b. (a -> b) -> a -> b
$ forall (t :: Type -> Type) a. Foldable t => t a -> [a]
toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticFile -> NonEmpty WikiLink
staticFileSelfRefs)

staticFileSelfRefs :: StaticFile -> NonEmpty WL.WikiLink
staticFileSelfRefs :: StaticFile -> NonEmpty WikiLink
staticFileSelfRefs =
  forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => NonEmpty Slug -> NonEmpty (WikiLinkType, WikiLink)
WL.allowedWikiLinks
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (ext :: FileType a). R @a ext -> NonEmpty Slug
R.unRoute
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaticFile -> R @SourceExt 'AnyExt
_staticFileRoute

makeLenses ''StaticFile