/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { DataReader, DataWriter, Endian } from "@matter/general";
import { Schema } from "../schema/Schema.js";
import { TlvCodec } from "./TlvCodec.js";
class TlvSchema extends 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 DataWriter(Endian.Little);
  writeTag(typeLength, tag) {
    TlvCodec.writeTag(this.writer, typeLength, tag);
  }
  writePrimitive(typeLength, value) {
    TlvCodec.writePrimitive(this.writer, typeLength, value);
  }
  toByteArray() {
    return this.writer.toByteArray();
  }
}
class TlvByteArrayReader {
  reader;
  constructor(byteArray) {
    this.reader = new DataReader(byteArray, Endian.Little);
  }
  readTagType() {
    return TlvCodec.readTagType(this.reader);
  }
  readPrimitive(typeLength) {
    return TlvCodec.readPrimitive(this.reader, typeLength);
  }
}
export {
  TlvArrayReader,
  TlvArrayWriter,
  TlvByteArrayReader,
  TlvByteArrayWriter,
  TlvSchema
};
//# sourceMappingURL=TlvSchema.js.map
