//go:build tinygo.wasm

// Code generated by protoc-gen-go-plugin. DO NOT EDIT.
// versions:
// 	protoc-gen-go-plugin v0.1.0
// 	protoc               v4.23.4
// source: hook.proto

package protos

import (
	context "context"
	wasm "github.com/knqyf263/go-plugin/wasm"
	_ "unsafe"
)

const HookPluginAPIVersion = 1

//export hook_api_version
func _hook_api_version() uint64 {
	return HookPluginAPIVersion
}

var hook Hook

func RegisterHook(p Hook) {
	hook = p
}

//export hook_metadata
func _hook_metadata(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(Empty)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.Metadata(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

//export hook_on_load
func _hook_on_load(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(HookLoadEvent)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.OnLoad(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

//export hook_on_unload
func _hook_on_unload(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(HookUnloadEvent)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.OnUnload(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

//export hook_on_device_join
func _hook_on_device_join(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(DeviceJoinEvent)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.OnDeviceJoin(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

//export hook_on_device_leave
func _hook_on_device_leave(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(DeviceLeaveEvent)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.OnDeviceLeave(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

//export hook_on_device_event
func _hook_on_device_event(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(DeviceEvent)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.OnDeviceEvent(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

//export hook_on_device_custom_event
func _hook_on_device_custom_event(ptr, size uint32) uint64 {
	b := wasm.PtrToByte(ptr, size)
	req := new(DeviceCustomEvent)
	if err := req.UnmarshalVT(b); err != nil {
		return 0
	}
	response, err := hook.OnDeviceCustomEvent(context.Background(), req)
	if err != nil {
		ptr, size = wasm.ByteToPtr([]byte(err.Error()))
		return (uint64(ptr) << uint64(32)) | uint64(size) |
			// Indicate that this is the error string by setting the 32-th bit, assuming that
			// no data exceeds 31-bit size (2 GiB).
			(1 << 31)
	}

	b, err = response.MarshalVT()
	if err != nil {
		return 0
	}
	ptr, size = wasm.ByteToPtr(b)
	return (uint64(ptr) << uint64(32)) | uint64(size)
}

type hookAPI struct{}

func NewHookAPI() HookAPI {
	return hookAPI{}
}

//go:wasm-module env
//export fire_event
//go:linkname _fire_event
func _fire_event(ptr uint32, size uint32) uint64

func (h hookAPI) FireEvent(ctx context.Context, request *FireEventReq) (*Empty, error) {
	buf, err := request.MarshalVT()
	if err != nil {
		return nil, err
	}
	ptr, size := wasm.ByteToPtr(buf)
	ptrSize := _fire_event(ptr, size)
	wasm.FreePtr(ptr)

	ptr = uint32(ptrSize >> 32)
	size = uint32(ptrSize)
	buf = wasm.PtrToByte(ptr, size)

	response := new(Empty)
	if err = response.UnmarshalVT(buf); err != nil {
		return nil, err
	}
	return response, nil
}
