"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloseTLV = void 0;
const bitcoin_1 = require("@node-lightning/bitcoin");
const bufio_1 = require("@node-lightning/bufio");
const MessageType_1 = require("../MessageType");
class CloseTLV {
    constructor() {
        this.type = CloseTLV.type;
    }
    /**
     * Deserializes a close_tlv message
     * @param buf
     */
    static deserialize(buf) {
        const instance = new CloseTLV();
        const reader = bufio_1.StreamReader.fromBuffer(buf);
        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        instance.contractId = reader.readBytes(32);
        instance.offerPayoutSatoshis = reader.readBigUInt64BE();
        instance.offerFundingPubKey = reader.readBytes(33);
        instance.acceptFundingPubkey = reader.readBytes(33);
        instance.outpoint = bitcoin_1.OutPoint.parse(reader);
        return instance;
    }
    /**
     * Converts close_tlv to JSON
     */
    toJSON() {
        return {
            type: this.type,
            contractId: this.contractId.toString('hex'),
            offerPayoutSatoshis: Number(this.offerPayoutSatoshis),
            offerFundingPubKey: this.offerFundingPubKey.toString('hex'),
            acceptFundingPubkey: this.acceptFundingPubkey.toString('hex'),
            outpoint: this.outpoint.toJSON(), // Use OutPoint's toJSON method
        };
    }
    /**
     * Serializes the close_tlv message into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(CloseTLV.type);
        const dataWriter = new bufio_1.BufferWriter();
        dataWriter.writeBytes(this.contractId);
        dataWriter.writeUInt64BE(this.offerPayoutSatoshis);
        dataWriter.writeBytes(this.offerFundingPubKey);
        dataWriter.writeBytes(this.acceptFundingPubkey);
        dataWriter.writeBytes(this.outpoint.serialize()); // Use OutPoint's serialize method
        writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());
        return writer.toBuffer();
    }
}
exports.CloseTLV = CloseTLV;
CloseTLV.type = MessageType_1.MessageType.CloseTLV;
//# sourceMappingURL=CloseTLV.js.map