"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 TlvNullable_exports = {};
__export(TlvNullable_exports, {
  NullableSchema: () => NullableSchema,
  TlvNullable: () => TlvNullable
});
module.exports = __toCommonJS(TlvNullable_exports);
var import_TlvNumber = require("#tlv/TlvNumber.js");
var import_TlvArray = require("./TlvArray.js");
var import_TlvCodec = require("./TlvCodec.js");
var import_TlvSchema = require("./TlvSchema.js");
var import_TlvString = require("./TlvString.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class NullableSchema extends import_TlvSchema.TlvSchema {
  schema;
  constructor(schema) {
    super();
    if (schema instanceof import_TlvNumber.TlvNumericSchema && schema.type !== import_TlvCodec.TlvType.Float) {
      if (schema.baseTypeMin === 0 && schema.max === schema.baseTypeMax) {
        if (typeof schema.baseTypeMax === "number") {
          schema = schema.bound({ min: schema.min, max: schema.baseTypeMax - 1 });
        } else {
          schema = schema.bound({ min: schema.min, max: schema.baseTypeMax - BigInt(1) });
        }
      } else if (schema.baseTypeMin < 0 && schema.min === schema.baseTypeMin) {
        if (typeof schema.baseTypeMin === "number") {
          schema = schema.bound({ min: schema.baseTypeMin + 1, max: schema.max });
        } else {
          schema = schema.bound({ min: schema.baseTypeMin + BigInt(1), max: schema.max });
        }
      }
    }
    this.schema = schema;
  }
  encodeTlvInternal(writer, value, tag, options) {
    if (value === null) {
      writer.writeTag({ type: import_TlvCodec.TlvType.Null }, tag);
    } else {
      this.schema.encodeTlvInternal(writer, value, tag, options);
    }
  }
  decodeTlvInternalValue(reader, typeLength) {
    if (typeLength.type === import_TlvCodec.TlvType.Null) return null;
    const value = this.schema.decodeTlvInternalValue(reader, typeLength);
    if (value !== null && (this.schema instanceof import_TlvArray.ArraySchema || this.schema instanceof import_TlvString.StringSchema) && value.length === 0) {
      if (this.schema instanceof import_TlvString.StringSchema && this.schema.type === import_TlvCodec.TlvType.ByteString || this.schema.minLength !== void 0 && this.schema.minLength > 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);
//# sourceMappingURL=TlvNullable.js.map
