"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 TlvString_exports = {};
__export(TlvString_exports, {
  StringSchema: () => StringSchema,
  TlvByteString: () => TlvByteString,
  TlvHardwareAddress: () => TlvHardwareAddress,
  TlvString: () => TlvString,
  TlvString256max: () => TlvString256max,
  TlvString32max: () => TlvString32max,
  TlvString64max: () => TlvString64max
});
module.exports = __toCommonJS(TlvString_exports);
var import_general = require("@matter/general");
var import_ValidationError = require("../common/ValidationError.js");
var import_TlvCodec = require("./TlvCodec.js");
var import_TlvSchema = require("./TlvSchema.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const stringBoundCache = /* @__PURE__ */ new WeakMap();
class StringSchema extends import_TlvSchema.TlvSchema {
  constructor(type, minLength = 0, maxLength = 1024) {
    super();
    this.type = type;
    this.minLength = minLength;
    this.maxLength = maxLength;
    if (minLength < 0) throw new import_general.ImplementationError("Minimum length should be a positive number.");
    if (maxLength < 0) throw new import_general.ImplementationError("Maximum length should be a positive number.");
    if (minLength > maxLength)
      throw new import_general.ImplementationError("Minimum length should be smaller than maximum length.");
  }
  encodeTlvInternal(writer, value, tag) {
    const length = typeof value === "string" ? value.length : value.byteLength;
    const typeLength = { type: this.type, length: import_TlvCodec.TlvCodec.getUIntTlvLength(length) };
    writer.writeTag(typeLength, tag);
    writer.writePrimitive(typeLength, value);
  }
  decodeTlvInternalValue(reader, typeLength) {
    if (typeLength.type !== this.type) throw new import_general.UnexpectedDataError(`Unexpected type ${typeLength.type}.`);
    return reader.readPrimitive(typeLength);
  }
  validate(value) {
    if (this.type === import_TlvCodec.TlvType.Utf8String && typeof value !== "string")
      throw new import_ValidationError.ValidationDatatypeMismatchError(`Expected string, got ${typeof value}.`);
    if (this.type === import_TlvCodec.TlvType.ByteString && !import_general.Bytes.isBytes(value))
      throw new import_ValidationError.ValidationDatatypeMismatchError(`Expected bytes, got ${typeof value}.`);
    const length = typeof value === "string" ? value.length : value.byteLength;
    if (length > this.maxLength)
      throw new import_ValidationError.ValidationOutOfBoundsError(
        `String ${(0, import_general.serialize)(value)} is too long: ${length}, max ${this.maxLength}.`
      );
    if (length < this.minLength)
      throw new import_ValidationError.ValidationOutOfBoundsError(
        `String ${(0, import_general.serialize)(value)} is too short: ${length}, min ${this.minLength}.`
      );
  }
  /** @deprecated Part of old ClusterType() compat layer. */
  get element() {
    const result = {
      type: this.type === import_TlvCodec.TlvType.Utf8String ? "string" : "octstr"
    };
    const constraint = {};
    if (this.minLength > 0) {
      constraint.min = this.minLength;
    }
    if (this.maxLength !== 1024) {
      constraint.max = this.maxLength;
    }
    if (constraint.min !== void 0 || constraint.max !== void 0) {
      result.constraint = constraint;
    }
    return result;
  }
  bound({ minLength, maxLength, length }) {
    const effectiveMin = length ?? minLength ?? this.minLength;
    const effectiveMax = length ?? maxLength ?? this.maxLength;
    const key = `${effectiveMin}:${effectiveMax}`;
    let inner = stringBoundCache.get(this);
    if (inner === void 0) {
      inner = /* @__PURE__ */ new Map();
      stringBoundCache.set(this, inner);
    }
    let result = inner.get(key);
    if (result === void 0) {
      result = new StringSchema(this.type, effectiveMin, effectiveMax);
      inner.set(key, result);
    }
    return result;
  }
}
const TlvByteString = new StringSchema(import_TlvCodec.TlvType.ByteString);
const TlvString = new StringSchema(import_TlvCodec.TlvType.Utf8String);
const TlvString32max = TlvString.bound({ maxLength: 32 });
const TlvString64max = TlvString.bound({ maxLength: 64 });
const TlvString256max = TlvString.bound({ maxLength: 256 });
const TlvHardwareAddress = TlvByteString.bound({ minLength: 6, maxLength: 8 });
//# sourceMappingURL=TlvString.js.map
