#pragma once

#include <unordered_map>
#include "Types.hpp"
#include <functional>
#include "elements/BehaviourScript.hpp"

namespace TSE::EDITOR
{
    class BehaviourScriptRegistry
    {
    private:
        using BehaviourScriptCtor = BehaviourScript* (*)();

        inline static std::unordered_map<string, BehaviourScriptCtor> registeredScripts = {};

    public:
        inline static void RegisterBehaviourScript(string name, BehaviourScriptCtor ctor )
        {
            registeredScripts[name] = ctor;
        }
        inline static BehaviourScript* CreateBehaviourScript(string name)
        {
            auto it = registeredScripts.find(name);
            if (it != registeredScripts.end())
                return it->second();
            return nullptr;
        }
        inline static void UnregisterBehaviourScript(string name)
        {
            registeredScripts.erase(name);
        }
        inline static int GetScriptCount()
        {
            return registeredScripts.size();
        }
        inline static string GetScriptNameAt(int i)
        {
            if (i < 0 || i >= static_cast<int>(registeredScripts.size()))
                return "";

            auto it = registeredScripts.begin();
            std::advance(it, i);
            return it->first;
        }
        
        static const auto& GetRegistry()
        {
            return registeredScripts;
        }
    };
} // namespace TSE::EDITOR

