"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleVisitor = void 0;
const ast_1 = require("@wapc/widl/ast");
const helpers_1 = require("./helpers");
const struct_visitor_1 = require("./struct_visitor");
const host_visitor_1 = require("./host_visitor");
const handlers_visitor_1 = require("./handlers_visitor");
const wrappers_visitor_1 = require("./wrappers_visitor");
class ModuleVisitor extends ast_1.BaseVisitor {
    constructor(writer) {
        super(writer);
        this.setCallback("AllOperationsBefore", "host", (context) => {
            const host = new host_visitor_1.HostVisitor(writer);
            context.document.accept(context, host);
        });
        this.setCallback("AllOperationsBefore", "handlers", (context) => {
            const handlers = new handlers_visitor_1.HandlersVisitor(this.writer);
            context.document.accept(context, handlers);
            const register = new handlers_visitor_1.RegisterVisitor(this.writer);
            context.document.accept(context, register);
        });
        this.setCallback("AllOperationsBefore", "wrappers", (context) => {
            const wrapperVars = new wrappers_visitor_1.WrapperVarsVisitor(this.writer);
            context.document.accept(context, wrapperVars);
            const wrapperFuncs = new wrappers_visitor_1.WrapperFuncsVisitor(this.writer);
            context.document.accept(context, wrapperFuncs);
        });
        this.setCallback("OperationAfter", "arguments", (context) => {
            if (context.operation.isUnary()) {
                return;
            }
            const argObject = this.convertOperationToObject(context.operation);
            const struct = new struct_visitor_1.StructVisitor(this.writer);
            argObject.accept(context.clone({ object: argObject }), struct);
        });
        this.setCallback("Object", "struct", (context) => {
            const struct = new struct_visitor_1.StructVisitor(this.writer);
            context.object.accept(context, struct);
        });
    }
    visitDocumentBefore(context) {
        const packageName = context.config["package"] || "module";
        this.write(`package ${packageName}\n`);
        this.write(`\n`);
        this.write(`import (\n`);
        this.write(`\twapc "github.com/wapc/wapc-guest-tinygo"\n`);
        this.write(`\tmsgpack "github.com/wapc/tinygo-msgpack"\n`);
        this.write(`)\n\n`);
        super.triggerDocumentBefore(context);
    }
    convertOperationToObject(operation) {
        var fields = operation.arguments.map((arg) => {
            return new ast_1.FieldDefinition(arg.loc, arg.name, arg.description, arg.type, arg.default, arg.annotations);
        });
        return new ast_1.ObjectDefinition(operation.loc, new ast_1.Name(operation.name.loc, helpers_1.capitalize(operation.name.value) + "Args"), undefined, [], operation.annotations, fields);
    }
}
exports.ModuleVisitor = ModuleVisitor;
//# sourceMappingURL=module_visitor.js.map