#pragma once

#include <Windows.h>
#include <sstream>
#include <string>

#include "external/il2cpp/il2cpp-object-internals.h"

namespace ta::adapters::il2cpp
{
    inline std::string conv_string(const Il2CppString* string)
    {
        if (!string || string->length <= 0)
            return {};

        const auto* utf16 = reinterpret_cast<const wchar_t*>(string->chars);
        const int utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, utf16, string->length,
                                                   nullptr, 0, nullptr, nullptr);
        if (utf8_size <= 0)
            return {};

        std::string result(static_cast<size_t>(utf8_size), '\0');
        WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, utf16, string->length,
                            result.data(), utf8_size, nullptr, nullptr);
        return result;
    }

    inline std::wstring conv_wstring(const Il2CppString* string)
    {
        if (!string || string->length <= 0)
            return {};

        std::wstringstream ss;
        for (int i = 0; i < string->length; ++i)
            ss << string->chars[i];
        return ss.str();
    }
}
