"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderMetadataV0 = exports.OrderMetadata = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
class OrderMetadata {
    static deserialize(buf) {
        const reader = new bufio_1.BufferReader(buf);
        const type = Number(reader.readBigSize());
        switch (type) {
            case MessageType_1.MessageType.OrderMetadataV0:
                return OrderMetadataV0.deserialize(buf);
            default:
                throw new Error(`Order metadata TLV type must be OrderMetadataV0`);
        }
    }
}
exports.OrderMetadata = OrderMetadata;
/**
 * OrderMetadata message contains information about a node and indicates its
 * desire to enter into a new contract. This is the first step toward
 * order negotiation.
 */
class OrderMetadataV0 extends OrderMetadata {
    constructor() {
        super(...arguments);
        /**
         * The type for order_metadata_v0 message. order_metadata_v0 = 62774
         */
        this.type = OrderMetadataV0.type;
        /**
         * Timestamp for order creation
         */
        this.createdAt = 0;
        /**
         * Amount of time order is good untill
         */
        this.goodTill = 0;
    }
    /**
     * Deserializes an offer_dlc_v0 message
     * @param buf
     */
    static deserialize(buf) {
        const instance = new OrderMetadataV0();
        const reader = new bufio_1.BufferReader(buf);
        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        const offerIdLength = reader.readBigSize();
        const offerIdBuf = reader.readBytes(Number(offerIdLength));
        instance.offerId = offerIdBuf.toString();
        if (!reader.eof) {
            instance.createdAt = reader.readUInt32BE();
            instance.goodTill = reader.readUInt32BE();
        }
        return instance;
    }
    /**
     * Converts order_metadata_v0 to JSON
     */
    toJSON() {
        return {
            type: this.type,
            offerId: this.offerId,
            createdAt: this.createdAt,
            goodTill: this.goodTill,
        };
    }
    /**
     * Serializes the oracle_event message into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(this.type);
        const dataWriter = new bufio_1.BufferWriter();
        dataWriter.writeBigSize(this.offerId.length);
        dataWriter.writeBytes(Buffer.from(this.offerId));
        dataWriter.writeUInt32BE(this.createdAt);
        dataWriter.writeUInt32BE(this.goodTill);
        writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());
        return writer.toBuffer();
    }
}
exports.OrderMetadataV0 = OrderMetadataV0;
OrderMetadataV0.type = MessageType_1.MessageType.OrderMetadataV0;
//# sourceMappingURL=OrderMetadata.js.map