"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HashValue = void 0;
const bufio_1 = require("@node-dlc/bufio");
const HashByteOrder_1 = require("./HashByteOrder");
class HashValue {
    /**
     * Parses a hashed value in internal byte order. This is often
     * referred to as little-endian due to the block target needing to
     * be reversed.
     * @param stream
     */
    static parse(reader) {
        return new HashValue(reader.readBytes(32));
    }
    /**
     * Parses a hashed value in RPC byte order (which is the reverse) of
     * the natural or internal byte order. This is often referred to as
     * big-endian due to the reversed block target being big-endian.
     * @param stream
     */
    static fromRpcStream(reader) {
        const value = reader.readBytes(32).reverse();
        return new HashValue(value);
    }
    /**
     * Parses a hashed value in RPC byte order (which is the reverse) of
     * the natural or internal byte order. This is often referred to as
     * big-endian due to the reversed block target being big-endian.
     * @param value
     */
    static fromRpc(value) {
        const reader = bufio_1.StreamReader.fromHex(value);
        return this.fromRpcStream(reader);
    }
    /**
     * Constructs a new HashValue instance from the internal-byte-order
     * value provided.
     * @param value Interval-byte-order of a hash value
     */
    constructor(value) {
        this._value = value;
    }
    /**
     * Returns the hash value as a hex string. Defaults to using RPC
     * (reversed/big-endian) byte order.
     */
    toString(byteOrder = HashByteOrder_1.HashByteOrder.RPC) {
        if (byteOrder === HashByteOrder_1.HashByteOrder.RPC) {
            return Buffer.from(this._value).reverse().toString("hex"); // prettier-ignore
        }
        else {
            return Buffer.from(this._value).toString('hex');
        }
    }
    /**
     * Serializes to JSON returning a hex string. Defaults to using
     * RPC (reversed/big endian) byte order.
     */
    toJSON(byteOrder) {
        return this.toString(byteOrder);
    }
    /**
     * Serializes the hash value into an internal-byte order Buffer
     */
    serialize(byteOrder = HashByteOrder_1.HashByteOrder.Internal) {
        if (byteOrder === HashByteOrder_1.HashByteOrder.Internal) {
            return Buffer.from(this._value);
        }
        else {
            return Buffer.from(this._value).reverse();
        }
    }
    /**
     * Deep copy clone
     */
    clone() {
        return new HashValue(Buffer.from(this._value));
    }
}
exports.HashValue = HashValue;
//# sourceMappingURL=HashValue.js.map