/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ArraySchema } from "./TlvArray.js";
import { TlvType } from "./TlvCodec.js";
import { TlvSchema } from "./TlvSchema.js";
import { StringSchema } from "./TlvString.js";
class NullableSchema extends TlvSchema {
  constructor(schema) {
    super();
    this.schema = schema;
  }
  encodeTlvInternal(writer, value, tag, options) {
    if (value === null) {
      writer.writeTag({ type: TlvType.Null }, tag);
    } else {
      this.schema.encodeTlvInternal(writer, value, tag, options);
    }
  }
  decodeTlvInternalValue(reader, typeLength) {
    if (typeLength.type === TlvType.Null) return null;
    const value = this.schema.decodeTlvInternalValue(reader, typeLength);
    if (value !== null && (this.schema instanceof ArraySchema || this.schema instanceof StringSchema) && value.length === 0) {
      return null;
    }
    return value;
  }
  validate(value) {
    if (value !== null) this.schema.validate(value);
  }
  injectField(value, fieldId, fieldValue, injectChecker) {
    if (value !== null) {
      return this.schema.injectField(value, fieldId, fieldValue, injectChecker);
    }
    return value;
  }
  removeField(value, fieldId, removeChecker) {
    if (value !== null) {
      return this.schema.removeField(value, fieldId, removeChecker);
    }
    return value;
  }
}
const TlvNullable = (schema) => new NullableSchema(schema);
export {
  NullableSchema,
  TlvNullable
};
//# sourceMappingURL=TlvNullable.js.map
