/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Go WASM handler for AbstractWasmPluginDataHandler.
// Exports six handler functions — all (i64) -> ().
// Compile with: tinygo build -target wasm-unknown -opt=2 -no-debug -panic=trap -o plugin.wasm main.go

package main

import (
	"shenyu/wasmabi"
)

func handle(argId int64) {
	buf := make([]byte, 1024)
	// get arg from java
	input := wasmabi.GetArgs(argId, buf)
	wasmabi.Eprintln("go side-> recv:" + string(input))
	// pass go result to java
	wasmabi.PutResult(argId, []byte("go result"))
}

//go:wasmexport handlerPlugin
func handlerPlugin(argId int64) {
	wasmabi.Eprintln("go side-> handlerPlugin")
	handle(argId)
}

//go:wasmexport removePlugin
func removePlugin(argId int64) {
	wasmabi.Eprintln("go side-> removePlugin")
	handle(argId)
}

//go:wasmexport handlerSelector
func handlerSelector(argId int64) {
	wasmabi.Eprintln("go side-> handlerSelector")
	handle(argId)
}

//go:wasmexport removeSelector
func removeSelector(argId int64) {
	wasmabi.Eprintln("go side-> removeSelector")
	handle(argId)
}

//go:wasmexport handlerRule
func handlerRule(argId int64) {
	wasmabi.Eprintln("go side-> handlerRule")
	handle(argId)
}

//go:wasmexport removeRule
func removeRule(argId int64) {
	wasmabi.Eprintln("go side-> removeRule")
	handle(argId)
}

// main is required by TinyGo's WASI target but is never called.
func main() {}
