"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlersVisitor = void 0;
const ast_1 = require("@wapc/widl/ast");
const helpers_1 = require("./helpers");
const utils_1 = require("../utils");
class HandlersVisitor extends ast_1.BaseVisitor {
    constructor(writer) {
        super(writer);
    }
    visitOperation(context) {
        if (!utils_1.shouldIncludeHandler(context)) {
            return;
        }
        if (context.config.handlerPreamble != true) {
            const className = context.config.handlersClassName || "Handlers";
            this.write(`#[cfg(feature = "guest")]
pub struct ${className} {}

#[cfg(feature = "guest")]
impl ${className} {\n`);
            context.config.handlerPreamble = true;
        }
        const operation = context.operation;
        this.write(utils_1.formatComment("    /// ", operation.description));
        this.write(`pub fn register_${helpers_1.functionName(operation.name.value)}(f: fn(`);
        operation.arguments.forEach((arg, i) => {
            if (i > 0) {
                this.write(`, `);
            }
            this.write(helpers_1.expandType(arg.type, undefined, true, helpers_1.isReference(arg.annotations)));
        });
        this.write(`) -> HandlerResult<`);
        if (!helpers_1.isVoid(operation.type)) {
            this.write(`${helpers_1.expandType(operation.type, undefined, true, helpers_1.isReference(operation.annotations))}`);
        }
        else {
            this.write(`()`);
        }
        this.write(`>) {
        *${helpers_1.functionName(operation.name.value).toUpperCase()}.write().unwrap() = Some(f);
        register_function(&"${operation.name.value}", ${helpers_1.functionName(operation.name.value)}_wrapper);
    }\n`);
        super.triggerOperation(context);
    }
    visitAllOperationsAfter(context) {
        if (context.config.handlerPreamble == true) {
            this.write(`}\n\n`);
            delete context.config.handlerPreamble;
        }
        super.triggerAllOperationsAfter(context);
    }
}
exports.HandlersVisitor = HandlersVisitor;
//# sourceMappingURL=handlers_visitor.js.map