import { z } from "zod";
import { RJSVM_Implementations, RJSVM_Interface, RJSVM, RJSVM_Config, RJSVM_Builder } from "rjsvm"

const shoutSchema = z.object({
    title: z.string(),
    body: z.string(),
    from: z.string(),
    id: z.optional(z.string())
})

type Shout = z.infer<typeof shoutSchema>

type State_T = {
    shouts: Shout[]
}

type RJSVM_Endpoints = {
    submit: (data: Shout) => void
}

abstract class RJSVM_Base
extends RJSVM<State_T, RJSVM_Endpoints>
implements RJSVM_Interface<RJSVM_Base> {
    state: State_T = {
        shouts: []
    }
}

const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
    submit: {
        implementation: function (env, shout) {
            this.state.shouts.unshift(shout)
            console.log(shout)
        },
        visibility: 'public',
        fee: 10,
        parameterSchema: shoutSchema
    }
}

const conf: RJSVM_Config = {
    listeningAddress: "rBGqUNVzrNJ6CNpTriBts8QCTNi5VqUMnZ",
    rippleNode: "wss://s.altnet.rippletest.net:51233"
}
const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract);

export const tns_rjsvm = new Rjsvm(conf)
tns_rjsvm.state.shouts