/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes, ImplementationError, serialize, UnexpectedDataError } from "#general";
import { ValidationDatatypeMismatchError, ValidationOutOfBoundsError } from "../common/ValidationError.js";
import { TlvCodec, TlvType } from "./TlvCodec.js";
import { TlvSchema } from "./TlvSchema.js";
class StringSchema extends TlvSchema {
  constructor(type, minLength = 0, maxLength = 1024) {
    super();
    this.type = type;
    this.minLength = minLength;
    this.maxLength = maxLength;
    if (minLength < 0) throw new ImplementationError("Minimum length should be a positive number.");
    if (maxLength < 0) throw new ImplementationError("Maximum length should be a positive number.");
    if (minLength > maxLength)
      throw new 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: TlvCodec.getUIntTlvLength(length) };
    writer.writeTag(typeLength, tag);
    writer.writePrimitive(typeLength, value);
  }
  decodeTlvInternalValue(reader, typeLength) {
    if (typeLength.type !== this.type) throw new UnexpectedDataError(`Unexpected type ${typeLength.type}.`);
    return reader.readPrimitive(typeLength);
  }
  validate(value) {
    if (this.type === TlvType.Utf8String && typeof value !== "string")
      throw new ValidationDatatypeMismatchError(`Expected string, got ${typeof value}.`);
    if (this.type === TlvType.ByteString && !Bytes.isBytes(value))
      throw new ValidationDatatypeMismatchError(`Expected bytes, got ${typeof value}.`);
    const length = typeof value === "string" ? value.length : value.byteLength;
    if (length > this.maxLength)
      throw new ValidationOutOfBoundsError(
        `String ${serialize(value)} is too long: ${length}, max ${this.maxLength}.`
      );
    if (length < this.minLength)
      throw new ValidationOutOfBoundsError(
        `String ${serialize(value)} is too short: ${length}, min ${this.minLength}.`
      );
  }
  bound({ minLength, maxLength, length }) {
    return new StringSchema(
      this.type,
      length ?? minLength ?? this.minLength,
      length ?? maxLength ?? this.maxLength
    );
  }
}
const TlvByteString = new StringSchema(TlvType.ByteString);
const TlvString = new StringSchema(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 });
export {
  StringSchema,
  TlvByteString,
  TlvHardwareAddress,
  TlvString,
  TlvString256max,
  TlvString32max,
  TlvString64max
};
//# sourceMappingURL=TlvString.js.map
