"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClassVisitor = void 0;
const ast_1 = require("@wapc/widl/ast");
const helpers_1 = require("./helpers");
const decoder_visitor_1 = require("./decoder_visitor");
const encoder_visitor_1 = require("./encoder_visitor");
const utils_1 = require("../utils");
class ClassVisitor extends ast_1.BaseVisitor {
    constructor(writer) {
        super(writer);
    }
    visitObjectBefore(context) {
        super.triggerObjectBefore(context);
        this.write(utils_1.formatComment("// ", context.object.description));
        this.write(`export class ${context.object.name.value} implements Codec {\n`);
    }
    visitObjectField(context) {
        const field = context.field;
        this.write(utils_1.formatComment("  // ", field.description));
        this.write(`  ${field.name.value}: ${helpers_1.expandType(field.type, true, helpers_1.isReference(field.annotations))} = ${helpers_1.defValue(field)};\n`);
        super.triggerObjectField(context);
    }
    visitObjectFieldsAfter(context) {
        this.write(`\n`);
        const decoder = new decoder_visitor_1.DecoderVisitor(this.writer);
        context.object.accept(context, decoder);
        this.write(`\n`);
        const encoder = new encoder_visitor_1.EncoderVisitor(this.writer);
        context.object.accept(context, encoder);
        this.write(`\n`);
        this.write(`  toBuffer(): ArrayBuffer {
      let sizer = new Sizer();
      this.encode(sizer);
      let buffer = new ArrayBuffer(sizer.length);
      let encoder = new Encoder(buffer);
      this.encode(encoder);
      return buffer;
    }\n`);
        super.triggerObjectFieldsAfter(context);
    }
    visitObjectAfter(context) {
        this.write(`}\n\n`);
        super.triggerObjectAfter(context);
    }
}
exports.ClassVisitor = ClassVisitor;
//# sourceMappingURL=class_visitor.js.map