#pragma once

#include <flecs.h>
#include <memory>
#include <string>
#include <unordered_map>

namespace Asset {

struct AssetModule
{
    AssetModule(flecs::world& world);
};

template <typename T> struct AssetCache
{
    std::unordered_map<std::string, std::shared_ptr<T>> cache;
};

template <typename T> struct AssetLoader
{
    template <typename Args> std::shared_ptr<T> operator()(Args&&...)
    {
        static_assert(sizeof(T) == 0, "AssetLoader not implemented for this type.");
    }
};

template <typename T> std::shared_ptr<T> load(flecs::world& world, std::string asset_path)
{
    auto& asset_cache = world.ensure<AssetCache<T>>();

    auto asset_it = asset_cache.cache.find(asset_path);
    if (asset_it != asset_cache.cache.end())
        return asset_it->second;

    AssetLoader<T> asset_loader;
    auto loaded_asset = asset_loader(asset_path);

    asset_cache.cache.emplace(asset_path, loaded_asset);
    return loaded_asset;
}
} // namespace Asset
