"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 assertions_exports = {};
__export(assertions_exports, {
  assertArray: () => assertArray,
  assertBoolean: () => assertBoolean,
  assertBytes: () => assertBytes,
  assertInt: () => assertInt,
  assertNumber: () => assertNumber,
  assertNumeric: () => assertNumeric,
  assertObject: () => assertObject,
  assertSequence: () => assertSequence,
  assertString: () => assertString
});
module.exports = __toCommonJS(assertions_exports);
var import_general = require("#general");
var import_protocol = require("#protocol");
var import_types = require("@matter/types");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function assertNumber(value, path) {
  if (Number.isFinite(value)) {
    return;
  }
  throw new import_protocol.DatatypeError(path, "a number", value);
}
function assertBoolean(value, path) {
  if (typeof value === "boolean" || value === 0 || value === 1) {
    return;
  }
  throw new import_protocol.DatatypeError(path, "a boolean", value);
}
function assertObject(value, path) {
  if ((0, import_general.isObject)(value)) {
    return;
  }
  throw new import_protocol.DatatypeError(path, "an object", value);
}
function assertNumeric(value, path) {
  if (typeof value === "number" || typeof value === "bigint") {
    return;
  }
  throw new import_protocol.DatatypeError(path, "a number or bigint", value);
}
function assertString(value, path) {
  if (typeof value === "string") {
    return;
  }
  throw new import_protocol.DatatypeError(path, "a string", value);
}
function assertBytes(value, path) {
  if (import_general.Bytes.isBytes(value)) {
    return;
  }
  throw new import_protocol.DatatypeError(path, "a byte array", value);
}
function assertSequence(value, path) {
  if (typeof value === "string" || import_general.Bytes.isBytes(value)) {
    return;
  }
  throw new import_protocol.DatatypeError(path, "a string or byte array", value);
}
function assertArray(value, path) {
  if (!Array.isArray(value)) {
    throw new import_protocol.DatatypeError(path, "an array", value);
  }
}
const assertInt = {
  /**
   * Assertions for each integer type that is not nullable.
   */
  notNullable: {},
  /**
   * Assertions for nullable integer types.
   *
   * These are separate from the "not nullable" assertions because Matter reserves a high or low value (for unsigned
   * and signed, respectively) to indicate the field is null.
   */
  nullable: {}
};
for (let i = 1n; i < 9n; i++) {
  const intName = `int${i * 8n}`;
  const uintName = `u${intName}`;
  const numValues = 2n ** (i * 8n);
  const unsignedMax = numValues - 1n;
  assertInt.notNullable[uintName] = createIntAssertion(uintName, 0n, unsignedMax);
  assertInt.nullable[uintName] = createIntAssertion(`nullable ${uintName}`, 0n, unsignedMax - 1n);
  const halfNumValues = numValues / 2n;
  const signedMax = halfNumValues - 1n;
  const signedMin = -halfNumValues;
  assertInt.notNullable[intName] = createIntAssertion(intName, signedMin, signedMax);
  assertInt.nullable[intName] = createIntAssertion(`nullable ${intName}`, signedMin + 1n, signedMax);
}
assertInt.notNullable["epoch-s"] = createIntAssertion(
  "epoch-s",
  BigInt(import_types.MATTER_EPOCH_OFFSET_S),
  BigInt(import_general.UINT32_MAX + import_types.MATTER_EPOCH_OFFSET_S)
);
assertInt.nullable["epoch-s"] = createIntAssertion(
  `nullable epoch-s`,
  BigInt(import_types.MATTER_EPOCH_OFFSET_S),
  BigInt(import_general.UINT32_MAX + import_types.MATTER_EPOCH_OFFSET_S) - 1n
);
assertInt.notNullable["epoch-us"] = createIntAssertion(
  "epoch-us",
  import_types.MATTER_EPOCH_OFFSET_US,
  import_general.UINT64_MAX + import_types.MATTER_EPOCH_OFFSET_US
);
assertInt.nullable["epoch-us"] = createIntAssertion(
  `nullable epoch-us`,
  import_types.MATTER_EPOCH_OFFSET_US,
  import_general.UINT64_MAX + import_types.MATTER_EPOCH_OFFSET_US - 1n
);
function createIntAssertion(name, lowerBoundInclusive, upperBoundExclusive) {
  if (lowerBoundInclusive < Number.MIN_SAFE_INTEGER || upperBoundExclusive > Number.MAX_SAFE_INTEGER) {
    return createVarIntAssertion(name, lowerBoundInclusive, upperBoundExclusive);
  }
  return createVarIntAssertion(name, Number(lowerBoundInclusive), Number(upperBoundExclusive));
}
function createVarIntAssertion(name, min, max) {
  return function assertInt2(value, path) {
    assertNumeric(value, path);
    if (value < min) {
      throw new import_protocol.IntegerRangeError(path, `Value ${value} is below the ${name} minimum of ${min}`);
    }
    if (value > max) {
      throw new import_protocol.IntegerRangeError(path, `Value ${value} is above the ${name} maximum of ${max}`);
    }
  };
}
//# sourceMappingURL=assertions.js.map
