#include "platform_api/platform_services.h"

#include "platform_api/platform_policy.h"

namespace pp::platform {
namespace {

class DefaultWebPlatformServices final : public WebPlatformServices {
public:
    void publish_exported_image(std::string_view path) override
    {
        (void)path;
    }

    void flush_persistent_storage() override
    {
    }

    [[nodiscard]] int default_canvas_resolution() override
    {
        return platform_default_canvas_resolution(PlatformFamily::webgl);
    }

    void save_prepared_file(
        std::string_view path,
        std::string_view suggested_name,
        PreparedFileCallback callback) override
    {
        (void)suggested_name;
        callback(std::string(path), false);
    }
};

WebPlatformServices* g_injected_web_platform_services = nullptr;

static_assert(sizeof(PlatformServices*) == sizeof(void*));
}

WebPlatformServices& default_web_platform_services()
{
    static DefaultWebPlatformServices services;
    return services;
}

WebPlatformServices* injected_web_platform_services() noexcept
{
    return g_injected_web_platform_services;
}

void set_injected_web_platform_services(WebPlatformServices* services) noexcept
{
    g_injected_web_platform_services = services;
}

WebPlatformServices& resolve_web_platform_services(WebPlatformServices& fallback) noexcept
{
    if (auto* services = injected_web_platform_services())
        return *services;
    return fallback;
}

WebPlatformServices& active_web_platform_services()
{
    return resolve_web_platform_services(default_web_platform_services());
}
}
