"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderNegotiationFieldsV1 = exports.OrderNegotiationFieldsV0 = exports.DisjointOrderNegotiationFields = exports.SingleOrderNegotiationFields = exports.OrderNegotiationFields = void 0;
const bufio_1 = require("@node-dlc/bufio");
const OrderOffer_1 = require("./OrderOffer");
/**
 * Order negotiation fields for order contract negotiation.
 * Follows the same pattern as NegotiationFields with Single and Disjoint variants.
 */
class OrderNegotiationFields {
    static deserialize(buf) {
        const reader = new bufio_1.BufferReader(buf);
        const discriminator = Number(reader.readBigSize());
        switch (discriminator) {
            case 0:
                return SingleOrderNegotiationFields.deserialize(buf);
            case 1:
                return DisjointOrderNegotiationFields.deserialize(buf);
            default:
                throw new Error(`Invalid OrderNegotiationFields discriminator: ${discriminator}. Must be 0 (Single) or 1 (Disjoint)`);
        }
    }
    /**
     * Creates OrderNegotiationFields from JSON data
     * @param json JSON object representing order negotiation fields
     */
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
    static fromJSON(json) {
        if (!json || typeof json !== 'object') {
            throw new Error('Invalid JSON input for OrderNegotiationFields');
        }
        const variant = json.variant;
        switch (variant) {
            case 'Single':
                return SingleOrderNegotiationFields.fromJSON(json);
            case 'Disjoint':
                return DisjointOrderNegotiationFields.fromJSON(json);
            default:
                throw new Error(`Unknown order negotiation fields variant: ${variant}. Must be 'Single' or 'Disjoint'`);
        }
    }
}
exports.OrderNegotiationFields = OrderNegotiationFields;
/**
 * Order negotiation fields for contract based on a single event (basic/empty).
 */
class SingleOrderNegotiationFields extends OrderNegotiationFields {
    constructor() {
        super(...arguments);
        this.variant = 'Single';
        this.discriminator = 0;
    }
    /**
     * Creates a SingleOrderNegotiationFields from JSON data
     * @param json JSON object representing single order negotiation fields
     */
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
    static fromJSON(json) {
        const instance = new SingleOrderNegotiationFields();
        if (json.variant !== 'Single') {
            throw new Error(`Invalid variant for SingleOrderNegotiationFields: expected 'Single', got ${json.variant}`);
        }
        // Single order negotiation fields are currently empty/basic
        return instance;
    }
    /**
     * Deserializes single order negotiation fields
     * @param buf
     */
    static deserialize(buf) {
        const instance = new SingleOrderNegotiationFields();
        const reader = new bufio_1.BufferReader(buf);
        reader.readBigSize(); // read discriminator (0)
        // Single order negotiation fields are currently empty
        return instance;
    }
    /**
     * Converts single order negotiation fields to JSON
     */
    toJSON() {
        return {
            variant: this.variant,
        };
    }
    /**
     * Serializes the single order negotiation fields into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(this.discriminator);
        // Single order negotiation fields are currently empty
        return writer.toBuffer();
    }
}
exports.SingleOrderNegotiationFields = SingleOrderNegotiationFields;
/**
 * Order negotiation fields for contract based on multiple events.
 */
class DisjointOrderNegotiationFields extends OrderNegotiationFields {
    constructor() {
        super(...arguments);
        this.variant = 'Disjoint';
        this.discriminator = 1;
    }
    /**
     * Creates a DisjointOrderNegotiationFields from JSON data
     * @param json JSON object representing disjoint order negotiation fields
     */
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
    static fromJSON(json) {
        const instance = new DisjointOrderNegotiationFields();
        if (json.variant !== 'Disjoint') {
            throw new Error(`Invalid variant for DisjointOrderNegotiationFields: expected 'Disjoint', got ${json.variant}`);
        }
        if (!json.orderOffer) {
            throw new Error('DisjointOrderNegotiationFields requires orderOffer field');
        }
        instance.orderOffer = OrderOffer_1.OrderOffer.fromJSON
            ? OrderOffer_1.OrderOffer.fromJSON(json.orderOffer)
            : OrderOffer_1.OrderOffer.deserialize(Buffer.from(json.orderOffer, 'hex'));
        return instance;
    }
    /**
     * Deserializes disjoint order negotiation fields
     * @param buf
     */
    static deserialize(buf) {
        const instance = new DisjointOrderNegotiationFields();
        const reader = new bufio_1.BufferReader(buf);
        reader.readBigSize(); // read discriminator (1)
        const orderOfferLength = reader.readBigSize();
        const orderOfferBuf = reader.readBytes(Number(orderOfferLength));
        instance.orderOffer = OrderOffer_1.OrderOffer.deserialize(orderOfferBuf);
        return instance;
    }
    /**
     * Converts disjoint order negotiation fields to JSON
     */
    toJSON() {
        return {
            variant: this.variant,
            orderOffer: this.orderOffer.toJSON(),
        };
    }
    /**
     * Serializes the disjoint order negotiation fields into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(this.discriminator);
        const orderOfferData = this.orderOffer.serialize();
        writer.writeBigSize(orderOfferData.length);
        writer.writeBytes(orderOfferData);
        return writer.toBuffer();
    }
}
exports.DisjointOrderNegotiationFields = DisjointOrderNegotiationFields;
// Legacy exports for backward compatibility - map to new structure
exports.OrderNegotiationFieldsV0 = SingleOrderNegotiationFields; // V0 was basic/empty
exports.OrderNegotiationFieldsV1 = DisjointOrderNegotiationFields; // V1 had OrderOffer
//# sourceMappingURL=OrderNegotiationFields.js.map