"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Utilities_1 = require("../core/Utilities");
const Varint_1 = require("./Varint");
class LevelKeyValue {
    get unsharedKey() {
        if (this.unsharedKeyBytes === undefined) {
            return undefined;
        }
        const dv = new DataView(this.unsharedKeyBytes.buffer, this.unsharedKeyBytes.byteOffset, this.unsharedKeyBytes.byteLength);
        return Utilities_1.default.getAsciiString(dv, 0, dv.byteLength);
    }
    get key() {
        if (this.keyCached) {
            return this.keyCached;
        }
        const previous = this.previousKey;
        let key = "";
        if (previous !== undefined) {
            key = previous.key.substring(0, this.sharedByteLength);
        }
        const ukey = this.unsharedKey;
        if (ukey !== undefined) {
            key += ukey;
        }
        this.keyCached = key;
        return key;
    }
    get keyBytes() {
        if (!this.unsharedKeyBytes) {
            return undefined;
        }
        if (this.fullBytesCached) {
            return this.fullBytesCached;
        }
        if (this.sharedByteLength === undefined || this.sharedByteLength === 0) {
            return this.unsharedKeyBytes;
        }
        if (this.previousKey === undefined) {
            throw new Error("Unexpected shared key without a previous");
        }
        const previousBytes = this.previousKey.keyBytes;
        if (previousBytes === undefined) {
            throw new Error("Unexpected shared key without previous bytes");
        }
        const bytes = new Uint8Array(this.sharedByteLength + this.unsharedKeyBytes.length);
        const i = this.sharedByteLength;
        for (let j = 0; j < i; j++) {
            bytes[j] = previousBytes[j];
        }
        for (let j = 0; j < this.unsharedKeyBytes.length; j++) {
            bytes[j + i] = this.unsharedKeyBytes[j];
        }
        this.fullBytesCached = bytes;
        return bytes;
    }
    get isRestart() {
        return this.sharedByteLength === 0;
    }
    loadFromLdb(incomingBytes, startingIndex, prevKey) {
        this.fileBytes = incomingBytes;
        this.startIndex = startingIndex;
        let i = 0;
        const sharedBytes = new Varint_1.default(this.fileBytes, this.startIndex);
        this.sharedByteLength = sharedBytes.value;
        i += sharedBytes.byteLength;
        if (this.sharedByteLength > 0) {
            this.previousKey = prevKey;
        }
        const unsharedBytes = new Varint_1.default(this.fileBytes, this.startIndex + i);
        i += unsharedBytes.byteLength;
        const valueLength = new Varint_1.default(this.fileBytes, this.startIndex + i);
        i += valueLength.byteLength;
        // mystery: why is unsharedKeyBytes 8 bytes longer than what we are expecting for keys?
        this.unsharedKeyBytes = incomingBytes.subarray(startingIndex + i, startingIndex + i + unsharedBytes.value - 8);
        /*const extraBytes = incomingBytes.subarray(
          startingIndex + i + unsharedBytes.value - 8,
          startingIndex + i + unsharedBytes.value
        ); */
        i += unsharedBytes.value;
        this.value = incomingBytes.subarray(startingIndex + i, startingIndex + i + valueLength.value);
        i += valueLength.value;
        /*    this.restarts = [];
    
        for (let j = 0; j < restartsToRead; j++) {
          const offset = startingIndex + i + j * 4;
          this.restarts.push(
            DataUtilities.getUnsignedInteger(
              incomingBytes[offset],
              incomingBytes[offset + 1],
              incomingBytes[offset + 2],
              incomingBytes[offset + 3],
              true
            )
          );
        }
        const offset = startingIndex + i + restartsToRead * 4;
    
        const numRestarts = DataUtilities.getUnsignedInteger(
          incomingBytes[offset],
          incomingBytes[offset + 1],
          incomingBytes[offset + 2],
          incomingBytes[offset + 3],
          false
        );
    
        if (numRestarts != restartsToRead) {
          throw new Error("Unexpected restart mismatch");
        }*/
        this.length = i;
    }
}
exports.default = LevelKeyValue;

//# sourceMappingURL=../maps/minecraft/LevelKeyValue.js.map
