"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScaffoldVisitor = void 0;
const ast_1 = require("@wapc/widl/ast");
const helpers_1 = require("./helpers");
const utils_1 = require("../utils");
class ScaffoldVisitor extends ast_1.BaseVisitor {
    constructor(writer) {
        super(writer);
    }
    visitDocumentBefore(context) {
        const importPath = context.config.import || "github.com/myorg/mymodule/pkg/module";
        let module = context.config.module || "";
        if (module.length > 0) {
            module += " ";
        }
        super.visitDocumentBefore(context);
        this.write(`package main

    import (
      ${module}"${importPath}"
    )\n\n`);
    }
    visitAllOperationsBefore(context) {
        this.write(`\n`);
        const registration = new HandlerRegistrationVisitor(this.writer);
        context.document.accept(context, registration);
    }
    visitOperation(context) {
        if (!utils_1.shouldIncludeHandler(context)) {
            return;
        }
        const packageName = context.config["package"] || "module";
        const operation = context.operation;
        this.write(`\n`);
        this.write(`func ${operation.name.value}(${helpers_1.mapArgs(operation.arguments, packageName)})`);
        if (!helpers_1.isVoid(operation.type)) {
            this.write(` (${helpers_1.expandType(operation.type, packageName, true, helpers_1.isReference(operation.annotations))}, error)`);
        }
        else {
            this.write(` error`);
        }
        this.write(` {\n`);
        if (!helpers_1.isVoid(operation.type)) {
            const dv = helpers_1.defaultValueForType(operation.type, packageName);
            this.write(`  return ${dv}, nil`);
        }
        else {
            this.write(`  return nil`);
        }
        this.write(` // TODO: Provide implementation.\n`);
        this.write(`}\n`);
    }
}
exports.ScaffoldVisitor = ScaffoldVisitor;
class HandlerRegistrationVisitor extends ast_1.BaseVisitor {
    constructor(writer) {
        super(writer);
    }
    visitAllOperationsBefore(context) {
        this.write(`func main() {
      module.Handlers{\n`);
    }
    visitOperation(context) {
        if (!utils_1.shouldIncludeHandler(context)) {
            return;
        }
        const operation = context.operation;
        this.write(`\t\t${helpers_1.capitalize(operation.name.value)}: ${operation.name.value},\n`);
    }
    visitAllOperationsAfter(context) {
        this.write(`}.Register()
  }\n`);
    }
}
//# sourceMappingURL=scaffold_visitor.js.map