"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_varint_ts_1 = require("./utils-varint.js");
async function craftHandshake(hostname, port, protocolVersion) {
    let packetBody = await craftHandshakeBody(hostname, port, protocolVersion);
    // Field 1: Length of the entire object, (VarInt)
    // Field 2: PacketID, (VarInt)
    // Field 3: The body of the request
    const packetID = 0;
    let packetLengthBuffer = utils_varint_ts_1.default.encode(utils_varint_ts_1.default.encodingLength(packetID) + packetBody.length);
    let packetIDBuffer = utils_varint_ts_1.default.encode(packetID);
    let craftedHandshake = utils_varint_ts_1.default.concatUI8([
        packetLengthBuffer,
        packetIDBuffer,
        packetBody
    ]);
    return craftedHandshake;
}
async function craftHandshakeBody(hostname, port, protocolVersion) {
    // Field 1: The Protocol Version, (VarInt)
    // Field 2: The hostname of the server, (String) prefixed with it's length (VarInt)
    // Field 3: The port of the server, (UInt16)
    // Field 4: Next expected state, whether to get the status (1) or login (2), (VarInt)
    let protocolVersionBuffer = utils_varint_ts_1.default.encode(protocolVersion);
    let hostnamePrefixBuffer = utils_varint_ts_1.default.encode(hostname.length);
    let hostnameBuffer = new TextEncoder().encode(hostname);
    let portBuffer = utils_varint_ts_1.default.craftUInt16BE(port);
    let nextStateBuffer = utils_varint_ts_1.default.encode(1);
    let packetBody = utils_varint_ts_1.default.concatUI8([
        protocolVersionBuffer,
        hostnamePrefixBuffer,
        hostnameBuffer,
        portBuffer,
        nextStateBuffer
    ]);
    return packetBody;
}
async function craftEmptyPacket(packetID) {
    let packetLengthBuffer = utils_varint_ts_1.default.encode(utils_varint_ts_1.default.encodingLength(packetID));
    let packetIDBuffer = utils_varint_ts_1.default.encode(packetID);
    let craftedPacket = utils_varint_ts_1.default.concatUI8([
        packetLengthBuffer,
        packetIDBuffer
    ]);
    return craftedPacket;
}
async function craftPingPacket() {
    // Field 1: Length of the entire object, (VarInt)
    // Field 2: PacketID, (VarInt)
    // Field 3: Payload, (Long)
    //* The payload is the current time, however, it does not matter.
    // The server should return the same value back, but not all servers do.
    // The time of when the ping request was sent is stored in a variable.
    const packetID = 1;
    let longBuffer = utils_varint_ts_1.default.craftInt64BE(BigInt(Date.now()));
    let packetLengthBuffer = utils_varint_ts_1.default.encode(utils_varint_ts_1.default.encodingLength(packetID) + longBuffer.length);
    let packetIDBuffer = utils_varint_ts_1.default.encode(packetID);
    let craftedPacket = utils_varint_ts_1.default.concatUI8([
        packetLengthBuffer,
        packetIDBuffer,
        longBuffer
    ]);
    return craftedPacket;
}
exports.default = { craftHandshake, craftEmptyPacket, craftPingPacket };
//# sourceMappingURL=packetGenerator.js.map