"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 DataWriter_exports = {};
__export(DataWriter_exports, {
  DataWriter: () => DataWriter
});
module.exports = __toCommonJS(DataWriter_exports);
var import_Bytes = require("./Bytes.js");
var import_Number = require("./Number.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class DataWriter {
  littleEndian;
  length = 0;
  chunks = new Array();
  constructor(endian) {
    this.littleEndian = endian === import_Bytes.Endian.Little;
  }
  writeUInt8(value) {
    this.chunks.push(new Uint8Array([(0, import_Number.toNumber)(value)]));
    this.length += 1;
  }
  writeUInt16(value) {
    const chunk = new Uint8Array(2);
    new DataView(chunk.buffer, 0, 2).setUint16(0, (0, import_Number.toNumber)(value), this.littleEndian);
    this.chunks.push(chunk);
    this.length += 2;
  }
  writeUInt32(value) {
    const chunk = new Uint8Array(4);
    new DataView(chunk.buffer, 0, 4).setUint32(0, (0, import_Number.toNumber)(value), this.littleEndian);
    this.chunks.push(chunk);
    this.length += 4;
  }
  writeUInt64(value) {
    const chunk = new Uint8Array(8);
    new DataView(chunk.buffer, 0, 8).setBigUint64(0, BigInt(value), this.littleEndian);
    this.chunks.push(chunk);
    this.length += 8;
  }
  writeInt8(value) {
    const chunk = new Uint8Array(1);
    new DataView(chunk.buffer, 0, 1).setInt8(0, (0, import_Number.toNumber)(value));
    this.chunks.push(chunk);
    this.length += 1;
  }
  writeInt16(value) {
    const chunk = new Uint8Array(2);
    new DataView(chunk.buffer, 0, 2).setInt16(0, (0, import_Number.toNumber)(value), this.littleEndian);
    this.chunks.push(chunk);
    this.length += 2;
  }
  writeInt32(value) {
    const chunk = new Uint8Array(4);
    new DataView(chunk.buffer, 0, 4).setInt32(0, (0, import_Number.toNumber)(value), this.littleEndian);
    this.chunks.push(chunk);
    this.length += 4;
  }
  writeInt64(value) {
    const chunk = new Uint8Array(8);
    new DataView(chunk.buffer, 0, 8).setBigInt64(0, BigInt(value), this.littleEndian);
    this.chunks.push(chunk);
    this.length += 8;
  }
  writeFloat(value) {
    const chunk = new Uint8Array(4);
    new DataView(chunk.buffer, 0, 4).setFloat32(0, value, this.littleEndian);
    this.chunks.push(chunk);
    this.length += 4;
  }
  writeDouble(value) {
    const chunk = new Uint8Array(8);
    new DataView(chunk.buffer, 0, 8).setFloat64(0, value, this.littleEndian);
    this.chunks.push(chunk);
    this.length += 8;
  }
  writeByteArray(value) {
    this.chunks.push(import_Bytes.Bytes.of(value));
    this.length += value.byteLength;
  }
  toByteArray() {
    if (this.chunks.length === 0) return new Uint8Array(0);
    if (this.chunks.length === 1) return this.chunks[0];
    const result = new Uint8Array(this.length);
    let offset = 0;
    this.chunks.forEach((chunk) => {
      result.set(chunk, offset);
      offset += chunk.byteLength;
    });
    this.chunks.length = 0;
    this.chunks.push(result);
    return result;
  }
}
//# sourceMappingURL=DataWriter.js.map
