module Mischief.Assets.AssetRef
  ( AssetRef (..),
    getAsset,
    getAsset',
    getRefEntity,
    qasset,
    qassetRec,
    qasset',
    ToAssetRef (..),
  )
where

import Data.Set qualified as Set
import Mischief.Assets.Internal.Asset
import Mischief.ECS

-- | Gets the value of an asset.
qasset' :: forall a m w. (MonadSystem w m) => AssetRef a -> Query m a
qasset' :: forall a (m :: * -> *) w.
MonadSystem w m =>
AssetRef a -> Query m a
qasset' AssetRef a
ref = Maybe a -> Query m a
forall w (m :: * -> *) a. MonadSystem w m => Maybe a -> Query m a
qmaybe (Maybe a -> Query m a) -> Query m (Maybe a) -> Query m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m (Maybe a) -> Query m (Maybe a)
forall w (m :: * -> *) a. MonadSystem w m => m a -> Query m a
qlift (AssetRef a -> m (Maybe a)
forall a w (m :: * -> *).
MonadSystem w m =>
AssetRef a -> m (Maybe a)
getAsset' AssetRef a
ref)

-- | Gets the value of an asset. If it isn't loaded, it will attempt to start loading it.
qasset :: forall a. AssetRef a -> Query System a
qasset :: forall a. AssetRef a -> Query System a
qasset AssetRef a
ref = Maybe a -> Query System a
forall w (m :: * -> *) a. MonadSystem w m => Maybe a -> Query m a
qmaybe (Maybe a -> Query System a)
-> Query System (Maybe a) -> Query System a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< System (Maybe a) -> Query System (Maybe a)
forall w (m :: * -> *) a. MonadSystem w m => m a -> Query m a
qlift (AssetRef a -> IsRec -> Set TypeRep -> System (Maybe a)
forall a. AssetRef a -> IsRec -> Set TypeRep -> System (Maybe a)
getAsset AssetRef a
ref IsRec
False Set TypeRep
forall a. Set a
Set.empty)

qassetRec :: forall a. AssetRef a -> Query System a
qassetRec :: forall a. AssetRef a -> Query System a
qassetRec AssetRef a
ref = Maybe a -> Query System a
forall w (m :: * -> *) a. MonadSystem w m => Maybe a -> Query m a
qmaybe (Maybe a -> Query System a)
-> Query System (Maybe a) -> Query System a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< System (Maybe a) -> Query System (Maybe a)
forall w (m :: * -> *) a. MonadSystem w m => m a -> Query m a
qlift (AssetRef a -> IsRec -> Set TypeRep -> System (Maybe a)
forall a. AssetRef a -> IsRec -> Set TypeRep -> System (Maybe a)
getAsset AssetRef a
ref IsRec
True Set TypeRep
forall a. Set a
Set.empty)