/*
 * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <GLFW/glfw3.h>

#include <ox/std/string.hpp>

#include <turbine/turbine.hpp>

#include "context.hpp"

namespace turbine {

ox::String getClipboardText(Context const &ctx) noexcept {
	return ox::String(glfwGetClipboardString(ctx.window));
}

void setClipboardText(Context &ctx, ox::StringViewCR text) noexcept {
	auto cstr = ox_malloca(text.bytes() + 1, char);
	OX_ALLOW_UNSAFE_BUFFERS_BEGIN
	ox::strncpy(cstr.get(), text.data(), text.bytes());
	OX_ALLOW_UNSAFE_BUFFERS_END
	glfwSetClipboardString(ctx.window, cstr.get());
}

void setClipboardObject(Context &ctx, ox::UPtr<BaseClipboardObject> &&obj) noexcept {
	ctx.clipboard = std::move(obj);
}

ox::Result<BaseClipboardObject*> getClipboardData(Context &ctx, ox::StringViewCR typeId) noexcept {
	if (ctx.clipboard && ctx.clipboard->typeId() == typeId) {
		return ctx.clipboard.get();
	}
	return ox::Error(1);
}

}
