"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 TlvNumber_exports = {};
__export(TlvNumber_exports, {
  MATTER_EPOCH_OFFSET: () => MATTER_EPOCH_OFFSET,
  MATTER_EPOCH_OFFSET_S: () => MATTER_EPOCH_OFFSET_S,
  MATTER_EPOCH_OFFSET_US: () => MATTER_EPOCH_OFFSET_US,
  TlvBitmap: () => TlvBitmap,
  TlvDouble: () => TlvDouble,
  TlvEnum: () => TlvEnum,
  TlvEpochS: () => TlvEpochS,
  TlvEpochUs: () => TlvEpochUs,
  TlvFloat: () => TlvFloat,
  TlvInt16: () => TlvInt16,
  TlvInt32: () => TlvInt32,
  TlvInt64: () => TlvInt64,
  TlvInt8: () => TlvInt8,
  TlvLongNumberSchema: () => TlvLongNumberSchema,
  TlvNumberSchema: () => TlvNumberSchema,
  TlvNumericSchema: () => TlvNumericSchema,
  TlvPercent: () => TlvPercent,
  TlvPercent100ths: () => TlvPercent100ths,
  TlvPosixMs: () => TlvPosixMs,
  TlvSysTimeMS: () => TlvSysTimeMS,
  TlvSysTimeUs: () => TlvSysTimeUs,
  TlvUInt16: () => TlvUInt16,
  TlvUInt24: () => TlvUInt24,
  TlvUInt32: () => TlvUInt32,
  TlvUInt64: () => TlvUInt64,
  TlvUInt8: () => TlvUInt8
});
module.exports = __toCommonJS(TlvNumber_exports);
var import_general = require("#general");
var import_ValidationError = require("../common/ValidationError.js");
var import_BitmapSchema = require("../schema/BitmapSchema.js");
var import_TlvCodec = require("./TlvCodec.js");
var import_TlvSchema = require("./TlvSchema.js");
var import_TlvWrapper = require("./TlvWrapper.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class TlvNumericSchema extends import_TlvSchema.TlvSchema {
  constructor(type, lengthProvider, baseTypeMin, baseTypeMax, min, max) {
    super();
    this.type = type;
    this.lengthProvider = lengthProvider;
    this.baseTypeMin = baseTypeMin;
    this.baseTypeMax = baseTypeMax;
    if (min !== void 0 && min > baseTypeMin) {
      this.#min = min;
    }
    if (max !== void 0 && max < baseTypeMax) {
      this.#max = max;
    }
  }
  #min;
  #max;
  get min() {
    return this.#min ?? this.baseTypeMin;
  }
  get max() {
    return this.#max ?? this.baseTypeMax;
  }
  encodeTlvInternal(writer, value, tag) {
    const typeLength = { type: this.type, length: this.lengthProvider(value) };
    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}, was expecting ${this.type}.`);
    return reader.readPrimitive(typeLength);
  }
  validate(value) {
    if (typeof value !== "number" && typeof value !== "bigint")
      throw new import_ValidationError.ValidationDatatypeMismatchError(`Expected number, got ${typeof value}.`);
    this.validateBoundaries(value);
  }
  validateBoundaries(value) {
    if (this.min !== void 0 && value < this.min)
      throw new import_ValidationError.ValidationOutOfBoundsError(`Invalid value: ${value} is below the minimum, ${this.min}.`);
    if (this.max !== void 0 && value > this.max)
      throw new import_ValidationError.ValidationOutOfBoundsError(`Invalid value: ${value} is above the maximum, ${this.max}.`);
  }
  /** Restrict value range. */
  bound({ min, max }) {
    return new TlvNumericSchema(
      this.type,
      this.lengthProvider,
      (0, import_general.maxValue)(min, this.min),
      (0, import_general.minValue)(max, this.max)
    );
  }
}
class TlvNumberSchema extends TlvNumericSchema {
  constructor(type, lengthProvider, baseTypeMin, baseTypeMax, min, max) {
    super(type, lengthProvider, baseTypeMin, baseTypeMax, min, max);
  }
  decodeTlvInternalValue(reader, typeLength) {
    const value = super.decodeTlvInternalValue(reader, typeLength);
    return typeof value === "bigint" ? Number(value) : value;
  }
  bound({ min, max }) {
    return new TlvNumberSchema(
      this.type,
      this.lengthProvider,
      this.baseTypeMin,
      this.baseTypeMax,
      (0, import_general.maxValue)(min, this.min),
      (0, import_general.minValue)(max, this.max)
    );
  }
  validate(value) {
    if (typeof value !== "number")
      throw new import_ValidationError.ValidationDatatypeMismatchError(`Expected number, got ${typeof value}.`);
    this.validateBoundaries(value);
  }
}
const TlvLongNumberSchema = TlvNumericSchema;
const TlvFloat = new TlvNumberSchema(import_TlvCodec.TlvType.Float, (_value) => import_TlvCodec.TlvLength.FourBytes, import_general.FLOAT32_MIN, import_general.FLOAT32_MAX);
const TlvDouble = new TlvNumberSchema(import_TlvCodec.TlvType.Float, (_value) => import_TlvCodec.TlvLength.EightBytes, import_general.FLOAT64_MIN, import_general.FLOAT64_MAX);
const TlvInt8 = new TlvNumberSchema(
  import_TlvCodec.TlvType.SignedInt,
  (value) => import_TlvCodec.TlvCodec.getIntTlvLength(value),
  import_general.INT8_MIN,
  import_general.INT8_MAX
);
const TlvInt16 = new TlvNumberSchema(
  import_TlvCodec.TlvType.SignedInt,
  (value) => import_TlvCodec.TlvCodec.getIntTlvLength(value),
  import_general.INT16_MIN,
  import_general.INT16_MAX
);
const TlvInt32 = new TlvNumberSchema(
  import_TlvCodec.TlvType.SignedInt,
  (value) => import_TlvCodec.TlvCodec.getIntTlvLength(value),
  import_general.INT32_MIN,
  import_general.INT32_MAX
);
const TlvInt64 = new TlvLongNumberSchema(
  import_TlvCodec.TlvType.SignedInt,
  (value) => import_TlvCodec.TlvCodec.getIntTlvLength(value),
  import_general.INT64_MIN,
  import_general.INT64_MAX
);
const TlvUInt8 = new TlvNumberSchema(
  import_TlvCodec.TlvType.UnsignedInt,
  (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
  0,
  import_general.UINT8_MAX
);
const TlvUInt16 = new TlvNumberSchema(
  import_TlvCodec.TlvType.UnsignedInt,
  (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
  0,
  import_general.UINT16_MAX
);
const TlvUInt24 = new TlvNumberSchema(
  import_TlvCodec.TlvType.UnsignedInt,
  (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
  0,
  import_general.UINT24_MAX
);
const TlvUInt32 = new TlvNumberSchema(
  import_TlvCodec.TlvType.UnsignedInt,
  (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
  0,
  import_general.UINT32_MAX
);
const TlvUInt64 = new TlvLongNumberSchema(
  import_TlvCodec.TlvType.UnsignedInt,
  (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
  0,
  import_general.UINT64_MAX
);
const TlvEnum = () => TlvUInt32;
const TlvBitmap = (underlyingSchema, bitSchema) => {
  const bitmapSchema = (0, import_BitmapSchema.BitmapSchema)(bitSchema);
  return new import_TlvWrapper.TlvWrapper(
    underlyingSchema,
    (bitmapData) => bitmapSchema.encode(bitmapData),
    (value) => bitmapSchema.decode(value)
  );
};
const TlvPercent = TlvUInt8.bound({ max: 100 });
const TlvPercent100ths = TlvUInt16.bound({ max: 1e4 });
const TlvPosixMs = TlvUInt64;
const TlvSysTimeUs = TlvUInt64;
const TlvSysTimeMS = TlvUInt64;
const MATTER_EPOCH_OFFSET = (0, import_general.Days)(10957);
const MATTER_EPOCH_OFFSET_S = import_general.Seconds.of(MATTER_EPOCH_OFFSET);
const MATTER_EPOCH_OFFSET_US = BigInt(MATTER_EPOCH_OFFSET * 1e3);
const TlvEpochS = new import_TlvWrapper.TlvWrapper(
  new TlvNumberSchema(
    import_TlvCodec.TlvType.UnsignedInt,
    (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
    0,
    // too low values will be caught in the wrapper
    import_general.UINT32_MAX + MATTER_EPOCH_OFFSET_S
  ),
  (unixEpoch) => {
    const value = unixEpoch - MATTER_EPOCH_OFFSET_S;
    if (value < 0) {
      throw new import_general.ImplementationError(
        "Do not convert Epoch-values yourself, use TlvEpochS directly with unix epoch values."
      );
    }
    return value;
  },
  (epochS) => epochS + MATTER_EPOCH_OFFSET_S
);
const TlvEpochUs = new import_TlvWrapper.TlvWrapper(
  new TlvLongNumberSchema(
    import_TlvCodec.TlvType.UnsignedInt,
    (value) => import_TlvCodec.TlvCodec.getUIntTlvLength(value),
    0,
    // too low values will be caught in the wrapper
    import_general.UINT64_MAX + MATTER_EPOCH_OFFSET_US
  ),
  (unixEpoch) => {
    const result = BigInt(unixEpoch) - MATTER_EPOCH_OFFSET_US;
    if (result < BigInt(0)) {
      throw new import_general.ImplementationError(
        "Do not convert Epoch-values yourself, use TlvEpochUs directly with unix epoch values."
      );
    }
    return result;
  },
  (epochUs) => BigInt(epochUs) + MATTER_EPOCH_OFFSET_US
);
//# sourceMappingURL=TlvNumber.js.map
