"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");
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("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) {
        this.write(`package module\n`);
        this.write(`\n`);
        this.write(`import (\n`);
        this.write(`"context"\n`);
        this.write(`\n`);
        this.write(`\t"github.com/wapc/wapc-go"\n`);
        this.write(`\t"github.com/vmihailenco/msgpack/v4"\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