"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var DataReader_exports = {};
__export(DataReader_exports, {
  DataReader: () => DataReader
});
module.exports = __toCommonJS(DataReader_exports);
var import_Bytes = require("./Bytes.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class DataReader {
  #littleEndian;
  #dataView;
  #buffer;
  #offset = 0;
  constructor(buffer, endian) {
    this.#buffer = import_Bytes.Bytes.of(buffer);
    this.#dataView = import_Bytes.Bytes.dataViewOf(this.#buffer);
    this.#littleEndian = endian === import_Bytes.Endian.Little;
  }
  readUInt8() {
    return this.#dataView.getUint8(this.getOffsetAndAdvance(1));
  }
  readUInt16() {
    return this.#dataView.getUint16(this.getOffsetAndAdvance(2), this.#littleEndian);
  }
  readUInt32() {
    return this.#dataView.getUint32(this.getOffsetAndAdvance(4), this.#littleEndian);
  }
  readUInt64() {
    return this.#dataView.getBigUint64(this.getOffsetAndAdvance(8), this.#littleEndian);
  }
  readInt8() {
    return this.#dataView.getInt8(this.getOffsetAndAdvance(1));
  }
  readInt16() {
    return this.#dataView.getInt16(this.getOffsetAndAdvance(2), this.#littleEndian);
  }
  readInt32() {
    return this.#dataView.getInt32(this.getOffsetAndAdvance(4), this.#littleEndian);
  }
  readInt64() {
    return this.#dataView.getBigInt64(this.getOffsetAndAdvance(8), this.#littleEndian);
  }
  readFloat() {
    return this.#dataView.getFloat32(this.getOffsetAndAdvance(4), this.#littleEndian);
  }
  readDouble() {
    return this.#dataView.getFloat64(this.getOffsetAndAdvance(8), this.#littleEndian);
  }
  readUtf8String(length) {
    const offset = this.getOffsetAndAdvance(length);
    return new TextDecoder().decode(this.#buffer.subarray(offset, offset + length));
  }
  readByteArray(length) {
    const offset = this.getOffsetAndAdvance(length);
    return this.#buffer.subarray(offset, offset + length);
  }
  get remainingBytesCount() {
    return this.#dataView.byteLength - this.#offset;
  }
  get remainingBytes() {
    return this.#buffer.subarray(this.#offset);
  }
  get length() {
    return this.#dataView.byteLength;
  }
  set offset(offset) {
    if (offset > this.#dataView.byteLength) {
      throw new Error(`Offset ${offset} is out of bounds.`);
    }
    this.#offset = offset;
  }
  get offset() {
    return this.#offset;
  }
  getOffsetAndAdvance(size) {
    const result = this.#offset;
    this.#offset += size;
    if (this.#offset > this.#dataView.byteLength) {
      this.#offset = this.#dataView.byteLength;
    }
    return result;
  }
}
//# sourceMappingURL=DataReader.js.map
