/**
 * Tiny binary reader for Uint8Array-backed DataView.
 * Shared by ZIP parsers.
 */
import { decodeLatin1, decodeUtf8 } from "./text.js";
export function writeUint32LE(value) {
    const out = new Uint8Array(4);
    const view = new DataView(out.buffer);
    view.setUint32(0, value >>> 0, true);
    return out;
}
export function readUint32LE(data, offset) {
    const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
    return view.getUint32(offset, true);
}
export class BinaryReader {
    constructor(data, offset = 0) {
        this.data = data;
        this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
        this.offset = offset;
    }
    get position() {
        return this.offset;
    }
    set position(value) {
        this.offset = value;
    }
    get remaining() {
        return this.data.length - this.offset;
    }
    readUint8() {
        const value = this.view.getUint8(this.offset);
        this.offset += 1;
        return value;
    }
    readUint16() {
        const value = this.view.getUint16(this.offset, true);
        this.offset += 2;
        return value;
    }
    readUint32() {
        const value = this.view.getUint32(this.offset, true);
        this.offset += 4;
        return value;
    }
    readBigUint64() {
        const value = this.view.getBigUint64(this.offset, true);
        this.offset += 8;
        return value;
    }
    readBytes(length) {
        const bytes = this.data.subarray(this.offset, this.offset + length);
        this.offset += length;
        return bytes;
    }
    readString(length, utf8 = true) {
        const bytes = this.readBytes(length);
        return utf8 ? decodeUtf8(bytes) : decodeLatin1(bytes);
    }
    skip(length) {
        this.offset += length;
    }
    slice(start, end) {
        return this.data.subarray(start, end);
    }
    peekUint32(offset) {
        return this.view.getUint32(offset, true);
    }
}
