"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 TlvSchema_exports = {};
__export(TlvSchema_exports, {
  TlvArrayReader: () => TlvArrayReader,
  TlvArrayWriter: () => TlvArrayWriter,
  TlvByteArrayReader: () => TlvByteArrayReader,
  TlvByteArrayWriter: () => TlvByteArrayWriter,
  TlvSchema: () => TlvSchema
});
module.exports = __toCommonJS(TlvSchema_exports);
var import_general = require("@matter/general");
var import_Schema = require("../schema/Schema.js");
var import_TlvCodec = require("./TlvCodec.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class TlvSchema extends import_Schema.Schema {
  decodeInternal(encoded) {
    return this.decodeTlvInternal(new TlvByteArrayReader(encoded)).value;
  }
  encodeInternal(value) {
    const writer = new TlvByteArrayWriter();
    this.encodeTlvInternal(writer, value);
    return writer.toByteArray();
  }
  encodeTlv(value, options) {
    const writer = new TlvArrayWriter();
    this.encodeTlvInternal(writer, value, void 0, options);
    return writer.toTlvArray();
  }
  decodeTlv(encoded) {
    return this.decodeTlvInternal(new TlvArrayReader(encoded)).value;
  }
  decodeTlvInternal(reader) {
    const { tag, typeLength } = reader.readTagType();
    return { tag, value: this.decodeTlvInternalValue(reader, typeLength) };
  }
  injectField(value, _fieldId, _fieldValue, _injectChecker) {
    return value;
  }
  removeField(value, _fieldId, _removeChecker) {
    return value;
  }
}
class TlvArrayWriter {
  tlvArray = new Array();
  writeTag(typeLength, tag) {
    this.tlvArray.push({ tag, typeLength });
  }
  writePrimitive(_typeLength, value) {
    this.tlvArray[this.tlvArray.length - 1].value = value;
  }
  toTlvArray() {
    return this.tlvArray;
  }
}
class TlvArrayReader {
  constructor(tlvElements) {
    this.tlvElements = tlvElements;
  }
  index = -1;
  readTagType() {
    this.index++;
    return this.tlvElements[this.index];
  }
  readPrimitive(_typeLength) {
    return this.tlvElements[this.index].value;
  }
}
class TlvByteArrayWriter {
  writer = new import_general.DataWriter(import_general.Endian.Little);
  writeTag(typeLength, tag) {
    import_TlvCodec.TlvCodec.writeTag(this.writer, typeLength, tag);
  }
  writePrimitive(typeLength, value) {
    import_TlvCodec.TlvCodec.writePrimitive(this.writer, typeLength, value);
  }
  toByteArray() {
    return this.writer.toByteArray();
  }
}
class TlvByteArrayReader {
  reader;
  constructor(byteArray) {
    this.reader = new import_general.DataReader(byteArray, import_general.Endian.Little);
  }
  readTagType() {
    return import_TlvCodec.TlvCodec.readTagType(this.reader);
  }
  readPrimitive(typeLength) {
    return import_TlvCodec.TlvCodec.readPrimitive(this.reader, typeLength);
  }
}
//# sourceMappingURL=TlvSchema.js.map
